First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
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.98 Votes: 41
(10 being the highest)
» 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!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.