From a51a32fa9d3bf036c3c1727e9a5ca74b6ad70c9e Mon Sep 17 00:00:00 2001 From: zhangcheng Date: Wed, 3 Jun 2015 15:10:48 +0800 Subject: [PATCH 1/3] 1, add onEnter and onExit of components to CCNode's onEnter and onExit function. --- cocos/2d/CCNode.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 423a60286e..ce220e5636 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1414,6 +1414,11 @@ void Node::onEnter() if (_onEnterCallback) _onEnterCallback(); + if (_componentContainer && !_componentContainer->isEmpty()) + { + _componentContainer->onEnter(); + } + #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { @@ -1493,6 +1498,11 @@ void Node::onExit() if (_onExitCallback) _onExitCallback(); + if (_componentContainer && !_componentContainer->isEmpty()) + { + _componentContainer->onExit(); + } + #if CC_ENABLE_SCRIPT_BINDING if (_scriptType == kScriptTypeJavascript) { From 8d3d3a0348d29d6b24147552d7d2af34022debaa Mon Sep 17 00:00:00 2001 From: zhangcheng Date: Thu, 4 Jun 2015 14:06:31 +0800 Subject: [PATCH 2/3] 1, Add onAdd and onRemove function to replace onEnter and onCancel used in onAdd and onRemove of ComponentContainer. --- cocos/2d/CCComponent.cpp | 22 +++++++++++++++++++ cocos/2d/CCComponent.h | 4 ++++ cocos/2d/CCComponentContainer.cpp | 8 +++---- .../editor-support/cocostudio/CCComAudio.cpp | 10 +++++++++ cocos/editor-support/cocostudio/CCComAudio.h | 10 +++++++++ .../cocostudio/CCComController.cpp | 12 ++++++++++ .../cocostudio/CCComController.h | 10 +++++++++ .../editor-support/cocostudio/CCComRender.cpp | 16 ++++++++++++++ cocos/editor-support/cocostudio/CCComRender.h | 10 +++++++++ .../EnemyController.cpp | 6 ++--- .../EnemyController.h | 4 ++-- .../PlayerController.cpp | 6 ++--- .../PlayerController.h | 4 ++-- .../ProjectileController.cpp | 6 ++--- .../ProjectileController.h | 4 ++-- .../SceneController.cpp | 6 ++--- .../SceneController.h | 4 ++-- 17 files changed, 118 insertions(+), 24 deletions(-) diff --git a/cocos/2d/CCComponent.cpp b/cocos/2d/CCComponent.cpp index 2a252db2d0..5b7049e6f7 100644 --- a/cocos/2d/CCComponent.cpp +++ b/cocos/2d/CCComponent.cpp @@ -91,6 +91,28 @@ void Component::onExit() #endif } +void Component::onAdd() +{ +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendComponentEventToJS(this, kComponentOnAdd)) + return; + } +#endif +} + +void Component::onRemove() +{ +#if CC_ENABLE_SCRIPT_BINDING + if (_scriptType == kScriptTypeJavascript) + { + if (sendComponentEventToJS(this, kComponentOnRemove)) + return; + } +#endif +} + void Component::update(float delta) { #if CC_ENABLE_SCRIPT_BINDING diff --git a/cocos/2d/CCComponent.h b/cocos/2d/CCComponent.h index 90d6de8ca3..226d410ef1 100644 --- a/cocos/2d/CCComponent.h +++ b/cocos/2d/CCComponent.h @@ -38,6 +38,8 @@ class Node; enum { kComponentOnEnter, kComponentOnExit, + kComponentOnAdd, + kComponentOnRemove, kComponentOnUpdate }; @@ -58,6 +60,8 @@ public: virtual void onEnter(); virtual void onExit(); + virtual void onAdd(); + virtual void onRemove(); virtual void update(float delta); virtual bool serialize(void* r); virtual bool isEnabled() const; diff --git a/cocos/2d/CCComponentContainer.cpp b/cocos/2d/CCComponentContainer.cpp index 0de3cd8cdc..963e73369d 100644 --- a/cocos/2d/CCComponentContainer.cpp +++ b/cocos/2d/CCComponentContainer.cpp @@ -68,7 +68,7 @@ bool ComponentContainer::add(Component *com) CC_BREAK_IF(component); com->setOwner(_owner); _components->insert(com->getName(), com); - com->onEnter(); + com->onAdd(); ret = true; } while(0); return ret; @@ -85,7 +85,7 @@ bool ComponentContainer::remove(const std::string& name) CC_BREAK_IF(iter == _components->end()); auto com = iter->second; - com->onExit(); + com->onRemove(); com->setOwner(nullptr); _components->erase(iter); @@ -105,7 +105,7 @@ bool ComponentContainer::remove(Component *com) { if (iter->second == com) { - com->onExit(); + com->onRemove(); com->setOwner(nullptr); _components->erase(iter); break; @@ -122,7 +122,7 @@ void ComponentContainer::removeAll() { for (auto iter = _components->begin(); iter != _components->end(); ++iter) { - iter->second->onExit(); + iter->second->onRemove(); iter->second->setOwner(nullptr); } diff --git a/cocos/editor-support/cocostudio/CCComAudio.cpp b/cocos/editor-support/cocostudio/CCComAudio.cpp index 25c334f87a..8e53f5eb88 100644 --- a/cocos/editor-support/cocostudio/CCComAudio.cpp +++ b/cocos/editor-support/cocostudio/CCComAudio.cpp @@ -56,6 +56,16 @@ void ComAudio::onExit() stopAllEffects(); } +void ComAudio::onAdd() +{ +} + +void ComAudio::onRemove() +{ + stopBackgroundMusic(true); + stopAllEffects(); +} + bool ComAudio::isEnabled() const { return _enabled; diff --git a/cocos/editor-support/cocostudio/CCComAudio.h b/cocos/editor-support/cocostudio/CCComAudio.h index d91dbe7bee..a5997aadeb 100644 --- a/cocos/editor-support/cocostudio/CCComAudio.h +++ b/cocos/editor-support/cocostudio/CCComAudio.h @@ -59,6 +59,16 @@ public: * @lua NA */ virtual void onExit() override; + /** + * @js NA + * @lua NA + */ + virtual void onAdd() override; + /** + * @js NA + * @lua NA + */ + virtual void onRemove() override; virtual bool isEnabled() const override; virtual void setEnabled(bool b) override; virtual bool serialize(void* r) override; diff --git a/cocos/editor-support/cocostudio/CCComController.cpp b/cocos/editor-support/cocostudio/CCComController.cpp index a258321ba1..f24b7bebf5 100644 --- a/cocos/editor-support/cocostudio/CCComController.cpp +++ b/cocos/editor-support/cocostudio/CCComController.cpp @@ -54,6 +54,18 @@ void ComController::onExit() { } +void ComController::onAdd() +{ + if (_owner != nullptr) + { + _owner->scheduleUpdate(); + } +} + +void ComController::onRemove() +{ +} + void ComController::update(float delta) { } diff --git a/cocos/editor-support/cocostudio/CCComController.h b/cocos/editor-support/cocostudio/CCComController.h index 8a3ad4f32a..c3dda3fd1e 100644 --- a/cocos/editor-support/cocostudio/CCComController.h +++ b/cocos/editor-support/cocostudio/CCComController.h @@ -59,6 +59,16 @@ public: * @lua NA */ virtual void onExit() override; + /** + * @js NA + * @lua NA + */ + virtual void onAdd() override; + /** + * @js NA + * @lua NA + */ + virtual void onRemove() override; virtual void update(float delta) override; virtual bool isEnabled() const override; virtual void setEnabled(bool b) override; diff --git a/cocos/editor-support/cocostudio/CCComRender.cpp b/cocos/editor-support/cocostudio/CCComRender.cpp index 4243e3aafb..b69e0570d2 100644 --- a/cocos/editor-support/cocostudio/CCComRender.cpp +++ b/cocos/editor-support/cocostudio/CCComRender.cpp @@ -68,6 +68,22 @@ void ComRender::onExit() } } +void ComRender::onAdd() +{ + if (_owner != nullptr) + { + _owner->addChild(_render); + } +} + +void ComRender::onRemove() +{ + if (_owner != nullptr) + { + _owner->removeChild(_render, true); + } +} + cocos2d::Node* ComRender::getNode() { return _render; diff --git a/cocos/editor-support/cocostudio/CCComRender.h b/cocos/editor-support/cocostudio/CCComRender.h index 02b6e3fdd5..2911359bd8 100644 --- a/cocos/editor-support/cocostudio/CCComRender.h +++ b/cocos/editor-support/cocostudio/CCComRender.h @@ -57,6 +57,16 @@ public: * @lua NA */ virtual void onExit() override; + /** + * @js NA + * @lua NA + */ + virtual void onAdd() override; + /** + * @js NA + * @lua NA + */ + virtual void onRemove() override; virtual bool serialize(void* r) override; virtual cocos2d::Node* getNode(); virtual void setNode(cocos2d::Node *node); diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.cpp index f109da7e5e..0f72d1331a 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.cpp @@ -18,9 +18,9 @@ bool EnemyController::init() return true; } -void EnemyController::onEnter() +void EnemyController::onAdd() { - ComController::onEnter(); + ComController::onAdd(); // Determine where to spawn the target along the Y axis Size winSize = Director::getInstance()->getVisibleSize(); float minY = getOwner()->getContentSize().height/2; @@ -52,7 +52,7 @@ void EnemyController::onEnter() _owner->runAction( Sequence::create(actionMove, actionMoveDone, nullptr) ); } -void EnemyController::onExit() +void EnemyController::onRemove() { } diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h index 790f9da45d..542d673cf0 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/EnemyController.h @@ -14,8 +14,8 @@ protected: public: virtual bool init() override; - virtual void onEnter() override; - virtual void onExit() override; + virtual void onAdd() override; + virtual void onRemove() override; virtual void update(float delta) override; static EnemyController* create(void); diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.cpp index bd926530d1..d697052ac7 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.cpp @@ -20,13 +20,13 @@ bool PlayerController::init() return true; } -void PlayerController::onEnter() +void PlayerController::onAdd() { - ComController::onEnter(); + ComController::onAdd(); setTouchEnabled(true); } -void PlayerController::onExit() +void PlayerController::onRemove() { setTouchEnabled(false); } diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h index 7848ff1e08..c24e388b7a 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/PlayerController.h @@ -17,8 +17,8 @@ public: public: virtual bool init() override; - virtual void onEnter() override; - virtual void onExit() override; + virtual void onAdd() override; + virtual void onRemove() override; virtual void update(float delta) override; static PlayerController* create(void); diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.cpp index 29b11b5df7..fd24115998 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.cpp @@ -19,9 +19,9 @@ bool ProjectileController::init() return true; } -void ProjectileController::onEnter() +void ProjectileController::onAdd() { - ComController::onEnter(); + ComController::onAdd(); auto winSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); _owner->setPosition( Vec2(origin.x+20, origin.y+winSize.height/2) ); @@ -30,7 +30,7 @@ void ProjectileController::onEnter() static_cast(com)->getProjectiles().pushBack(_owner); } -void ProjectileController::onExit() +void ProjectileController::onRemove() { } diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h index 55cd5cba78..3d3b76db73 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/ProjectileController.h @@ -13,8 +13,8 @@ protected: public: virtual bool init() override; - virtual void onEnter() override; - virtual void onExit() override; + virtual void onAdd() override; + virtual void onRemove() override; virtual void update(float delta) override; static ProjectileController* create(void); diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp index e7a677109d..835cb99c87 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.cpp @@ -24,16 +24,16 @@ bool SceneController::init() return true; } -void SceneController::onEnter() +void SceneController::onAdd() { - ComController::onEnter(); + ComController::onAdd(); _fAddTargetTime = 1.0f; static_cast(_owner->getComponent("Audio"))->playBackgroundMusic("background.wav", true); static_cast(_owner->getComponent("CCComAttribute"))->setInt("KillCount", 0); } -void SceneController::onExit() +void SceneController::onRemove() { } diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h index a4eda36e4f..770f73ee27 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioComponentsTest/SceneController.h @@ -13,8 +13,8 @@ protected: public: virtual bool init() override; - virtual void onEnter() override; - virtual void onExit() override; + virtual void onAdd() override; + virtual void onRemove() override; virtual void update(float delta) override; static SceneController* create(); From 6a53cfdc764e509a7f517ad736e24ffa0ba6f196 Mon Sep 17 00:00:00 2001 From: zhangcheng Date: Thu, 4 Jun 2015 14:08:47 +0800 Subject: [PATCH 3/3] 1, modify space format. --- cocos/2d/CCComponent.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos/2d/CCComponent.h b/cocos/2d/CCComponent.h index 226d410ef1..a6151f337a 100644 --- a/cocos/2d/CCComponent.h +++ b/cocos/2d/CCComponent.h @@ -38,8 +38,8 @@ class Node; enum { kComponentOnEnter, kComponentOnExit, - kComponentOnAdd, - kComponentOnRemove, + kComponentOnAdd, + kComponentOnRemove, kComponentOnUpdate };