Search tutorials
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 charGo 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; } }
Lets 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 isnt, 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 wouldnt be very useful controls. Its 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 shouldnt 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 arent 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, Ill add in some things to collect and some walls.
This may seem strange, but it is necessary. Name the movie clip and the instance charGo 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; } }
Lets 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 isnt, 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 wouldnt be very useful controls. Its 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 shouldnt 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 arent 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, Ill 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. |
Download (5 kb) |
» Forums |
More help? Search our boards for quick answers! |