Skip to main content
August 5, 2009
Question

Game play controls...

  • August 5, 2009
  • 1 reply
  • 549 views

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?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 5, 2009

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);

August 5, 2009

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

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

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

kglad
Community Expert
Community Expert
August 5, 2009

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());