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.


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:

function enemyBullets(enemy)
  //name bullets
  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)
      if (this.hitTest(_root.hero))

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.

So, how are we going to use it? Like this:

shoot = setInterval(_root.enemyBullets, interval, this);

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!

If you want to download the .fla, click here. If you have any questions, please email me at

Name: Richard Nias aka Crashlanding
Location: South-east London, UK
Age: 14
Flash experience: Had Flash for 1 1/4 years now. I learnt from the help files before finding gotoandplay and flashkit, getting me intrested in games.
Job: None
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| | | |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048