Tutorial: Make a Vertical Shooter in AS3 – Part 5
Categories: Flash
Table of Contents
Step 5: Scoring
Now that we’ve got the hardest part down, it all gets easier from here. This chapter will be simple, just some code that has scoring. Also, as promised, we’re going to have a function run when the player is hit by an enemy. Let’s start with this one first, eh?
In order to do this, we have to make a frame called “lose”. This will be the frame that we’ll navigate to when we get hit. It’ll be simple, just some text that says “You Lose” and a listener for keystrokes or a click that will return us to play another game. First, make the frame called “lose”. I recommend making an entire new layer for labels, but this isn’t required. In that frame, draw or type in whatever you want to signify that the player has lost the game. I’m just going to put the text, “YOU LOSE”.
Then, in the actions, type in the following code:
stop();
stage.addEventListener(MouseEvent.CLICK, goBack);
function goBack(event:MouseEvent):void{
gotoAndStop(1);
stage.removeEventListener(MouseEvent.CLICK, goBack);
}
Then, we’ll have to add a variable called gameOver to signify that the game is over and that some stuff should be deleted and such.
//whether or not the game is over
var gameOver:Boolean = false;
Then, both in the Bullet‘s and the Enemy‘s eFrame() function, add the following code:
//checking if game is over
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
Finally, add this code to where we hit tested for the main character in “Enemy.as”
//hit testing with the user
if(hitTestObject(_root.mcMain)){
//losing the game
_root.gameOver = true;
_root.gotoAndStop('lose');
}
If you test the game, there will be a bug that comes up. Don’t worry, we can fix it. Just keep mcMain in the “lose” frame. Also, so it can’t be controlled while in the “lose” frame, place the following code:
//keeping mcMain out of sight
mcMain.x = stage.stageWidth;
mcMain.y = stage.stageHeight;
mcMain.removeEventListener(Event.ENTER_FRAME, moveChar);
Phew, that was a lot of work, wasn’t it? Now we can move onto actually scoring the game. First, we have to define a score variable at the top:
//the player's score
var score:int = 0;
Then, increment the score every time an enemy is killed. Place this code in the Enemy’s hit testing for the bullet.
//up the score
_root.score += 5;
Now, we can show the score to the user with a dynamic text field. Make one at the bottom of the stage and give it an instance name of txtScore. Next, place this code into the moveChar function.
//updating the score text
txtScore.text = 'Score: '+score;
Pretty easy, right? Well, that’s all we’re going to do for scoring. Next, we’ll add some sweet finishing touches, eh?
Problems
1: Score does not work.
2. Get this error at game over
“TypeError: Error #1009: Cannot access a property or method of a null object reference.
3. Game over will not occur unless bullet hits mcMain with space bar depressed
at shooter_fla::MainTimeline/moveChar()
I’m sure I am pasting stuff in the wrong places, so I will continue trying
February 17th, 2009 at 3:07 pm
I actually have the same problem.
May 6th, 2009 at 3:19 am
Yeah I have the same problem too.
I think this is just bogus.
May 16th, 2009 at 6:22 pm
Hey,
I solved it!!! you just have to put the if(_root.gameOver) code block at the end of the eFrame function.
ciao Dani
July 30th, 2009 at 4:00 am
Thank you!
August 12th, 2009 at 1:09 am
IT’S OVER 9000!!!!111
October 29th, 2009 at 2:54 pm
WHAT LAYER DO WE CREATE THE SCORE ON? AMD WHAT FRAME?
April 17th, 2010 at 3:53 am