如果我只是创建一个新的 Box2DPhysicsObject,它会完美运行。
如果我像这样覆盖它的功能:
public class CarObject extends Box2DPhysicsObject
{
private const degreesToRadians:Number=0.0174532925;
private var worldScale:int=30;
private var leftAxleContainerShape:b2PolygonShape;
private var leftAxleContainerFixture:b2FixtureDef;
private var rightAxleContainerShape:b2PolygonShape;
private var rightAxleContainerFixture:b2FixtureDef;
private var carPosX:Number=500;
private var carPosY:Number=300;
private var carWidth:Number=45;
private var carHeight:Number=10;
private var axleContainerDistance:Number=30;
private var axleContainerWidth:Number=5;
private var axleContainerHeight:Number=20;
private var axleContainerDepth:Number=10;
private var axleAngle:Number=20;
private var wheelRadius:Number=25;
public function CarObject(name:String, params:Object=null)
{
super(name, params);
}
override protected function defineBody():void{
//super.defineBody();
_bodyDef = new b2BodyDef();
_bodyDef.position.Set(carPosX/worldScale,carPosY/worldScale);
_bodyDef.type = b2Body.b2_dynamicBody;
}
override protected function createBody():void{
//super.createBody();
_body = _box2D.world.CreateBody(_bodyDef);
}
override protected function createShape():void{
//super.createShape();
_shape = new b2PolygonShape();
b2PolygonShape(_shape).SetAsBox(carWidth/worldScale,carHeight/worldScale);
}
override protected function defineFixture():void{
//super.defineFixture();
_fixtureDef = new b2FixtureDef();
_fixtureDef.density=5;
_fixtureDef.friction=3;
_fixtureDef.restitution=0.3;
_fixtureDef.filter.groupIndex=-1;
_fixtureDef.shape = _shape;
}
override protected function createFixture():void{
//super.createFixture();
_body.CreateFixture(_fixtureDef);
}
override public function handleBeginContact(contact:b2Contact):void{
super.handleBeginContact(contact);
trace("1");
}
override public function handleEndContact(contact:b2Contact):void{
super.handleEndContact(contact);
trace("3");
}
override public function handlePostSolve(contact:b2Contact, impulse:b2ContactImpulse):void{
super.handlePostSolve(contact, impulse);
trace("2");
}
override public function handlePreSolve(contact:b2Contact, oldManifold:b2Manifold):void{
super.handlePreSolve(contact, oldManifold);
trace("3");
}
}
它总是告诉我这个错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at citrus.physics.box2d::Box2DContactListener/PreSolve()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\src\citrus\physics\box2d\Box2DContactListener.as:29]
at Box2D.Dynamics.Contacts::b2Contact/http://www.box2d.org/ns/b2internal::Update()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\srclib\Box2D\Dynamics\Contacts\b2Contact.as:330]
at Box2D.Dynamics::b2ContactManager/Collide()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\srclib\Box2D\Dynamics\b2ContactManager.as:265]
at Box2D.Dynamics::b2World/Step()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\srclib\Box2D\Dynamics\b2World.as:575]
at citrus.physics.box2d::Box2D/update()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\src\citrus\physics\box2d\Box2D.as:112]
at citrus.core::State/update()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\src\citrus\core\State.as:109]
at citrus.core::CitrusEngine/handleEnterFrame()[C:\Users\Aymeric\Workspace\Flash\Librairies\Citrus-Engine\src\citrus\core\CitrusEngine.as:256]
如果我向内部世界添加一个新的 ContactListener:
public function CarObject(name:String, params:Object=null)
{
super(name, params);
_box2D.world.SetContactListener(new MyContactListener());
}
它运行没有任何错误。