Main Content

You are here:

Arcade Game: Bullets

Last time we made a game; you could move to avoid the falling stars This is what we will make (use the left and right arrow keys to move; avoid the stars + space to fire!):

It is a little rough around the edges, I know, but it's the best you can do with simple ActionScript. I think. Now, working from last time:

That wasn't so much, was it? Now we can start on the ActionScript. Let's think through the firing process. First comes the "trigger", which is triggered in the player Movie Clip, for simplicity's sake. Add at the bottom:

on(keyPress "") {
        duplicateMovieClip(this._parent.bulletfirst, "bullet"+_root.int1, _root.getNextHighestDepth());
}

You don't have to change much to interpret that:

on(space is pressed down) { duplicate a new bullet }

That creates the bullet. We hit a problem soon, and I will attempt to explain it before it happens: you cannot remove the first/head Movie Clip, from which you duplicate others. My personal response to this is to have a different name for the first one than they will be generated to - the first mine is called "mine", but all the others have numbers on the end; the first bullet is "bulletfirst", all the others are "bullet1" etc. This can be used to keep the first one which cannot be removed out of play, off stageThe _name property carries the Movie Clip's name. The bullet's ActionScript should comprise of two parts like this:

onClipEvent (load) {
        if(this._name != "bulletfirst"){
                this._x = _root.player._x;
                this._y = 310.1
        }
}

If the name of the bullet is not the first, then move to the location of the player, and to just above it (you might want to change that one), ready to be lauched:

onClipEvent (enterFrame) {
        if(this._name != "bulletfirst"){
                this._y -= 10;
                if (this._y < - 20){
                        this.removeMovieClip();
                }
        }
}

Again, if it not the first bullet, then move upwards (-10 per frame), but once it get off stage at the top (-20 should be enough), remove it. And so we continue with our gun story: it has been fired, but now it needs to hit something - in this case a snow flake. So go onto the snow flake and change "this._y += 3;" to:

if(this._name != "mine"){
        this._y += 3;
}

So the first one doesn't move, otherwise it would be invincible when hit, as it could not be removed. The first major syntax/language comes now - a for loop. You can learn about how they work in another tutorial. Getting back to the game, make your for loop (and put it within the onClipEvent(enterFrame) section):

 for (var i = 0; i<=_root.int1; i++) {
                if (this.hitTest(_root["bullet"+i])) {
                        this.removeMovieClip(); 
                }
}

The hittest now takes a hittest of all the bullets bullet1, bullet2 and such, using the [ ] square brackets to say that the text coming up is actually the name of an object we want to refer to. So if, while looping through hittesting all the bullets, it gets a hit, then it removes itself. We could have put a name check on here, but it is probably unneccesary. And so that raps up our game. It works! I think - I really cannot be bothered to try it out again myself, so use the contact form if there are any problems. Thanks again,

Harry.

Comments