简体   繁体   中英

Box2DPhysicsObject strangeness in Citrus engine

If I just create a new Box2DPhysicsObject, it runs perfectly.

If I override its functions like this:

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");
        }
    }

It always tells me this error:

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]

And if I add a new ContactListener to the inside world:

public function CarObject(name:String, params:Object=null)
{
    super(name, params);
    _box2D.world.SetContactListener(new MyContactListener());
}

It runs without any errors.

I'm getting the same issue with Citrus 3.1.6 every time I init objects (Hero, Platform, etc).

I doubt this is the correct solution because it's modifying the citrus core code but this seems to help.

If you download the source code version of citrus and add it to src/ (instead of using a swc), you can modify citrus.physics.box2d.Box2DContactListener.as as follows:

Change BeginContact(), EndContact(), PreSolve(), PostSolve() respectively, such as:

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

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

This will stop the class from throwing the error. I would love to know why it happens though!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM