簡體   English   中英

Citrus引擎中的Box2DPhysicsObject異常

[英]Box2DPhysicsObject strangeness in Citrus engine

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

它運行沒有任何錯誤。

每當我初始化對象(Hero,Platform等)時,Citrus 3.1.6都會遇到相同的問題。

我懷疑這是正確的解決方案,因為它正在修改柑桔類核心代碼,但這似乎有所幫助。

如果您下載citrus的源代碼版本並將其添加到src /中(而不是使用swc),則可以按以下方式修改citrus.physics.box2d.Box2DContactListener.s:

分別更改BeginContact(),EndContact(),PreSolve(),PostSolve(),例如:

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

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

這將阻止類拋出錯誤。 我很想知道為什么會這樣!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM