简体   繁体   English

Citrus引擎中的Box2DPhysicsObject异常

[英]Box2DPhysicsObject strangeness in Citrus engine

If I just create a new Box2DPhysicsObject, it runs perfectly. 如果我只是创建一个新的Box2DPhysicsObject,它将完美运行。

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: 如果我向内部世界添加新的ContactListener:

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). 每当我初始化对象(Hero,Platform等)时,Citrus 3.1.6都会遇到相同的问题。

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: 如果您下载citrus的源代码版本并将其添加到src /中(而不是使用swc),则可以按以下方式修改citrus.physics.box2d.Box2DContactListener.s:

Change BeginContact(), EndContact(), PreSolve(), PostSolve() respectively, such as: 分别更改BeginContact(),EndContact(),PreSolve(),PostSolve(),例如:

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! 我很想知道为什么会这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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