Merge branch 'v2_addOnEnterAndOnExitToCCNode' of https://github.com/chengstory/cocos2d-x into compJSB

This commit is contained in:
pandamicro 2015-06-04 15:54:58 +08:00
commit 86d372222c
18 changed files with 128 additions and 24 deletions

View File

@ -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

View File

@ -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;

View File

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

View File

@ -1419,6 +1419,11 @@ void Node::onEnter()
if (_onEnterCallback)
_onEnterCallback();
if (_componentContainer && !_componentContainer->isEmpty())
{
_componentContainer->onEnter();
}
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
@ -1498,6 +1503,11 @@ void Node::onExit()
if (_onExitCallback)
_onExitCallback();
if (_componentContainer && !_componentContainer->isEmpty())
{
_componentContainer->onExit();
}
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{

View File

@ -56,6 +56,16 @@ void ComAudio::onExit()
stopAllEffects();
}
void ComAudio::onAdd()
{
}
void ComAudio::onRemove()
{
stopBackgroundMusic(true);
stopAllEffects();
}
bool ComAudio::isEnabled() const
{
return _enabled;

View File

@ -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;

View File

@ -54,6 +54,18 @@ void ComController::onExit()
{
}
void ComController::onAdd()
{
if (_owner != nullptr)
{
_owner->scheduleUpdate();
}
}
void ComController::onRemove()
{
}
void ComController::update(float delta)
{
}

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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()
{
}

View File

@ -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);

View File

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

View File

@ -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);

View File

@ -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<SceneController*>(com)->getProjectiles().pushBack(_owner);
}
void ProjectileController::onExit()
void ProjectileController::onRemove()
{
}

View File

@ -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);

View File

@ -24,16 +24,16 @@ bool SceneController::init()
return true;
}
void SceneController::onEnter()
void SceneController::onAdd()
{
ComController::onEnter();
ComController::onAdd();
_fAddTargetTime = 1.0f;
static_cast<ComAudio*>(_owner->getComponent("Audio"))->playBackgroundMusic("background.wav", true);
static_cast<ComAttribute*>(_owner->getComponent("CCComAttribute"))->setInt("KillCount", 0);
}
void SceneController::onExit()
void SceneController::onRemove()
{
}

View File

@ -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();