Tutorial: Make a Vertical Shooter in AS3 – Part 5


Written By MrSun at 8:04 am - Saturday, August 23rd, 2008
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 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?

The Final Product:

Source Files (zipped)

Read More...

Tutorial: Make a Vertical Shooter in AS3 – Part 4


Written By MrSun at 8:03 am - Saturday, August 23rd, 2008
Categories: Flash

Table of Contents Programming the Character Programming the Character – Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 4: Programming the Enemies Now we have to make it so the enemies can be shot. This probably will be the most complicated of the steps. But, it also is what makes this […]

Read More...

Tutorial: Make a Vertical Shooter in AS3 – Part 3


Written By MrSun at 8:02 am - Saturday, August 23rd, 2008
Categories: Flash

Table of Contents Programming the Character Programming the Character – Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 3: Creating the Enemies Well, now that we can make our lil’ guy shoot, we have to make something for him to shoot at! I’m going to first start by just drawing a […]

Read More...

Tutorial: Make a Vertical Shooter in AS3 – Part 2


Written By MrSun at 8:01 am - Saturday, August 23rd, 2008
Categories: Flash

Table of Contents Programming the Character Programming the Character – Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 2: Programming the Character Pt 2 – Making it Shoot Well, now that we’ve got the character moving, we have to make him able to shoot. The first step in doing this is […]

Read More...

Tutorial: Make a Vertical Shooter in AS3


Written By MrSun at 8:00 am - Saturday, August 23rd, 2008
Categories: Flash

Table of Contents Programming the Character Programming the Character – Part 2 Creating the Enemies Programming the Enemies Scoring Finishing Touches Step 1: Programming the Character Today, we’re going to make a classic vertical shooter game in ActionScript 3. I hope you learn from it! Let us begin. The first thing that I’m going to […]

Read More...

The Laws of Effective Simplicity


Written By MrSun at 8:00 am - Friday, August 22nd, 2008
Categories: Uncategorized

Law 1: Be Clear and Focused. Law 2: Leave an Impression

Read More...

Tutorial: Pagination in ActionScript 3


Written By MrSun at 8:00 am - Thursday, August 21st, 2008
Categories: Beginner Tutorials, Flash

This is going to be another tutorial about pagination. This time, however, it will be done in ActionScript 3.0. Here’s a brief description of what this tutorial will be about, taken from my previous tutorial in ActionScript 2.0: This is going to be a smaller tutorial about how to paginate objects in ActionScript 2.0. By […]

Read More...

15 Tips on Improving your Flash Games


Written By MrSun at 8:00 am - Wednesday, August 20th, 2008
Categories: Flash

Here’s just a compilation of a bunch of the posts I made about flash game development in the past. Hopefully, some of these will be new to you and will help you. When to Use Storylines in Flash Games 34 Tips on Getting your Flash Game Sponsored Efficient Flash Game Development 50 Ways to Make […]

Read More...

Why You Should Keep All of Your Works


Written By MrSun at 8:00 am - Tuesday, August 19th, 2008
Categories: Misc

If you create anything, from works of art, to flash games or books, you should know that your work will accumulate over time. These works should all be precious to you, no matter the age or quality. You would never throw out a diamond, just as you should never throw away your work. Pretty cheesy […]

Read More...

40 Reasons Why I LOVE WordPress


Written By MrSun at 8:00 am - Monday, August 18th, 2008
Categories: Uncategorized

It’s free It’s easy to use Thousands (maybe millions) of people use it It’s well organized It has great documentation There are thousands of was to extend it It’s customizable (if you know what you’re doing) It lets me express myself It’s hip It’s fly It keeps me appetized I can schedule posts Everything supports […]

Read More...