Tutorial: Make a Vertical Shooter in AS2 – Part 5


Written By MrSun at 8:05 am - Saturday, January 17th, 2009
Categories: Flash

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 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” and, in smaller text, “Press Space to Restart”.

Then, in the actions, type in the following code:

stop();//stops the game so nothing goes wrong

_root.onEnterFrame = function(){ //create a function to check for keystrokes
	if(Key.isDown(32)){//if the space key is down...
		gotoAndStop(1); //then go back to the first frame, restarting the game
	}
}

Now, I’ve noticed a bit of a problem in the main code. For some reason, the game thinks that you get hit whenever you shoot an enemy. I’ve found a way around this strange behavior, however. First of all, add this variable to the top of the code:

//showing the amount of enemies that have been added to the stage
var enemyTotal:Number = 0;

Next, find the code where we have to create the enemy and add it to the stage. Change it to this:

_root.attachMovie('mcEnemy', 'en'+enemyTotal,_root.getNextHighestDepth());//then add the enemy
//setting it's coordinates
_root['en'+enemyTotal]._x = int(Math.random()*Stage.width);//randomly within the boundaries
_root['en'+enemyTotal]._y = -50; //sets this offstage at first
_root['en'+enemyTotal].onEnterFrame = function(){//then give it some functions
	this._y += 5;
	
	//run a loop checking if it's touching any bullets
	for(var cBullet:String in _root.bulletHolder){
		//if it's touching the bullet
		//we have to use coordinates because hit testing doesn't seem to work
		if(this._y >= _root.bulletHolder[cBullet]._y-30 && this._y <= _root.bulletHolder[cBullet]._y){
			if(this._x <= _root.bulletHolder[cBullet]._x+5 && this._x >= _root.bulletHolder[cBullet]._x -35){
				//then destroy this guy
				this.removeMovieClip();
				//and destroy the bullet
				_root.bulletHolder[cBullet].removeMovieClip();
			}
		}
	}
	
	//hit testing with the user
	if(this.hitTest(_root.mcMain)){
		//we'll add code here in the next part
		//but for now, we'll just trace something
		trace('You just got hit!');
	}
}
enemyTime = 0;//reset the time
enemyTotal ++; //add 1 more to the amount of enemies total

I’m sorry about this delay, but we need this to continue on with our game.

Now, we have to create a new variable at the top that will say whether or not the game has been ended:

stop();//add a stop function too!

var gameOver:Boolean = false;

Then, add this code to both the mcBullet‘s and mcEnemy‘s onEnterFrame functions:

//checking if the game is over
if(gameOver){
	//destroy this guy if the game is over
	this.removeMovieClip();
}

Now, we can make this Boolean false when the user gets touched by the enemy. Find where we hit test for mcMain and replace that trace statement with:

//hit testing with the user
if(this.hitTest(_root.mcMain)){
	//set the game to be over and go to lose screen
	gameOver = true;
	gotoAndStop('lose');
}

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:Number = 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 onEnterFrame function.

//updating the score text field
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?

The Final Product:

Source .fla File

2 Comments

k4ssu:

ive done alot of ur tutorials.. and when im done with them none of them works…. either this one the mcMain is just staying still and no enemies coming… wtf?! am i doing wrong?


Greeny:

Great tutorial, keep up the tutorials


«
»