Thursday, October 21, 2010

Create a shooting game using AS3: Part 8

Today we will add another HUD element - misses.

If an enemy doesn't get killed and reaches the end - you get a miss.


I created another TextField and another variable in my HudDisplay.as, as well as an addMiss element. I also set this thing's depth to second (score is the first):

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 playerMisses:Number = 0;
public var showScore:TextField = new TextField();
public var showMisses: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);
_stage.addChild(showMisses);
showMisses.x = 10;
showMisses.y = 40;
showMisses.width = 50;
showMisses.height = 24;
showMisses.selectable = false;
showMisses.text = "0";
showMisses.background = true;
showMisses.backgroundColor = 0xFF6464;
showMisses.addEventListener(Event.ENTER_FRAME, depthChecker);
}

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

public function addMiss(amount:Number):void
{
playerMisses +=  amount;
showMisses.text = playerMisses.toString();
}

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

In the enemyManager.as, I add an if() statement in the place where the enemy leaves the screen, which checks if the ship is visible. If it is, then it's a miss.

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)
{
if (enemyArray[u].mc.alpha == 1)
{
_hud.addMiss(1);
}
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);
}

}

}
}
}

And we have a simple little game. Of course, it's not the game of the year, but it is something you can do with classes in AS3. These tutorial series were an example of using OOP to create games.

Thank for reading! I hope you've learnt something useful.

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 7

20 comments:

Anonymous said...

is there a place to get the source files?

Anonymous said...

how to create statment win loss game?:P

Kirill Poletaev said...

You could simply tell flash to gotoAndStop(); to a win frame if your score is (for example) 10, and go to a lose frame if your hit count is 3 or something.

I don't think I'll upload the source files, sorry. If you follow all the tutorials well and do it step by step - you'd end up with the results same as mine.

Anonymous said...

your tutorial is damn good...nice share...it help me alot with my project....
really nice bro..hahaha

Anonymous said...

actually if you follow it step by step you still have no idea how u set up your enemy mc. and when i followed your code exactly and figured out how you did the enemy mc's, mine only spawns 2 out of the 3 enemies and it only spawns the first one once.I dont know why its doing this but any input would be great, thanks. and other then that one part missing this is a great tutorial!

Anonymous said...

Just so you know, only certain speeds work. is it supposed to be that way?

Kirill Poletaev said...

The creation of enemies is explained in the 3rd and 4th tutorial parts. I don't know why does it only spawn 2 enemies out of 3...

I'm not sure what speeds do you mean, everything works on my end...

The example on this page uses the exact same code as I published... so it has to work.

Mike said...

really good tutorial helped me a lot setting how fire bullets at different rates.

I have one question and i know its not much use for this game, but how would you go about firing in different directions? instead of straight ahead.

Anonymous said...

Thanks a lot, it helped very much...

Hans said...

Thanks for your tutorial. I have a question, how to make an explosion if the enemy hit the ship?

Kirill Poletaev said...

It is explained in part 6 of the tutorial.

Anonymous said...

which page is hit point?where the bullet meeets the ship?i need help with my project realy bad.....

Anonymous said...

if you are there please answer me

Kirill Poletaev said...

Destroying enemies is covered in part 5...

karthi said...

Can you help that when enemy an player touches each other collision has to be happened and Score should increase

Sven Roegiers said...

is there a way to add a start frame with a button that leads to the game.
The same thing for an exitbutton in the game that leads to the startframe again.
Is it possible to add a pause button too?

Kirill Poletaev said...

Yes, of course, everything is possible.

There are numerous ways of managing game start and end. One solution would be to turn the whole game into one class that extends a MovieClip, and then just put this MovieClip on frame 2 of your flash file. On frame 1, add some kind of menu with a button that directs the player to frame 2, where the game is located. And you can do it the other way around - add a button on frame 2 that takes you back to main menu on frame 1.

This is all, of course, achievable with just code too. In that case you'll need to manually display and hide elements of the game based on which state the player is in (menu, game, credits, etc).

Pause button is basically a button that toggles a Boolean value. In the code, check if the boolean is true, and only execute movement of objects if it is true.

Hope this helped.

Justice1 said...

is there a way to make it like 10 misses and it's game over or 40 kills and you win the game??
i've been trying to make the code for that but im really a newbie with as3...

could you help me with this??

Anonymous said...

OK so I've searched forever for finding a working hittest/remove child method and yours works :D
Enemies[I].parent.removechild(enemies[i]);
Enemies.splice(i,1);
This worked perfectly I'll be saving this website for future reference, thanks a bunch!!!

Cedric said...

Very nice tutorial

Post a Comment