A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: [Problem]: else function?? Should work fine!!

  1. #1
    Senior Member
    Join Date
    May 2005
    Posts
    223

    [Problem]: else function?? Should work fine!!

    Ok so I have this game, and the enemy sits on the left side of the screen and goes up and down. I do this by deltaY *= -1 wich is set to 10 ((The speed of the enemy ship)). The problem is, is that im trying to get him to stop if his y position is equal to the ship's why position. This is the code I have on the enemy ship, it should work perfect I have no idea what the problem is!

    onClipEvent(load) {
    deltaY=10;
    }
    onClipEvent(enterFrame) {

    //THE PROBLEM STARTS HERE
    if(this._y==_root.ship._y) {
    trace("shot");
    } else {
    this._y+=deltaY;
    }
    //THE PROBLEM ENDS HERE

    if(this._y<=50) {
    this.deltaY *= -1;
    }
    if(this._y>=390) {
    this.deltaY *=-1;
    }
    }
    I dont know if it makes a difference, but im using flash 8.

    Thanks
    -Joey-

  2. #2
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    See notes in red
    Code:
    onClipEvent(load) {
    	deltaY=10;
    	}
    onClipEvent(enterFrame) {
    
    // Try adding traces here
    trace( "Enemy: " + this._y + " and ship " + _root.ship._y );
    // I think you'll find that they are never the same
    
    //THE PROBLEM STARTS HERE
    	if(this._y==_root.ship._y) {
    		trace("shot");
    	} else {
    		this._y+=deltaY;
    	}
    //THE PROBLEM ENDS HERE
    
    	if(this._y<=50) {
    		this.deltaY *= -1;
    	}
    	if(this._y>=390) {
    		this.deltaY *=-1;
    	}
    }
    If you can read this, you're in the right place.

  3. #3
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Wow, youre right thanks. Any idea how I could make it so they are the same. I just tried making them both start at 200 and I set deltaY to 15.5. Im not really sure how I could do it anymore . . .

  4. #4
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    you need to change it so instead of
    if (something == somethingelse)

    to

    if (something > (somethingelse - 5) {
    if (something < (somethingelse + 5) {
    do something
    }
    }
    this allows a range of difference instead of needing them to be exact.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  5. #5
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Hey guys, thanks alot I got it to work. For those wondering what I ended up doing here is the code . . .

    // Stop if this Y = ship Y else move
    if(this._y > (_root.ship._y-7.5)) {
    trace("shot");
    } else if (moveV==true) {
    this._y+=deltaY;
    }
    if(this._y < (_root.ship._y+7.5)) {
    trace("shot");
    } else if (moveV==true) {
    this._y+=deltaY;
    }

  6. #6
    Senior Member
    Join Date
    May 2005
    Posts
    223
    I tried it with the +5 and -5, but sometimes it would skip over the ship. It works everytime this way though

  7. #7
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Now I have another problem. I have a new layer called shot, with an mc instance name "shot". I want to make it hidden then when the enemy's y position is equal to the ships y position I want to replace the ((trace("shot")) from the code above, to actually duplicating the shot mc, making it visible, making it move to the right by shotMove((wich will be set to 17.5)), and I want it so that there is only one duplicate of the shot mc on the stage at one time. Ive dont this before in a tutorial I took on making a spaceship game. I went back to this tutorial and tried to use what I did in it to make it work for this game, but I couldnt get it to work. Sorry to post a "this is what I want" but im not sure how exactly I would do it . . .
    Thanks
    -Joey-

  8. #8
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Well it actually depends on how you want it implemented but I have several ideas. If I understand correctically you want each fighter to shoot at the main ship when they get close to it on the y plane and you have gotten that to work. Then you want each ship to shoot a laser but only have one laser on the screen at a time. Now this is where i have to guess but I am assuming there will be more than one enemy so there will still be more than one laser on the screen. If you can get the duplicate movie clip part to work and understand dynamic pathing it should not be too hard.

    I would tell the laser which movie clip spawned it.
    _root[name].parentname = this._name;//name represents the name of the duplicated laser
    and set a varible on the shooting ship
    shotfired = true;

    Now when the duplicated laser dies just say:
    _root[parentname].shotfired = false;

    And set an if statement on the shooting ship to only shoot when shotfired is false.
    This could also work if you only wanted one laser on the screen by setting all the varibles to global so every ship shares it.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  9. #9
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Well on level 1 there is a fighter on the left side of the screen. Then as you progress in levels, the enemies on the top left and bottum will begin to move and shoot. All I want is a shot by the enemies, the ship will not shott ((Its more of a dodge the enemies bullets type of game)). I am actually just remaking this game, http://www.freearcade.com/Sphererunn...ererunner.html
    So you can check it out and see what I mean. I have everything working like the text at the top lives, score, bonus score ect. The spheres work. when you run over them they fill in and the variable spheres at the top is reduced by one and a sound is played. The enemy moves up and down on the left side, like in the game and stops when he is close the the ships y position. Now im just trying to get it to shoot like the one in this game.

  10. #10
    Senior Member
    Join Date
    May 2005
    Posts
    223
    While im on the subject I have another question . . . Would it be illegal for me to remake this game and post it on a site I want to put up soon? I wouldnt be making money on the actual game just the advertisments. I mean if you think about it, how many games are called pac-man? That game has been remade a milion times.

  11. #11
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Ok I played the game and my method should still work. I dont think you need to duplicate the bullets though as that will just make it more complicated. For the shoot code in the gun I would put something like: (this is for the gun on the left)

    if (shooting == false) {
    _root.shot._y = this._y
    _root.shot._x = 0;
    _root.shot._visible = true;
    shooting = true;
    }

    and then on the bullet
    if (this._x > 550) {
    _root.gun.shooting = false
    this._visible = false;
    }
    and you could do this seperatly for each gun using different names.
    The idea behind this is for the gun to shoot the bullet then set a varible to tell itself not to shoot anymore. Then, once the bullet has done its stuff the bullet can tell the gun that its ok to shoot again.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  12. #12
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Sorry but I dont exactly understand that. first of all, it says
    if(shooting==false) {
    shooting=true;
    _root.shot._visible=true;
    }
    thats weird because if shooting = false then it suddenly = true so it will always be true correct?? You also have it so that if shooting = false then the shot is visible wich is kinda wird. Do I need to set this variable up like onClipEvent(load) { shooting=false;?? Im also wondering how this will make the bullet shoot. Like there is no this._x+=moveSpeed; When do I declare that shooting is true. all this good stuff
    Sorry, not trying to sound rude or anything.
    Thanks for the help

  13. #13
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Well I skipped quite a bit of code there, all I wrote was the parts that would involve shooting only once.
    What happens at first is that the bullet movie clip will be invisible and just doing its stuff off on the side somewhere.
    And here is some of the gun code:
    onClipEvent(load) {
    shooting = false;//This just initialises the varible
    }
    onClipEvent (enterFrame) {
    blah blah blah // whatever goes here
    //The stuff to decide when to shoot.
    if (shooting == false) { //shooting is false because we set it so in the onclipevent load part
    shooting = true;
    _root.bullet._y = this._y //sets the bullets y position to the guns position
    _root.bullet._x = this._x //same but along the x axis
    _root.bullet._visible = true //makes the bullet visible again
    }
    this means that the shooting loop will only happen once (after the first loop shooting will no longer be false)
    The _root.bullet stuff sets the bullets position to the guns position and makes it so you can see the bullet again.

    So pretty much this code will take the bullet, make it visible and place it at the guns position and tell the gun to not "shoot" again.

    Now on the bullets side:
    onClipEvent (enterFrame) {
    //put moving stuff here
    //put the hittest stuff here
    if (this._x > 550) { //if the bullet goes off the screen
    this._visible = false; // make it invisible
    _root.gun.shooting = false; //this is the clever bit, it sets the guns varible shooting too false;
    }
    Some of the variable names can be a bit confusing but if you just think about how they are being used, it should make more sense.

    And thats it. What this would do is wait for the bullet to move off the screen then make it invisible (just for the sake of it) then it sets the guns varible shooting to false which means that the gun can now shoot all over again. When the gun shoots again it will reset the bullets position and make it visible again it the cycle just repeats.
    And finally here is a quick .fla that shows a primitive use of my code.
    Attached Files Attached Files
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  14. #14
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Before trying it I can already tell you one thing, its going to keep resetting the x and y position do the enemy, atleast while youre sitting in front of the enemy.

  15. #15
    Senior Member
    Join Date
    May 2005
    Posts
    223
    wait my bad, I should finish reading first =X

  16. #16
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Sorry but I still cant get it to work! =X
    Think If I emailed you the .fla you would be able to figure it out? Also do you have msn??

  17. #17
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    I dont have msn but I could probbably figure it out if you e-mailed it too me. I will pm you my e-mail address but its getting late here so I probbably wont get to look at it until tomorow.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  18. #18
    Senior Member
    Join Date
    May 2005
    Posts
    223
    alright cool thanks

  19. #19
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    I just e-mailed the working version to you. Your game is going to be great when its done! I couldn't resist taking a look at it and I got carried away...
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  20. #20
    Senior Member
    Join Date
    May 2005
    Posts
    223
    Hey thanks alot! Not only for the help but for the complement. Too bad I kinda stole the idea. =(

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center