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



17 comments:
is there a place to get the source files?
how to create statment win loss game?:P
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.
your tutorial is damn good...nice share...it help me alot with my project....
really nice bro..hahaha
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!
Just so you know, only certain speeds work. is it supposed to be that way?
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.
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.
Thanks a lot, it helped very much...
Thanks for your tutorial. I have a question, how to make an explosion if the enemy hit the ship?
It is explained in part 6 of the tutorial.
which page is hit point?where the bullet meeets the ship?i need help with my project realy bad.....
if you are there please answer me
Destroying enemies is covered in part 5...
Can you help that when enemy an player touches each other collision has to be happened and Score should increase
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?
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.
Post a Comment