我刚开始使用 FlashPunk,我在 Mac 上使用 Flash Builder 作为我的 IDE。
我用 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));
}
}
}