Sunday, October 17, 2010

Create a shooting game using AS3: Part 4

Today we'll add different variations of enemies with different speeds and spawn them randomly.

Here's what we will get by the end of this tutorial:


Here's my EnemyManager.as:

package cl.kirill
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class EnemyManager extends MovieClip
{
public var enemyArray:Array = [];
private var _stage;
private var speeds:Array = [5,7,10];

public function EnemyManager(get_stage:Stage)
{
trace("Enemy manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
_stage = get_stage;
}
public function spawnEnemies(num:Number, enemytype:Number):void
{
for (var i:int=0; i<num; i++)
{
var anEnemy:MovieClip = new Enemy();
anEnemy.gotoAndStop(enemytype);
var enemySpeed:Number = speeds[enemytype - 1];
enemyArray.push({mc: anEnemy, eSpeed:enemySpeed});
_stage.addChild(anEnemy);
anEnemy.x = Math.random() * 400;
anEnemy.y =  -  Math.random() * 400;
trace("Enemy spawned: " + anEnemy.height);
}
}
private function managing(Event):void
{
for (var u:int=0; u<enemyArray.length; u++)
{
enemyArray[u].mc.y +=  enemyArray[u].eSpeed;
if (enemyArray[u].mc.y > 450)
{
enemyArray[u].mc.y =  -  Math.random() * 400;
enemyArray[u].mc.x = Math.random() * 400;
}
}
}
}
}

As you can see, I have made a lot of edits to the spawnEnemies method. First of all, I created a new parameter for the function - enemtype:Number. This is the number which tells the manager what type of enemies to spawn. For example, if I want to spawn 3 enemies of type 1 and 2 enemies of type 2, I will have to call this function twice:

myEnemyManager.spawnEnemies(3,1);
myEnemyManager.spawnEnemies(2,2);

The loop inside spawnEnemies tells the Enemy MovieClip to go on a specific frame. My MovieClip has 3 frames with 3 different graphics for different enemy space ships. So, you need to create 3 frames in your MC and draw different graphics for enemy types.

Later in the loop, I create the enemySpeed variable, which takes the speed from another array (speeds). I create that array in the beginning of me code. Note that I use speeds[enemytype - 1], not speeds[enemytype], because my enemy types are 1, 2, 3 and speeds in my speeds array are 0, 1, 2.

Then I push the movieclip AND the speed value to the array as an object.

You can see that I randomly set the starting position for each enemy using Math.random().

I also edited the managing function - I now refer to the movieclip as enemyArray[u].mc and to speed as enemyArray[u].eSpeed. Oh, and I also set the position of my Enemy to random again, after it goes off screen.

There are changes for Ship.as too, just replace the previous spawnEnemies() function to these:

myEnemyManager.spawnEnemies(4, 1);
myEnemyManager.spawnEnemies(2, 2);
myEnemyManager.spawnEnemies(2, 3);

Now it will spawn 4 enemies of type 1, 2 enemies of type 2, and 2 enemies of type 3.

Thanks for reading!

Related:

Create a shooting game using AS3: Part 1
Create a shooting game using AS3: Part 2
Create a shooting game using AS3: Part 3
Create a shooting game using AS3: Part 5
Create a shooting game using AS3: Part 6
Create a shooting game using AS3: Part 7
Create a shooting game using AS3: Part 8

No comments:

Post a Comment