Monday, October 18, 2010

Create a shooting game using AS3: Part 5

In this tutorial we destroy our enemies.

Here's what we will get:


Here's the edited EnemyManager.as I have and its explanation below:

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];

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;
}
}
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
}
}

}
}
}
}

First of all, you can see that there is a new variable - _bulletmanager. This is a link for my BulletManager object (which is, if you remember, defined in the Ship.as). The _bulletmanager variable gets its value (the link to the object) through the construction method - as the second parameter. So, now, when we're creating an instance of EnemyManager class in Ship.as, we will need to feed it the path to BulletManager too:

public var myManager:BulletManager = new BulletManager();
public var myEnemyManager:EnemyManager = new EnemyManager(stage, myManager);

Then you can see that I edited the main loop heavily. First of all, I will explain how to 'killing' of enemies work: when the bullet and enemy collide - both go transparent and don't interract with anything now - the bullet, once it goes off screen is removed, and the enemy gets respawned (now visible).

So I added another loop, which is called _bulletmanager.bulletArray.length times per each enemy. It checks, whether any of the elements of bulletArray hits this enemy. They both also have to be fully visible. If they collide, their visibility is set to 0. Simple!

Here's the slightly edited Ship.as. I only changed the interval a little and the definition of EnemyManager object.

package cl.kirill
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.*;

public class Ship extends MovieClip
{
private var mouseIsDown:Boolean = false;
public var myManager:BulletManager = new BulletManager();
public var myEnemyManager:EnemyManager = new EnemyManager(stage, myManager);
private var bulletInerval = setInterval(createBullet, 200);

public function Ship()
{
trace("Ship added to stage!");
stage.addEventListener(Event.ENTER_FRAME, moveShip);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
myEnemyManager.spawnEnemies(4, 1);
myEnemyManager.spawnEnemies(2, 2);
myEnemyManager.spawnEnemies(2, 3);
}
private function moveShip(Event):void
{
this.x+=(stage.mouseX-this.x)/5;
}
private function mDown(MouseEvent):void
{
mouseIsDown = true;
}
private function mUp(MouseEvent):void
{
mouseIsDown = false;
}
private function createBullet():void
{
if (mouseIsDown)
{
var aBullet:Bullet = new Bullet();
stage.addChild(aBullet);
aBullet.x = this.x;
aBullet.y = this.y;
myManager.bulletArray.push(aBullet);
}
}
}
}

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

No comments:

Post a Comment