
In this part of the tutorial we’re gonna add enemies to our game to shoot at, this is going to be tricky so be prepared and make sure you read the previous parts first!
If you haven’t read the previous parts first, go ahead and read part 1 and part 2 of the tutorial to learn how to create and move the spaceship and make it shoot.
Step 1. Making the enemy
Like in the first few parts of the tutorial we’re going to draw our object first. So go ahead and draw a nice looking enemy spaceship. Here’s mine:

If you’ve done that, select the enemy and hit F8. In the new window tick MovieClip and press OK. Now go ahead and delete the MovieClip like we did with the bullet. Again, it’s still in the library just not on the stage anymore but that’s okay – we’re going to create the enemies dynamicly.
Hit CTRL + L (on Windows) to open the Library of your project which shows you the elements in your document, the enemy ship should be there!
Step 2. Linking the enemy
In the Library, select the enemy spaceship 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 “enemy”, now hit “OK” and close the library.

Step 3. Coding part 1
This is probably going to be the most advanced and difficult code of the whole project so take your time, read it slowly and try to imagine what every line of code says in normal English.
First, let’s place the following code on the main timeline. What that is? Basicly you click somewhere on the stage (not on any object, so clicking the background is fine) and press F9 to bring up the code panel – then, place your code there. If you then create the Flash file (CTRL + ENTER, remember?) you should see no errors if you did it right.
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 | i = 0; enemies = []; spawnEnemy = function(){ _root.attachMovie("enemy", "enemy"+i, _root.getNextHighestDepth()); _root["enemy"+i]._x = random(Stage.width); _root["enemy"+i]._y = 0; enemies.push("enemy"+i); _root["enemy"+i].onEnterFrame = function(){ this._y += 3; if(this._y > Stage.height){ for(e = 0; e < _root.enemies.length; e++){ if(_root.enemies[e] == this._name){ _root.enemies.splice(e, 1); } } this.removeMovieClip(); } } i++; } enemy_interval = setInterval(spawnEnemy, 2000); |
Alright, the explanation of the above code:
1 2 | i = 0; enemies = []; |
First we make a new variable named i and set it to 0, this is going to be used to make sure our enemies all have an unique name. Then we make a new array (which basicly is a package of variables) to store all the enemy names.
1 | spawnEnemy = function(){ |
Create a new function, functions are blocks of code that can simply be reused by calling the function name (so calling spawnEnemy() would execute all the following code).
1 | _root.attachMovie("enemy", "enemy"+i, _root.getNextHighestDepth()); |
Just like in the part of the tutorial about the bullets we’re going to attach the enemy spaceship from the library to the stage. Give it an unique name (for example, enemy1 or enemy10) and make sure the depth is the next on the stage with _root.getNextHighestDepth().
1 2 | _root["enemy"+i]._x = random(Stage.width); _root["enemy"+i]._y = 0; |
Now set the X position of the newly created enemy to a random position with a maximum of the total width of the stage. Then set the Y position to 0, the top of the screen.
1 | enemies.push("enemy"+i); |
Now add the enemy name (ex. enemy1) to the enemies array we created earlier which should look like this now: enemies = ["enemy1"];.
1 | _root["enemy"+i].onEnterFrame = function(){ |
Now make sure the enemy executes the following code every frame…
1 | this._y += 3; |
Add 3 pixels to the Y position which makes the enemy spaceship move down, towards the player.
1 | if(this._y > Stage.height){ |
But if the Y position is larger than the total height of the stage (so if the enemy has left the stage…)
1 | for(e = 0; e < _root.enemies.length; e++){ |
Do a loop. A loop goes through a number of values and then executes functions like removing an enemy from an array (which we’re about to do!).
1 | if(_root.enemies[e] == this._name){ |
Ah! So if the value in the array is exactly the same (==) as the name of the enemy then remove it. Because, once the enemy has left the stage we have no use for it anymore and we might aswell remove it from the array to save CPU power.
1 | _root.enemies.splice(e, 1); |
Splice removes items from an array, so “e” is the position of the loop and the exact name of the enemy (checked for that above) and 1 means that 1 item is going to be removed. Putting a 2 there would make Flash remove the 2 following items, but we don’t want that now.
1 | this.removeMovieClip(); |
Then we close some open functions and to removeMovieClip which does that it says, removes a MovieClip from the stage (the enemy that has left the stage).
1 | i++; |
Now close some more open functions and add 1 (++ is the same as += 1) to the i value to make sure the next enemy has an unique name (ex. enemy2).
1 | enemy_interval = setInterval(spawnEnemy, 2000); |
Last but not least we make an interval. Intervals execute a function every X seconds (2000 milliseconds in this case). So Flash is going to create a new enemy every 2000 milliseconds, you can lower the number to create more enemies every two seconds.
Step 4. Coding part 2
Okay, now for the next code you have to seek the code for the bullets to be created which is on the player MovieClip. So go ahead and click the player (spaceship) and hit F9 to open the coding panel again.
You can now see this code:
1 2 3 4 5 6 | _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } } |
Replace it with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } for(k = 0; k < _root.enemies.length; k++){ if(this.hitTest(_root[_root.enemies[k]])){ this.removeMovieClip(); _root[_root.enemies[k]].removeMovieClip(); _root.enemies.splice(k, 1); } } } |
Okay, don’t worry about this code being difficult – we learned all functions and methods above, so it’s kind of an exam, see if you’ve learned something
Let’s check what the code means line by line:
1 2 3 4 5 | _root["bullet"+i].onEnterFrame = function(){ this._y -= 10; if(this._y < -30){ this.removeMovieClip(); } |
This is old code so I’m going to skip that, you should already know what this means
1 | for(k = 0; k < _root.enemies.length; k++){ |
Make a new loop and loop through the enemy array like before.
1 | if(this.hitTest(_root[_root.enemies[k]])){ |
If a bullet hits one of the entries in the enemies array (so, if a bullet hits an enemy…)
1 2 | this.removeMovieClip(); _root[_root.enemies[k]].removeMovieClip(); |
Then remove the bullet AND the enemy.
1 | _root.enemies.splice(k, 1); |
And remove the enemy that has been hit from the enemies array.
Step 5. Testing and finishing
Now press CTRL + ENTER to test the game, you should be able to shoot bullets and hit enemies. If you had any problems feel free to ask questions below, if you want a sample download the source file here:
Up next we’re going to add scoring and lives and a game over screen!

Home
Games
Portfolio
Goodies
Contact






hey i like how you did this spaceship
but by the time i got to the end of third part
it is giving me errors
the errors look like this “Statement must appear within on/onClipEvent handler”
so whats going on can you help me out