Tutorial: Create a Platform Game in AS3 – Part 7
Categories: AS3, Advanced Tutorials, All Tutorials, Flash, Game Development
Table of Contents
Step 7: Finishing Touches
As I’ve done with most of my previous tutorials, this “Finishing Touches” part of the tutorial will mostly be me telling you what to do, but not how to do it. Of course, if it’s new or advanced material, I’ll definitely give you the code. Let’s get started, shall we?
The first thing we need to do to our game is show the score to the user. Just create a dynamic text field somewhere at the top of the stage and make some code that will update it with the text “Score: 0″. Of course, the 0 would be replaced with whatever the guy’s score is. I suggest placing it somewhere in the moveChar() function so that you don’t have to create a totally new function for it.
The next thing I want to do is reset the score whenever the player dies. This actually will be a little bit tricky because we use the same resetLvl() function both when the player loses and when the player beats a level. This can be accomplished pretty easily. First of all, just set the score to 0 at the end of the resetLvl() function. Now, find the code where we reset the level when the player won. It should be at around line 30 in the “Goal.as” file. Change it to this:
//advancing a level _root.lvlCurrent ++; var lastScore:int = _root.mainScore; _root.resetLvl(); _root.mainScore = lastScore;
This just saves the score that we have before advancing a level and then re-applies that score after it’s reset. Pretty cool, eh?
Next, we’re going to add a background to the game. The background will be a bit darker so we can distinguish it from the game, and it’ll also move slower than the game background to create an illusion that it’s farther away. The first thing we have to do is create another holder for the particles within the level holder. Do it just like how we did it for the bumpers and trampolines. It should be called bgHolder and you should place it before all of the other elements so it’s under everything
Next, add this code to the end of the createLvl() function:
//creating random background particles //we'll create 1 particle for every block var newPart:Shape = new Shape(); newPart.graphics.beginFill(0x222222); newPart.graphics.drawRect(0,0,int(Math.random()*10)+1,int(Math.random()*10)+1); newPart.x = int(Math.random()*lvlColumns*50)-550; newPart.y = (row-1)*25; bgHolder.addChild(newPart);
Next, we have to make them move slower than the game background. You can do this in the moveChar function. Find this code:
//if certain keys are down then move the character if(leftKeyDown){ lvlHolder.x += mainSpeed; } if(rightKeyDown){ lvlHolder.x -= mainSpeed; }
and replace it with this:
//if certain keys are down then move the character if(leftKeyDown){ lvlHolder.x += mainSpeed; bgHolder.x -= mainSpeed*.5; } if(rightKeyDown){ lvlHolder.x -= mainSpeed; bgHolder.x += mainSpeed*.5; }
The final thing we need to do for the background is to reset it’s x value whenever the level is reset. Just set it to x either in the createLvl() function or resetLvl() function, whichever works best for you. I’m going to do mine it the createLvl() function.
Well, that’s basically all that I’m going to do for the finishing touches. Enjoy your new game!





where do i get as3 for free
September 18th, 2008 at 7:40 pm
This is probably the best AS3 game programming tutorial out there. Certainly the best I’ve seen.
September 19th, 2008 at 1:56 am
@hector:
By as3 I think you mean Flash? Flash is the program that lets you develop games in AS3. You can’t get it for free, but there is a free trial available at http://www.adobe.com
@Jason:
Thanks :)
September 19th, 2008 at 6:20 am
I sorta found a glitch in your Demo basically if i press left or right then right click you ball just keeps goin in that direction until you refresh it. Good tutorial
October 5th, 2008 at 1:16 pm
Hi, This tutorial is really thorough. I was wondering if you were planning on adding parts that would help to input art. IE: Appropriate Character Animations playing when I run left or right, perhaps projectiles? I guess I’m looking for calling up the right movie clips, etc. I’m new to AS. Bleh!
If anybody knows of anything leave a coment and I’d really appreciate it!
Thanks!
October 9th, 2008 at 10:48 pm
Mr. Sun,
You rock! Cool website, great tutorials. Thank you.
October 12th, 2008 at 4:13 pm
I really appreciate it and it helped me a lot…very nice tutorials. Would like to know if you’re planning to push it farther up to saving high scores, high scores with user names. Crossing platforms from as3 to sql database, so the users can compete. Would like to ask though about scrolling backgrounds for this game. Thanks!
October 15th, 2008 at 6:52 am
Would also like to know if you have a similar tutorial for Actionscript2 for flash 8. Thanks!
October 15th, 2008 at 6:54 am
Super Tutorials!!
Maybe you could make a tutorial about animation?
For example:
if you walk to left you see legs moving :)
would be cool. :)
October 16th, 2008 at 9:00 am
nice work
October 27th, 2008 at 7:51 am
nice tutorial for the game. Very thankful to you, as it is rare to find on net such a good help.
November 3rd, 2008 at 6:40 am
how can i make my own enemy or my own wall? or own trampoline??? etc.???
can anybody help me?
November 16th, 2008 at 4:33 pm
thanks this tutorial is great! i cant wait to make my own game with this coding and i love how it has scrolling terrain!!!!!!!
November 18th, 2008 at 4:53 pm
wait what as3?!! plz tell me its flash 8 or that i can make a game with this tutorial on flash 8 plz someone answer my question!!!!!!!plz
November 20th, 2008 at 12:38 pm
Onions-
I don’t believe you can program in AS3 with Flash 8, I think you need Flash CS3 or CS4.
November 23rd, 2008 at 12:39 pm
Hey – excellent tutorial. You need Flash CS3 or CS4 to do this, Flash 8 doesn’t come with the req ActionScript 3 code.
To the people asking about animating things, etc…. this tutorial is pretty advanced stuff, I mean it’s programming a game already. If you’re trying to do this, but don’t know how to animate in Flash, you should be starting waaay earlier on the tutorial circuit. There’s a ton out there – learn to animate, then learn basic AS3 coding… then come back to this excellent tutorial.
January 9th, 2009 at 4:47 pm
how do you make walls… i go right throw them!
January 22nd, 2009 at 6:15 pm
hey
nice game! is there a version fpr flash 8 available? that would be awesome!
thanks
February 14th, 2009 at 8:33 pm
I dont know if there is a version for flash 8 (as2) but u can use other tutorials such as http://www.kirupa.com/developer/mx2004/platform_game.htm which I used, Works pretty good
February 19th, 2009 at 5:36 pm
Hi,
Your tutorials are just amazing. This is the best AS3 game programming info I’ve ever seen.
I would like to ask for a tutorial for a Pacman game. If you can produce such tutorial I would be most grateful. Thanks.
February 28th, 2009 at 8:08 pm
Great tutorial which has taught me alot! just wondering, would it be possible to make the ball rotate as the user goes left and right, for example the game Rolando does a similar thing. Thanks,
March 9th, 2009 at 12:28 pm
Are you going to make an extention to the tutorial, that teaches how to make moving platforms and doors, and walls?
March 31st, 2009 at 11:02 am
you can’t get AS3 for free,
its apart of ‘adobe flash CS3 pro’
April 10th, 2009 at 3:58 am