2012-05-31 37 views
0

我正在嘗試關注Box2D上的幾個教程,但不知何故,我似乎無法得到應用的重力。下面是我的代碼(我閱讀教程的URL),也許有一些我忽略了?謝謝你的幫助!重力不適用於Box2D for Flash(v2.1a)

package 
{ 
    import Box2D.Collision.Shapes.b2PolygonShape; 
    import Box2D.Common.Math.b2Vec2; 
    import Box2D.Dynamics.b2Body; 
    import Box2D.Dynamics.b2BodyDef; 
    import Box2D.Dynamics.b2DebugDraw; 
    import Box2D.Dynamics.b2FixtureDef; 
    import Box2D.Dynamics.b2World; 

    import org.osflash.signals.natives.NativeSignal; 

    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 

    /* 
    * http://active.tutsplus.com/tutorials/games/introduction-to-box2d-for-flash-and-as3/ 
    * http://blog.allanbishop.com/box2d-2-1a-tutorial-part-1/ 
    * http://plasticsturgeon.com/2010/08/making-an-as3-game-in-box2d-flash-version-2-0-hello-world-box2d/ 
    */ 
    [SWF(backgroundColor="#cccccc", frameRate="30", width="1280", height="720")] 
    public class Floor extends Sprite 
    { 
     private const WIDTH     :uint = 1280; 
     private const HEIGHT     :uint = 720; 
     private const FPS      :uint = 30; 
     private const NUM_PIXELS_PER_METER :uint = 30; 
     private const TIMESTEP    :Number = 0/NUM_PIXELS_PER_METER; 
     private const VELOCITY_ITERATIONS  :uint = 6; 
     private const POSITION_ITERATIONS  :uint = 2; 

     private var _world  :b2World; 
     private var _gravity :b2Vec2; 

     private var _updated :NativeSignal; 


     public function Floor() 
     { 
      init(); 
     } 


     private function init():void 
     { 
      initStage(); 
      initBox2D(); 
      _updated = new NativeSignal ( this, 
              Event.ENTER_FRAME, 
              Event 
             ); 
      _updated.add(onUpdated); 
     } 


     private function initStage():void 
     { 
      stage.frameRate = FPS; 
      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 
     } 


     private function initBox2D():void 
     { 
      _gravity = new b2Vec2(0, 10); 
      _world = new b2World(_gravity, true); 


      var vector:b2Vec2 = new b2Vec2(); 


      // floor 
      vector.x = (WIDTH * 0.5)/NUM_PIXELS_PER_METER; 
      vector.y = (HEIGHT * 0.75)/NUM_PIXELS_PER_METER; 
      var floorBodyDef:b2BodyDef = new b2BodyDef(); 
      floorBodyDef.position.Set(vector.x, vector.y); 

      var floorBody:b2Body = _world.CreateBody(floorBodyDef); 

      vector.x = (WIDTH * 0.5)/NUM_PIXELS_PER_METER; 
      vector.y = 32/NUM_PIXELS_PER_METER; 
      var floorBox:b2PolygonShape = new b2PolygonShape(); 
      floorBox.SetAsBox(vector.x, vector.y); 

      var floorFixture:b2FixtureDef = new b2FixtureDef(); 
      floorFixture.shape = floorBox; 
      floorFixture.density = 1; 
      floorFixture.friction = 1; 

      floorBody.CreateFixture(floorFixture); 


      // box 
      vector.x = (WIDTH * 0.5)/NUM_PIXELS_PER_METER; 
      vector.y = (HEIGHT * 0.125)/NUM_PIXELS_PER_METER; 
      var boxBodyDef:b2BodyDef = new b2BodyDef(); 
      boxBodyDef.type = b2Body.b2_dynamicBody; 
      boxBodyDef.position.Set(vector.x, vector.y); 

      var boxBody:b2Body = _world.CreateBody(boxBodyDef); 

      var box:b2PolygonShape = new b2PolygonShape(); 
      box.SetAsBox(1, 1); 

      var boxFixtureDef:b2FixtureDef = new b2FixtureDef(); 
      boxFixtureDef.shape = box; 
      boxFixtureDef.density = 1; 
      boxFixtureDef.friction = 0.3; 
      boxFixtureDef.restitution = 0.1; 

      boxBody.CreateFixture(boxFixtureDef); 


      // debug 
      var debug:Sprite = new Sprite(); 
      addChild(debug); 
      var debugDraw:b2DebugDraw = new b2DebugDraw(); 
      debugDraw.SetSprite(debug); 
      debugDraw.SetDrawScale(NUM_PIXELS_PER_METER); 
      debugDraw.SetLineThickness(1.0); 
      debugDraw.SetAlpha(1); 
      debugDraw.SetFillAlpha(0.4); 
      debugDraw.SetFlags(b2DebugDraw.e_shapeBit); 
      _world.SetDebugDraw(debugDraw); 
     } 


     private function onUpdated(_:Event):void 
     { 
      _world.Step ( TIMESTEP, 
          VELOCITY_ITERATIONS, 
          POSITION_ITERATIONS 
         ); 
      _world.ClearForces(); 
      _world.DrawDebugData(); 
     } 
    } 
} 

回答

0

對不起,我是一個傻瓜,複製從別的地方TIMESTEP不變的情況下,我得到0的結果......我的壞。