2012-04-22 152 views
0

我剛開始使用FlashPunk,並且在Mac上使用Flash Builder作爲我的IDE。爲什麼doDebug()給我這個錯誤?

我用Box2D和box2fp設置了FlashPunk,我一直在試圖構建一個非常簡單的遊戲。但我的問題是,當我運行doDebug()我收到以下錯誤:

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at net.box2fp::Box2DWorld/debug_draw()[/Users/samirghobril/Desktop/MusicGame/src/net/box2fp/Box2DWorld.as:103] 
at net.box2fp::Box2DWorld/doDebug()[/Users/samirghobril/Desktop/MusicGame/src/net/box2fp/Box2DWorld.as:123] 
at Level1()[/Users/samirghobril/Desktop/MusicGame/src/Level1.as:13] 
at Main()[/Users/samirghobril/Desktop/MusicGame/src/Main.as:18] 

這裏的Main.as:

package 
{ 

    import net.box2fp.Box2DWorld; 
    import net.flashpunk.Engine; 
    import net.flashpunk.FP; 
    import net.flashpunk.Screen; 

    [SWF(width='800',height='600',backgroundColor='#ffffff',frameRate='60')] 

    public class Main extends Engine 
    { 
     public function Main() 
     { 

      super(800, 600, Box2DWorld.DEFAULT_FRAMERATE, true); 

      FP.world = new Level1; 


     } 

     override public function init():void 
     { 
      FP.console.enable(); 
      FP.world = new Level1; 
     } 
    } 
} 

而這裏的Level1.as:

package 
{ 
    import Box2D.Common.Math.b2Vec2; 

    import net.box2fp.Box2DWorld; 

    public class Level1 extends Box2DWorld 
    { 
     public function Level1() 
     { 
      super(); 
      setGravity(new b2Vec2(0, 10)); 
      doDebug(); 
     } 

     override public function begin():void 
     { 
      add(new Player(80, 600)); 
     } 
    } 
} 

而且這裏是Player.as:

package 
{ 
    import Box2D.Dynamics.b2Body; 

    import net.box2fp.Box2DEntity; 
    import net.box2fp.Box2DShapeBuilder; 

    public class Player extends Box2DEntity 
    { 
     public function Player(x:Number=0, y:Number=0, w:uint=1, h:uint=1, b2Type:int=0) 
     { 
      super(x, y, 30, 60, b2Body.b2_dynamicBody); 
     } 

     override public function buildShapes(friction:Number, density:Number, restitution:Number, group:int, category:int, collmask:int):void 
     { 
      Box2DShapeBuilder.buildRectangle(body, width/(2.0 * box2dworld.scale), height/(2.0 * box2dworld.scale)); 
     } 
    } 
} 

回答

0

如果更新你的代碼如下你的項目應該編譯。

Main.as:

package 
{ 

    import net.box2fp.Box2DWorld; 

    import net.flashpunk.Engine; 
    import net.flashpunk.FP; 
    import net.flashpunk.Screen; 

    [SWF(width='800',height='600',backgroundColor='#ffffff',frameRate='60')] 

    public class Main extends Engine 
    { 
     public function Main() 
     { 
      super(800, 600, Box2DWorld.DEFAULT_FRAMERATE, true); 

      FP.console.enable(); 
      FP.world = new Level1(); 
     } 

     override public function init():void 
     { 
      trace("FlashPunk has initialised"); 
     } 
    } 
} 

Level1.as:

package 
{ 
    import Box2D.Common.Math.b2Vec2; 

    import net.box2fp.Box2DWorld; 

    public class Level1 extends Box2DWorld 
    { 
     public function Level1() 
     { 
      super(); 
      setGravity(new b2Vec2(0, 10)); 
     } 

     override public function begin():void 
     { 
      doDebug(); 
      add(new Player(80, 600)); 
     } 
    } 
} 

說明:doDebug方法嘗試一個Sprite添加到不存在構造時FP.stage實例您的Main類的名稱被調用(並且通過擴展您的Level1類的構造函數來調用該方法)。

如果上述不適合,您可以在Main類的init方法中創建您的世界。當FlashPunk成功初始化時,此方法由超類調用(請參閱this tutorial)。然而,看過文檔後,看起來世界通常是在主類的構造函數中創建的。

此外,雖然合法(我認爲),但在實例化一個類之後,在調用new之後省略括號並不認爲是非常好的做法。

這樣做:

FP.world = new Level1(); 

不是這個:

FP.world = new Level1; 
相關問題