Game Development Reference
In-Depth Information
6.
You can do it by adding the following line right after the Step method:
world.ClearForces(); Your final code is shown as follows:
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 {
private var world:b2World;
public function Main() {
var gravity:b2Vec2=new b2Vec2(0,9.81);
var sleep:Boolean=true;
world=new b2World(gravity,sleep);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
private function updateWorld(e:Event):void {
var timeStep:Number=1/30;
var velIterations:int=10;
var posIterations:int=10;
world.Step(timeStep,velIterations,posIterations);
world.ClearForces();
}
}
}
Downloading the example code
You can download the example code files for all Packt topics you
have purchased from your account at http://www.packtpub.
com . If you purchased this topic elsewhere, you can visit
http://www.packtpub.com/support and register to have
the files e-mailed directly to you.
And now you are really ready to place some action in your Box2D World.
Summary
You have just learned how to install Box2D for Flash. Include it in your projects and
create a running, gravity-ruled simulation managing time steps and constraint solvers.
You have an empty world ready to be the container where your awesome game will
take place. Save it and use it in every future project!
 
Search WWH ::




Custom Search