Tuesday, October 19, 2010

Create a shooting game using AS3: Part 6

In this tutorial we will add an explosion animation on enemy death.

First of all, create an explosion animation in a movie clip. Set its class path to Explosion. Don't make the animation too long though, around 15 frames is enough. But it's all up to you.

Now, the only thing you'll need to edit to add this feature is your EnemyManager.as class.

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 _bulletmanager;
private var speeds:Array = [5,7,10];
private var explosionArray:Array = [];

public function EnemyManager(get_stage:Stage, manager)
{
trace("Enemy manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
_stage = get_stage;
_bulletmanager = manager;
}
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;
enemyArray[u].mc.alpha = 1;
}

for (var g:int=0; g<_bulletmanager.bulletArray.length; g++)
{
if (_bulletmanager.bulletArray[g].alpha == 1 && enemyArray[u].mc.alpha == 1 && _bulletmanager.bulletArray[g].hitTestObject(enemyArray[u].mc))
{
enemyArray[u].mc.alpha = 0;
_bulletmanager.bulletArray[g].alpha = 0;
var anExplosion:MovieClip = new Explosion();
_stage.addChild(anExplosion);
anExplosion.x = enemyArray[u].mc.x;
anExplosion.y = enemyArray[u].mc.y;
explosionArray.push(anExplosion);
}
}

}

for (var l:int=0; l<explosionArray.length; l++)
{
if (explosionArray[l].currentFrame == 15)
{
_stage.removeChild(explosionArray[l]);
explosionArray.splice(l,1);
}

}
}
}
}

As you can see, I created a new Array to store all the explosions in - explosionArray. In the loop place where enemy gets destroyed (alpha set to 0), I create new instance of the Explosion object, add it to the stage and set its coordinates. I also push this object to the Array.

I have an extra loop, which is checking all explosionArray elements. If their animation ends (15th frame for me), remove the child from stage and also remove it from the Array.

Results:


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 4
Create a shooting game using AS3: Part 5
Create a shooting game using AS3: Part 7
Create a shooting game using AS3: Part 8

No comments:

Post a Comment