Main Content

You are here:

Arcade Game

Welcome to Foundation Flash tutorials once again. So far, we have covered a wide range of topics, so I thought " Why don't we actually make a game, well, why not? I should warn you now, this will be a fairly big project (as these tutorials go) so it will move quite fast. If you don't understand something, we probably already have a tutorial about it. This is what we will make (use the left and right arrow keys to move; avoid the stars):

Right here goes (here comes the preparation work):

Once you have done that, we can start on the ActionScript. First is the player:

onClipEvent(load){
        var framecount = 0;
        var int1 = 0;
}

All that does is initialise two variable: framecount and int1; we shall come to them now:

onClipEvent(enterFrame){
        framecount++;

Self explanatory really. Here comes what could be the first new thing: a basic "timer". We can use it to create a new enemy every 40 frames like this:

     if((framecount / 40) == Math.round(framecount / 40)){
        duplicatemovieclip(_root.mine,"mine" + int1,_root.getNextHighestDepth()) ;
        int1++
}

We have seen the duplicateMovie Clip() before, but the line before it? If you think through what that is saying, you realise the if will only get executed once every 40 frames - 24/40 for instance does not make a whole number. That is the importance of the framecount variable. Next comes the movement section, again what we have seen before:

if (Key.isDown(Key.RIGHT)) {
        if(!this.hitTest(this._parent.right)){
                this._x += 4;
        }
}
if (Key.isDown(Key.LEFT)) {
        if(!this.hitTest(this._parent.left)){
                this._x -= 4;
        }
}
}

The ! mark makes it negative, if NOT hitTest. Hence, if you are not touching the left barrier, you can move left, and the same with the right. Following? If you need refreshing, look back over past tutorials. That is it for the player - the final tailing } is the end of that Movie Clip event. Now, after a short pause, make a quick change to the actions for the frames: add to frame 1:

var notallowedtoduplicate = false;

And to frame 2:

notallowedtoduplicate = true
                                }

All that does is make a variable true in frame 1 and false in frame , so we know where we are. Now we move on to the enemy's coding:

onClipEvent (load) {
        this._y = -50;
        this._x = random(550);
}

Well, when the mine/enemy/falling thingy is created it moves off to the top of the screen, where it cannot be seen. It then moves to a random x location along the top of the screen, ready to fall down like a snow flake:

onClipEvent (enterFrame) {
        this._y += 3;

Every frame, every "snowflake" will move down 3 pixels. Not too hard. Now comes the if statements:

if (this._y > 420){
        this.removemovieclip();
}

"If(I[the "snowflake"] am off the bottom of the screen) then remove me" - so it doesn't clog up memory. Okay it won't take up much, but better nothing than anything. Remeber the frame code that asked whether we were in frame two or not? Here is its use:

if(_root.notallowedtoduplicate==true){
        this.removemovieclip();
}

By the way, the "==true" is unnecessary by default - that is what Flash assumes. Basically that three lines means that if we are in frame 2, destroy all the "snowflakes". Or rather they will all self-destruct. Of course, these bombs falling from the sky need to do something - in this case, it's game over when they touch you:

if (this.hitTest(this._parent.player)) {
        _root.gotoAndStop(2);
}
}

Oh yes, frame 2. I almost forgot, this will be the game over screen. Add some text, "Game Over" or whatever, and a retry button, with the simple ActionScript:

on(press){
        _root.gotoAndStop(1);
}

Back to frame one, to start the game again. Phew! That was one lot of code, but most of it was easy to understand I hope. I also hope that showed you how you can string different things together to make more complex games. See you again some time.

By the way, if your site loads your flash games slowly, you might want to consider getting one of the top cheap web hosting companies available. A good webhost doesn't have to be expensive just make sure you double check on those reviews.

Harry