First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Vincent0
Welcome to my tutorial. Make a new Flash document, AS 2, that is 550px by 400px and has a frame rate of 30fps. The first thing we are going to do is make the ground. Make a rectangle, mine is about 400x40. Call the movie clip ‘ground’. Call the instance ‘ground’ as well.The next element you need is a character. Make a character, mine is a 50x50 circle, and put the circle at -25, 0 (or have your x value be half your width).

This may seem strange, but it is necessary. Name the movie clip and the instance ‘char’Go back to your main stage and place both of them on the stage, with the character above the ground. In the main stage, add the following code.

stop(); speed = 8; _root.onEnterFrame = function(){ if(ground.hitTest(char._x, char._y+char._height, true) == false){ char._y += speed; } if(Key.isDown(key.LEFT)){ char._x -= speed; char._xscale = 100; }if(Key.isDown(key.RIGHT)){ char._x += speed; char._xscale = -100; } }

Let’s break this down. Line 1, stop();, just tells the movie not to go past this frame. Line 2 defines speed. Line 3 makes a function. This basically just makes everything in that block of code happen every frame. The next line checks to see whether the bottom of the character is touching the ground. If it isn’t, then it makes the character fall. The next lines check whether any keys are being held down, and if left is, it moves the character left. If the right is held down, it moves right. However, if the character stays facing the same direction, that is no good. Changing the xscale makes it flip around.

Of course, just moving side to side wouldn’t be very useful controls. It’s time to let the character jump. Here is your new code.

stop(); speed = 8; jumping = false; jump = 0; _root.onEnterFrame = function(){ if(jumping == false){ if(ground.hitTest(char._x, char._y+char._height, true) == false){ char._y += speed; }else{ jump = 0; } }if(Key.isDown(key.LEFT)){ char._x -= speed; char._xscale = 100; }if(Key.isDown(key.RIGHT)){ char._x += speed; char._xscale = -100; }if(Key.isDown(key.UP) && jump == 0){ jumping = true; }if(jumping == true){ char._y -= speed; jump++; }if(jump >= 15){ jumping = false; } }

What do these changes do? The first two new lines make some new variables. The third just checks whether the char is jumping, because if he is we know he shouldn’t be falling. Then it checks to see if the char is on the ground, and if he is, it sets jump to zero. This makes sure that in the next section, you can only jump if you are on the ground. If you press up and you aren’t already jumping, it makes jumping true, which subtracts eight from the y value every frame and increases the variable jump. If jump gets too high, (the jump reaches its max height) then it turns jumping to false.

That is it for part one. In the next section, I’ll add in some things to collect and some walls.
» Level Basic

Added: 2010-01-16
Rating: 4.87 Votes: 47
» Author
Vincent0 has been programming in AS 2 for 2 years now.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.