我刚开始使用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));
}
}
}
答案 0 :(得分: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;