Tutorial: Create a Platform Game in AS2 – Part 7
Categories: Flash
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 onEnterFrame() 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 wins, where we create the actions for the goal. Change it to this:
if(this.hitTest(_root.mcMain)){
//go to the next lvl
lastScore = _root.mainScore;
_root.lvlCurrent ++;
_root.createLvl();
_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. Do it also again within the resetLvl() function
Next, add this code to the end of the createLvl() function:
//creating random background particles
//we'll create 1 particle for every block
lvlHolder.bgHolder.createEmptyMovieClip("bg"+i, lvlHolder.bgHolder.getNextHighestDepth());
var bgX:Number = int(Math.random()*lvlColumns*50)-550; //the x value of this shape
var bgY:Number = (row-1)*25; //the y value of this shape
lvlHolder.bgHolder["bg"+i].beginFill(0x222222); //this just determines the shape's color
lvlHolder.bgHolder["bg"+i].moveTo(bgX, bgY); //moves the shape
//creating 4 random points to make a random shape
lvlHolder.bgHolder["bg"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));
lvlHolder.bgHolder["bg"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));
lvlHolder.bgHolder["bg"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));
lvlHolder.bgHolder["bg"+i].lineTo(bgX+int(Math.random()*25), bgY+int(Math.random()*25));
lvlHolder.bgHolder["bg"+i].endFill();//finishes up the shape
Next, we have to make them move slower than the game background. You can do this in the onEnterFrame function. Find this code:
if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down
lvlHolder._x += mainSpeed;//then the move the guy left
} else if(Key.isDown(39) || Key.isDown(68)){//if the "D" key or Right Arrow Key is Down
lvlHolder._x -= mainSpeed; //then move the guy to the right
}
and replace it with this:
if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down
lvlHolder._x += mainSpeed;//then the move the guy left
lvlHolder.bgHolder._x -= mainSpeed*.5;
} else if(Key.isDown(39) || Key.isDown(68)){//if the "D" key or Right Arrow Key is Down
lvlHolder._x -= mainSpeed; //then move the guy to the right
lvlHolder.bgHolder._x += mainSpeed*.5;
}
This just slows down the movement of the background. Well, that’s basically all that I’m going to do for the finishing touches. Enjoy your new game!
… ok yeah for most of the tutorial i got lost….. mostly becuz i couldnt find the area you kept on telling me to change….. it should be alot more organized….. or u could at least tell where all the coding is at the end (copyable) cuz this is ridiculous…. im still trying to find the part im supposed to change for jumping so my char doesnt float in midair… and when i do change it there is like 6 errors…
February 3rd, 2009 at 4:13 am
love this one!
how can i use a pre designed layout instead an array which sets the position of the elements?
thanks
February 15th, 2009 at 12:59 pm
wonderful tutorial, but it is not completed… you just make the character hit the goal, but after hitting the goal, can u pls add a tutorial about a button come out or something else to tell the player the game is completed?
thanks
March 14th, 2009 at 7:32 am
good tutorial but how do you get the character to look the way that you wish to move.
May 13th, 2009 at 1:02 pm
@helpplease
You can create additional boolean variable for example named ‘isLeft’ and change it depening on the key hit. Then draw the character depending on this variable.
May 31st, 2009 at 2:03 pm
@Raymond: after hitting the goal, all u need to do is redirect the timeline to a specific frame or scene which contains the message. then just add a button redirecting the player to the next level. common sense is crucial when coding…
June 16th, 2009 at 2:44 am
Hey I have a few questions.
How would I add an extra angled block with collision detecting involved?
Also how can I change the background to a background I’ve created?
How do I change the size of the Array as I get errors when I do this normally.
And how would I redirect the game to a different frame once the game is completed?
Thanks in advance.
June 21st, 2009 at 6:49 am
@ catsup
Can’t be arsed to actually think of how to solve the other problems, but the redirection to different frame when you’ve completed it, if(this.hitTest(_root.mcMain)){
gotoAndStop(the frame number where you want to go)
}
“this” being the final goal, the blue square.
August 6th, 2009 at 11:24 am
i dont get this tutorial at all when i put the 1st code i dont even move wtf help plz
August 20th, 2009 at 5:25 pm
AWESOME!!!
but it laggs like hell when i creat a new lvl^^
stupid arrays 😛
August 24th, 2009 at 12:11 pm
U R A GOD
Thanks a lot
I was searching the whole net for so many days 2 find D correct scripts & here it is
September 7th, 2009 at 3:57 am
omg, this sucks, i didnt even get the the second bit in making it, a the ver first step, u have t put the code in the frame, but then in frame 2, it dosnt say where to put the code, get a life dude
November 11th, 2009 at 6:16 am
final code?
February 4th, 2010 at 6:33 pm
Dude I made this flash and turned it into a mario game
February 9th, 2010 at 11:39 pm
This is really great tutorial! I’m using this as a guide in making a c++ game.
April 2nd, 2010 at 5:39 am
Does anyone know and tutorials where I can make a high score table for this?
May 1st, 2010 at 9:43 pm
where do i copy all of this?
May 30th, 2010 at 3:45 am
@helpplease if you want the guy to face where you make him go put:
if(Key.isDown(keynum)) {
May 30th, 2010 at 9:27 pm
fuck i clicked enter…
anyway:
if(Key.isDown(Key.RIGHT)) {
character._x += speed //stuff like that, then
character._rotation = 270 //if character is default facing down
May 30th, 2010 at 9:30 pm
Hey! Goooooooooood tutorial! But, how can i add sound when i jump? or kill enemy?
June 27th, 2010 at 6:51 am