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");
        }
      }
    }

    Nothing new, we just added an ENTER_FRAME event, but we needed it in order ...

Get Box2D for Flash Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.