Friday, July 25, 2008

[AS3] Gravity

Gravity is far simpler than it appears in AS3. However you have to understand a few key points:
  • As the object moves up on the stage, the Y coordinate decreases.
  • Therefore, if the y coordinate increases, it will be moving down.
With this in mind we first need to declare a few variables.
var yvel:Number = 10; /*how much the y coordinate will be increasing by when the frame is entered */
var gravity:Number = 1; //how much the yvel is going to be increased by
Now for our code:
import flash.events.* ;
stage.addEventListener(Event.ENTER_FRAME, enterFrameEventHandler); // stage event listener
//our function
function enterFrameEventHandler(e:Event):void{
my_mc.y += yvel; //makes the movie clip move
yvel += gravity; //gravity comes into play
}

No comments: