Posted on

Jump Algo in Flash

Hi,

I could not fullfill promise that I’ll post daily but I am trying…..

This post is for Flash Developers who are doing game programming.

Open new Flash File, Create one MovieClip and Paste this code on MovieClip (Bad Practice, but focus on what it’s doing)

//————– Copy from here ——————-

//This is settign the initial values onLoad
onClipEvent (load) {

//Speed of the Object
//More Speed will make object go higher in sky....
//This can be mass of the object
startspeed = 10;
//Amount of Gravity, 9.8 is standard Gravity
//Use 0 as gravity and your object will be lost in space
//Will never come down...
//Can be used for animated background, with some random wind algo.
gravity = 9.8;
//Current Status
jumping = false;
//Initial position
startpos = 0;

}
onClipEvent (enterFrame) {

//If user press up key and object is not jumping
//This will make object jump
if ((Key.isDown(87) or Key.isDown(Key.UP)) and jumping == false) {

jumping = true;
starttime = getTimer()/1000;
startpos = this._y;

}else if(Key.isDown(Key.LEFT)){

_x -=5;

}else if(Key.isDown(Key.RIGHT)){

_x +=5;

}
if (jumping) {

time = getTimer()/1000-starttime;
_y -= (startspeed-gravity*time);
if (_y>startpos) {

_y = startpos;
jumping = false;

}

}

}

//————– Stop Copying ——————-

This code is taken from http://www.video-animation.com/flash_06.shtml

About Tushar Vaghela

I am Tushar. I am working as programmer in Glam Media, India. It's located in Mumbai. Basically I am from Gujarat and my qualification is MCA. I am here to share my views on different topics, which is touching my life and I have decided to post mostly on all days of week. Feel Free to post your comments on any of my post and I would love to reply.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s