Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Flah mx Space Invaders Problem

Options
  • 03-03-2005 8:04pm
    #1
    Closed Accounts Posts: 4


    Doing a project in flash at the minute in Space Invaders but don't really know too much about flash mx. The problem I have is that when i shoot the attackers from my ship they're not blowing up and I don't know how the code to do this .My code at the minute for shooting is
    onClipEvent(load){

    moveSpeed=10;

    }
    onClipEvent (enterFrame) {

    if (Key.isDown(Key.RIGHT)) {
    this._x+=moveSpeed;
    } else if (Key.isDown(Key.LEFT)) {
    this._x-=moveSpeed;
    }
    }

    // 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;
    }

    and for the attacker is
    onClipEvent(load){
    this._y =0;
    speed=10;
    }
    onClipEvent(enterFrame){
    if(this._x <0)
    {
    // set direction to right
    speed=10;
    // drop down
    this._y +=30;
    }
    if(this._x > Stage.width)
    {
    // set direction to left
    speed=-10;
    // drop down
    this._y += 30;
    }
    // move horizontal
    this._x += speed;

    }

    If anyone could help me with some of the code I would much appreciate it


Comments

  • Closed Accounts Posts: 7,488 ✭✭✭SantaHoe


    I've never used actionscript (or whatever flash is using nowdays) so I'll see if I can help in pseudocode :)
    I haven't a clue what the flash coordinate system is like, so this could very well make no sense at all... just something I had to do before in C.

    I think you'd want to start with some kind of collision detection for the enemy units... a reasonable way of doing this would be to take the x/y position of an enemy unit and from that define a sort of box around it... you'd do this by taking the size of the sprite used and figuring out what size would make sense for a hitbox.
    For example, say the sprite for an enemy is 20x20 pixels and the unit is on the screen at 200x 100y and the x/y coordinate describes its center... you'd define the hitbox by something like this:

    IF (bullet_x_position > (enemy_x_position - 10)) AND (bullet_x_position < (enemy_x_position +10)) AND (bullet_y_position > (enemy_y_position - 10)) AND (bullet_y_position < (enemy_y_position + 10))
    THEN killenemy();

    You're basically checking the position of the bullet against the position of the enemy (with a plus or minus range a few pixels in each direction because the sprite doesn't just cover one point in space, it spans a few in each direction) ... if all of the conditions are true, then the bullet is definitly in the same x/y position as somewhere on the sprite and a hit should be registered.
    And then you can adopt this for many enemies and many bullets by sticking in whatever arrays of x/y positions that you want and using the one function to check them all every tick.

    Again, I don't know if this makes any sense in a Flash context, but I hope it's at least food for thought if nothing else.


  • Registered Users Posts: 2,647 ✭✭✭impr0v


    There's a tutorial on creating a space invaders type game, including collision detection, at http://www.video-animation.com/flash_12.shtml .

    I'd say it suits your purposes, since the code is so similar to your own, uncannily similar in fact.


Advertisement