diff --git a/extensions/physics-nodes/CCPhysicsSprite.cpp b/extensions/physics-nodes/CCPhysicsSprite.cpp index 620b0d2dff..86080ad7aa 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.cpp +++ b/extensions/physics-nodes/CCPhysicsSprite.cpp @@ -21,6 +21,8 @@ */ #include "CCPhysicsSprite.h" +#include "base/CCDirector.h" +#include "base/CCEventDispatcher.h" #if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION) @@ -41,6 +43,7 @@ PhysicsSprite::PhysicsSprite() , _CPBody(nullptr) , _pB2Body(nullptr) , _PTMRatio(0.0f) +, _syncTransform(nullptr) {} PhysicsSprite* PhysicsSprite::create() @@ -393,22 +396,30 @@ void PhysicsSprite::syncPhysicsTransform() const #endif } -// returns the transform matrix according the Chipmunk Body values -const Mat4& PhysicsSprite::getNodeToParentTransform() const +void PhysicsSprite::onEnter() +{ + Node::onEnter(); + _syncTransform = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&PhysicsSprite::afterUpdate, this, std::placeholders::_1)); + _syncTransform->retain(); +} + +void PhysicsSprite::onExit() +{ + if (_syncTransform != nullptr) + { + Director::getInstance()->getEventDispatcher()->removeEventListener(_syncTransform); + _syncTransform->release(); + } + Node::onExit(); +} + +void PhysicsSprite::afterUpdate(EventCustom *event) { syncPhysicsTransform(); - return _transform; -} - -void PhysicsSprite::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) -{ - if (isDirty()) - { - syncPhysicsTransform(); - } - - Sprite::draw(renderer, _transform, flags); + _transformDirty = false; + _transformUpdated = true; + setDirtyRecursively(true); } NS_CC_EXT_END diff --git a/extensions/physics-nodes/CCPhysicsSprite.h b/extensions/physics-nodes/CCPhysicsSprite.h index 6dd88562f9..ae17b824a0 100644 --- a/extensions/physics-nodes/CCPhysicsSprite.h +++ b/extensions/physics-nodes/CCPhysicsSprite.h @@ -26,6 +26,7 @@ #include "2d/CCSprite.h" #include "extensions/ExtensionMacros.h" #include "extensions/ExtensionExport.h" +#include "base/CCEventListenerCustom.h" #if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION) @@ -108,6 +109,7 @@ public: float getPTMRatio() const; void setPTMRatio(float fPTMRatio); + virtual void syncPhysicsTransform() const; // overrides virtual const Vec2& getPosition() const override; @@ -117,13 +119,13 @@ public: virtual void setPosition(const Vec2 &position) override; virtual float getRotation() const override; virtual void setRotation(float fRotation) override; - virtual void syncPhysicsTransform() const; - virtual const Mat4& getNodeToParentTransform() const override; - virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; + virtual void onEnter() override; + virtual void onExit() override; protected: const Vec2& getPosFromPhysics() const; + void afterUpdate(EventCustom *event); protected: bool _ignoreBodyRotation; @@ -134,6 +136,9 @@ protected: // box2d specific b2Body *_pB2Body; float _PTMRatio; + + // Event for update synchronise physic transform + cocos2d::EventListenerCustom* _syncTransform; }; NS_CC_EXT_END