5 Replies Latest reply on May 9, 2007 2:37 AM by gooberachi

    Controlling fire in Space Invaders game

    gooberachi
      Hi again,

      still working through the tutorial on Space Invaders in Flash MX 2004 (bit out of date, I know).

      I've got the player craft under mouse control. What I don't like about the game is that you can fire as rapidly as you wan't, though the number of bullets is limited. You can change this limit in the variable - I would rather have it though that you can only have a set number of bullets in play at a time (at the moment, if you go over the limit, the uppermost bullets just disapear).

      Here is the code:

      // when the mouse cliks
      onClipEvent(mouseDown)
      {
      // duplicate the bullet mc
      _root.bullet.duplicateMovieClip("bullet"+bulletNum,bulletNum);
      // set the coords to the mouse clik
      eval("_root.bullet" + bulletNum)._x = this._x;
      eval("_root.bullet" + bulletNum)._y = this._y;
      // increment the bullet number
      bulletNum++;
      // if more than 50 bullets , start again at 0
      if(bulletNum>50)
      bulletNum = 0;
      }

      What code could I use to have it so that you can only have, say, 3 bullets in play at once, and you can't fire again until they've hit / left the screen?

      regards,

      g
        • 1. Re: Controlling fire in Space Invaders game
          gooberachi Level 1
          I would like to rephrase my question a little - I'd like it so that you can only fire over a limited period - only one bullet every second, for example.

          Anyone got any ideas about this?
          • 2. Re: Controlling fire in Space Invaders game
            FlashChump
            Why not just keep track of the bullets fired in an array/counter and only when length of the array of value of the counter is less than 3 is there firing allowed.
            • 3. Re: Controlling fire in Space Invaders game
              gooberachi Level 1
              I don't know how to do that - this tutorial, and the advice I've gotten relating to it are my first attempts with flash, so it's all learning.

              Could you explain how to do what you suggested?

              cheers,

              g
              • 4. Re: Controlling fire in Space Invaders game
                FlashChump Level 1
                1) keep track of the bullets. (create an array and save what the duplicateMovieClip returns)
                2) determine when a bullet is off screen or whatever else that it is no longer counted: decrease bulletNum.

                1) is relatively easy, look up the help for arrays and duplicatemovieclip.
                2) is a little harder for a first time flash person. theres several possibilites on how to approach this, the easiest would be to do a brute force onenterframe check if the bullet is outside the screen. look up onenterframe and Stage.
                • 5. Re: Controlling fire in Space Invaders game
                  gooberachi Level 1
                  Hi again,

                  thanks for that. Another way would be to use code like the following:

                  var enableInterval:Number;
                  var allowFire:Boolean;
                  function tempDisableFire():Void {
                  allowFire = false;
                  enableInterval = setInterval(enableFire, 500); // 500 millisecond delay
                  };
                  function enableFire():Void {
                  allowFire = true;
                  clearInterval(enableInterval);
                  };

                  I am unsure though whether to insert this within my existing code, or to use this starting from scratch. If it was inside an onClipEvent tag (the event being the mouse click), I could then use it to control the fire. I'm not sure if I would do this with an "if / then" statement.

                  Any suggestions on this method? I know it may be asking a bit much, but if you could even complete this piece of code that would be magic!

                  cheers,

                  g