Game Development Reference
In-Depth Information
7.
Time to make a small recap. At the moment, your code should look like
the following:
package {
import flash.display.Sprite;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
public function Main() {
var gravity:b2Vec2=new b2Vec2(0,9.81);
var sleep:Boolean=true;
var world:b2World = new b2World(gravity,sleep);
}
}
}
Now you learned how to create and configure a Box2D World. Let's see how can you
simulate physics in it.
Running the simulation
You need to run the simulation at every frame, so first of all you need a listener to be
triggered at every frame.
1.
Let's make some simple changes to our class:
package {
import flash.display.Sprite;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
public function Main() {
var gravity:b2Vec2=new b2Vec2(0,9.81);
var sleep:Boolean=true;
var world:b2World = new b2World(gravity,sleep);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function updateWorld(e:Event):void {
trace("my awesome simulation runs here");
}
}
}
 
Search WWH ::




Custom Search