4 Replies Latest reply on Aug 5, 2009 2:16 PM by Mr.Wallace

    Game play controls...

    Mr.Wallace

      Hey all!

       

      I'm creating a real basic Flash shooter game where objects/enemies are flying by at the top of the screen and a soldier is at the bottom of the screen trying to shoot them down. I've got some code from a sample game but I need to change one thing.

       

      The first line of code (onMouseDown...), I would like to change it to when the spacebar is pushed it will shoot the bullet (interceptor), not on the mouse press. I've tried a few different things with "if" statements and "keypress" stuff but no luck. Here is that part of the code:

       

      -----------------------

      onMouseDown = function () {
           this.attachMovie('interceptor', 'interceptor'+totalobjects, this.getNextHighestDepth());
           eval('interceptor'+totalobjects)._x = soldier._x;
           eval('interceptor'+totalobjects)._y = soldier._y-20;
           eval('interceptor'+totalobjects).xvel = 13*(_xmouse-soldier._x)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
           eval('interceptor'+totalobjects).yvel = 13*(_ymouse-soldier._y)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
           eval('interceptor'+totalobjects).onEnterFrame = function() {
           this._x += this.xvel;
           this._y += this.yvel;
           enemyCollision(this._name);
           if (this._x<0 || this._x>Stage.width || this._y<0 || this._y>Stage.height) {
         removeMovieClip(this);
        }
      };
      totalobjects++;
      };

      -----------------------

       

      I'm sure I'm making this harder than it has to be. Any ideas?

        • 1. Re: Game play controls...
          kglad Adobe Community Professional & MVP

          var tl:MovieClip = this;

           

          var lo:Object={};

           

          lo.onKeyDown=function() {
              if(Key.isDown(Key.SPACE)){

              var mc:MovieClip = tl.attachMovie('interceptor','interceptor'+totalobjects,this.getNextHighestDepth());
              mc._x = soldier._x;
              mc._y = soldier._y-20;
              mc.xvel = 13*(_xmouse-soldier._x)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
              mc.yvel = 13*(_ymouse-soldier._y)/(Math.abs(_xmouse-soldier._x)+Math.abs(_ymouse-soldier._y));
              mc.onEnterFrame = function() {
                  this._x += this.xvel;
                  this._y += this.yvel;
                  enemyCollision(this._name);
                  if (this._x<0 || this._x>Stage.width || this._y<0 || this._y>Stage.height) {
                      removeMovieClip(this);
                  }
              };
              totalobjects++;
                                      }
          };

           

          Key.addListener(lo);

          1 person found this helpful
          • 2. Re: Game play controls...
            Mr.Wallace Level 1

            It's telling me that this line is creating an error:

             

                if(Key.isDown(Key.SPACE)){

             

            ERROR: Operator '=' must be followed by an opperand

            • 3. Re: Game play controls...
              kglad Adobe Community Professional & MVP

              this should be on one line with no space between the t and h in Depth:

               

                var mc:MovieClip = tl.attachMovie('interceptor','interceptor'+totalobjects,this.getNextHighestDepth());

              1 person found this helpful
              • 4. Re: Game play controls...
                Mr.Wallace Level 1

                Oh, I see the problem...there is a space between the 't' and the 'h' in 'getNextHighestDept h());'.

                 

                So, your code does work, he shoots when you press SPACE, but (there is always a but, LOL)...

                 

                Now my movie jumps to the ending as if the user has lost the game (it reacts the same way it would when the soldier gets killed).