Wednesday, October 20, 2010

Create a shooting game using AS3: Part 7

Today we will learn how to create a simple score system.

We will need to create a separate class for this - HudDisplay.as, we will use it to display score and manage it. Here's the class:

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

public class HudDisplay
{
public var playerScore:Number = 0;
public var showScore:TextField = new TextField();
private var _stage;

public function HudDisplay(get_stage:Stage)
{
_stage = get_stage;
_stage.addChild(showScore);
showScore.x = showScore.y = 10;
showScore.width = 50;
showScore.height = 24;
showScore.selectable = false;
showScore.text = "0";
showScore.background = true;
showScore.backgroundColor = 0xCCFDFD;
showScore.addEventListener(Event.ENTER_FRAME, depthChecker);
}

public function addScore(amount:Number):void
{
playerScore +=  amount;
showScore.text = playerScore.toString();
}

private function depthChecker(Event):void
{
if (_stage.getChildIndex(showScore)!=(_stage.numChildren-1))
{
_stage.setChildIndex(showScore, _stage.numChildren-1);
}
}
}
}

Things to note: the class holds 2 score-related variables - one to store the information (Number) and one TextField; stage passed to _stage variable through the constructor parameter; the last method sets the hud on top.

Changes made to the Ship.as: created an instance of HudDisplay class (myHud) as well as pass it to the EnemyManager as the third parameter:

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 myHud:HudDisplay = new HudDisplay(stage);
public var myEnemyManager:EnemyManager = new EnemyManager(stage, myManager, myHud);
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);
}
}
}
}

Changes made to the EnemyManager.as: receive the hud parameter and set the _hud variable, addScore() when enemy is destroyed.

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

public function EnemyManager(get_stage:Stage, manager, hud)
{
trace("Enemy manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
_stage = get_stage;
_bulletmanager = manager;
_hud = hud;
}
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);
_hud.addScore(10);
}


}

}

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

}

}
}
}

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

1 comment:

Ghanem Mohammed said...

Hi,
can you add a code for the game for example when the score reach 35 the game display message you won
Thanks

Post a Comment