Part 2 of the tutorial on how to create a game in Flash, in this part I’ll teach you how to make the spaceship shoot bullets. Let’s get started!
If you haven’t done so yet, read part 1 of the tutorial to learn how to create a spaceship and make it move. If you’ve done that, read on.
Step 1. Drawing the bullets
Start off by drawing a bullet or laser, whichever you wish. Make sure it’s facing up though! You can also use mine by downloading the source below.
![]()
If you’ve done that, select the bullet and hit F8. In the new window tick MovieClip and press OK. Now delete the MovieClip. Yes, this may sound strange but don’t worry – the MovieClip is actually still existing in your Flash document.
Hit CTRL + L to open the Library which shows you the elements in your document, the bullet should be right there!
Step 2. Linking the bullet
In the Library, select the bullet and right click it. In the menu select “Linkage…” (or “Properties…” if you’re using Adobe Flash CS4 or up).

In the new window tick “Export for ActionScript” which unlocks the ability to give the MovieClip an identifier. Insert “bullet”, now hit “OK” and close the library.

Step 3. Coding
Select the Spaceship MovieClip and hit F9 to open the code panel, then replace the whole code (code from part 1 should be there) with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | onClipEvent(load){ timer = 0; i = 0; } onClipEvent(enterFrame){ if(timer > 0){ timer--; } if(Key.isDown(Key.LEFT)){ _x -= 6; }else if(Key.isDown(Key.RIGHT)){ _x += 6; } if(Key.isDown(Key.SPACE)){ if(timer == 0){ _root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); _root["bullet"+i]._x = _x; _root["bullet"+i]._y = _y-20; _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } } i++; timer = 20; } } } |
Oh God, that looks like a lot of code to explain! Indeed true, but don’t worry – it’s all rather simple
1 | onClipEvent(load){ |
This tells the code that the following actions should be done when the MovieClip loads, which is a one time event.
1 2 | timer = 0; i = 0; |
Creates and sets 2 variables (timer and i) to 0 for later use.
1 | onClipEvent(enterFrame){ |
As learned in the first tutorial, the following actions are going to be repeated.
1 | if(timer > 0){ |
If the timer variable is higher than 0…
1 | timer--; |
Subtract 1 (– is a different method to write -= 1) from the timer variable.
1 2 3 4 5 | if(Key.isDown(Key.LEFT)){ _x -= 6; }else if(Key.isDown(Key.RIGHT)){ _x += 6; } |
This code is already explained in the first tutorial, it makes the spaceship move left and right.
1 | if(Key.isDown(Key.SPACE)){ |
If the spacebar is held down…
1 | if(timer == 0){ |
And if the timer variable is exactly 0.
1 | _root.attachMovie("bullet", "bullet"+i, _root.getNextHighestDepth()); |
Then attach a MovieClip from the library (our bullet) to the stage, give it the name “bullet”+i (ie. bullet0) and make sure the depth is a unique number, namely the next highest depth on the stage. This is for sorting which object is on the front and which on the back.
1 | _root["bullet"+i]._x = _x; |
Then, locate the newly created bullet on the stage and change its X position to the X position where the code is executed – which is the spaceship.
1 | _root["bullet"+i]._y = _y-20; |
Also make sure the Y position of the bullet is the same as the spaceship’s but subtract 20 pixels to make sure the bullet spawns in the front of the spaceship instead of the middle.
1 | _root["bullet"+i].onEnterFrame = function(){ |
The following code should repeatedly be executed ON the bullet, so essentially we’re creating code for the bullet.
1 | this._y -= 10; |
Constatly subtract 10 pixels from the Y position of the bullet, to make it fly up.
1 | if(this._y < -30){ |
But if the current position of the bullet is lower than -30 (which is off the stage)…
1 | this.removeMovieClip(); |
Then remove the MovieClip, as it has left the visible game area.
1 2 | i++;
timer = 20; |
Then add 1 to the i variable (to make sure the next bullet that’s going to be created has an unique name) and set the timer to 20. This makes sure you can’t shoot bullets in a stream but there are little pauses between shootings.
Step 4. Testing and finishing
Press CTRL + ENTER to test the game, you should be able to shoot bullets using the spacebar!
If you had any problems feel free to ask questions below, if you want a sample download the source file here:
In the next tutorial we’re going to make enemies to shoot at!

Home
Games
Portfolio
Tutorials
Mobile
Contact









Amazing, in a few steps u have a spaceship shooting bullets-
Its the easiest and best tutorial i´ve ever seen