Saturday, February 3, 2007

[Flash] Character jumping

Character jumping

Create a new Flash document, set the frame rate to 25fps.

Draw a ball or something and convert it into a movie clip.

Goto main stage and select the ball MC, press F9 and paste in this code:

onClipEvent (load) {
//Where the ball start jumping from
y_start = _y;
//On load, jumping is false
jumping = false;
//And jumpspeed is 0
jumpspeed = 0;
}
onClipEvent (enterFrame) {
//If jumping is true
if (jumping) {
//The _y + jumpspeed = _y
_y += jumpspeed;
//And here is the gravity which makes the ball fall
jumpspeed += 1;
//But if the ball's _Y > than the y_start
if (_y>=y_start) {
// The ball's Y = y_start
_y = y_start;
//And jumping is false
jumping = false;
}
}
//Else
else {
//If the SPACE Key is down
if (Key.isDown(Key.SPACE)) {
//Jumping becomes true
jumping = true;
//And jumpspeed is -14
jumpspeed = -14;
}
}
}

Test your movie (CTRL + Enter) and press Space =)
The codes are explained in the code with comments (//Comment) =P

Enjoy!

2 comments:

Anonymous said...

VERY NICE!!!!

djodin said...

thanks =)