Developing a space shooter game [ October 07, 2004 ] by Richard Nias aka Crashlanding
In this collection of tutorials, the author explains how to create a basic shooter game, considering all its different aspects: movement, shooting, enemies, sound, etc.
ENEMIES THAT SHOOT
Only a few things are missing from this game. The main thing is the enemies don't shoot. Once this has been added, you can get to work on polishing your game off.
There are no new graphics needed here so we get straight to the coding. First, we need a function for shooting. We could use the old one we used for the hero, but to make things simple, lets make a new one based on the old one:
var newname = "ebullet" + j;
_root.attachMovie("bullet", newname, j*400);
_root[newname]._y = enemy._y + 8.5;
_root[newname]._x = enemy._x;
_root[newname].onEnterFrame = function()
if (paused == false)
var bullet_speed = 9;
this._x -= bullet_speed;
if (this._x < 0)
The main differences are that the bullets are called ebullet, they are set at higher depths, the bullets go the other way and there is a parameter which tells the function which enemy is shooting, so when you call the function you write:
enemyBullets(enemy which is shooting);
Simple! Now its time to call the function. In the reset function, add this variable:
interval = (Math.random() * 1000) + 1000;
In much the same way as the speed and position variables were set, this creates a random number between 1000 and 2000. Now to use this interval variable, we are going to use setInterval, which works basically like this:
var int_name = setInterval(function Name, interval, parameters);
What does it do? Well, if the interval parameter is 5000, then the function and its parameters would be called every 5000 milliseconds, or 5 seconds. One thing you have to remember, never define a setInterval in an onEnterFrame event, because then every say, 30th of a second, Flash would be told that every 5000 milliseconds, starting from now, this function should be called. So what you end up with is Flash calling a function every 5 seconds while telling itself to do the same every 5 seconds... so you end up with the function being called every frame. Not good.
Put this at the end of the reset function. So, using the random interval we created from 1000 to 2000, the enemyBullets function is called. Remember we made the enemy parameter? Well, this refers to the movieClip which the code is placed on, in this case an enemy. However, we need to remove the previous interval every time the enemy resets, because otherwise we would end up with something rather like mentioned in the paragraph above. So, we can use the ingenious clearInterval function at the beginning of reset().
So every time the evil enemy resets... the old interval is removed and a new one replaces it. Brilliant.
Now... wait for it... you've done it! You've completed the space shooter tutorials!