How To Make A Simple Space Shooting Game In Flash Part 3: Creating The Enemy Ship



Go back to How To Make A Simple Space Shooting Game In Flash

1. First is to draw an enemy ship from your favorite image or graphics editor. Or, you can just download an example here. Put transparency on it and save it as Enemyship.png from your image editor.





 2. Next, open the .fla file from Part 2 and drag the Enemyship.png to the Library. If the Library window is not visible yet, use Ctrl+L to show it.




3. Next, drag it from the Library going to the Flash document. Then press F8 to convert it to symbol Movieclip. Name it as Enemy, click the Advance button and on the Linkage identifier, name it as enemy and check the Export for ActionScript. Click OK.


You can now see that an Enemy Movieclip has been added to the Library.
You can now delete the one we've just dragged on the Flash document. It will still be in the Library.

4. Now, click anywhere on the Flash document and press F9. This should bring out the code editor. You should be at the Layer 1: Frame 1 code window. Paste this code in there:
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);

Description:
i = 0;
We will use this for making a unique name for indexing the enemy later.

enemies = [];
Declare an array variable which will hold each enemy we will create.

spawnEnemy = function(){
Declare a function named spawnEnemy. The routine for spawining enemy will be inside his fuinction and will be called repeatedly later.

_root.attachMovie("enemy", "enemy"+i, _root.getNextHighestDepth());
This is similar to the previous chapter where we are adding a new instance of a movieclip from Library to stage which is the enemy movieclip and assigning a unique name by cancatenating the i variable.

_root["enemy"+i]._x = random(Stage.width);
_root["enemy"+i]._y = 0;

These will place the enemy at the top of the stage and at random along horizontal within the Stage's width only.

enemies.push("enemy"+i);
Put the currently created enemy into the array.

_root["enemy"+i].onEnterFrame = function(){
This function we will use to make the code executed everytime.

this._y += 3;
This will move the enemy going down 3 pixels from the y-axis.

if(this._y > Stage.height){
This will check if the enemy has reached the most bottom of the stage.

for(e = 0; e < _root.enemies.length; e++){
Perform a loop to check all enemies we put in the array.

if(_root.enemies[e] == this._name){
Check if this current enemy exists in the array

_root.enemies.splice(e, 1);
If so, then remove it in the array and

this.removeMovieClip();.
remove it to the stage since it has reached the bottom of the stage.

i++;
Update the index for the next enemy creation

enemy_interval = setInterval(spawnEnemy, 2000);
Set a timer to repeatedly call the spawnEnemy() function every 2000 ms or 2 seconds.

Now press Ctrl+Enter to Test your work.

Go back to How To Make A Simple Space Shooting Game In Flash

No comments: