0

如果我只是创建一个新的 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());
}

它运行没有任何错误。

4

1 回答 1

0

每次我初始化对象(英雄、平台等)时,我都会遇到与 Citrus 3.1.6 相同的问题。

我怀疑这是正确的解决方案,因为它正在修改柑橘核心代码,但这似乎有帮助。

如果你下载了citrus的源码版本并添加到src/(而不是使用swc),你可以修改citrus.physics.box2d.Box2DContactListener.如下:

分别更改BeginContact()、EndContact()、PreSolve()、PostSolve(),如:

if( a && b ) { //make sure they exist...
    if (a.beginContactCallEnabled)
        a.handleBeginContact(contact);

    if (b.beginContactCallEnabled)
        b.handleBeginContact(contact);
}

这将阻止类抛出错误。我很想知道为什么会这样!

于 2013-05-12T09:53:04.140 回答