From ce1c207aeed4525e1f5608531425d582f629efb9 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 8 Sep 2017 09:28:57 +0800 Subject: [PATCH] add LayerMultiplex::switch(int ,bool) (#18224) --- cocos/2d/CCLayer.cpp | 14 +++++-- cocos/2d/CCLayer.h | 3 ++ tests/cpp-tests/Classes/MenuTest/MenuTest.cpp | 40 +++++++++---------- tests/cpp-tests/Classes/MenuTest/MenuTest.h | 8 ++-- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/cocos/2d/CCLayer.cpp b/cocos/2d/CCLayer.cpp index 07ce2e1468..7795565b24 100644 --- a/cocos/2d/CCLayer.cpp +++ b/cocos/2d/CCLayer.cpp @@ -1253,13 +1253,19 @@ bool LayerMultiplex::initWithArray(const Vector& arrayOfLayers) } void LayerMultiplex::switchTo(int n) +{ + + switchTo(n, true); +} + +void LayerMultiplex::switchTo(int n, bool cleanup) { CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" ); - - this->removeChild(_layers.at(_enabledLayer), true); - + + this->removeChild(_layers.at(_enabledLayer), cleanup); + _enabledLayer = n; - + this->addChild(_layers.at(n)); } diff --git a/cocos/2d/CCLayer.h b/cocos/2d/CCLayer.h index 861fcc9527..b48343dfa7 100644 --- a/cocos/2d/CCLayer.h +++ b/cocos/2d/CCLayer.h @@ -727,6 +727,9 @@ public: * @param n The layer indexed by n will display. */ void switchTo(int n); + /** The same as switchTo(int), but has a parameter to set if need to clean up child. + */ + void switchTo(int n, bool cleanup); /** release the current layer and switches to another layer indexed by n. The current (old) layer will be removed from it's parent with 'cleanup=true'. * diff --git a/tests/cpp-tests/Classes/MenuTest/MenuTest.cpp b/tests/cpp-tests/Classes/MenuTest/MenuTest.cpp index e5417a4097..97527d307f 100644 --- a/tests/cpp-tests/Classes/MenuTest/MenuTest.cpp +++ b/tests/cpp-tests/Classes/MenuTest/MenuTest.cpp @@ -58,13 +58,12 @@ MenuLayerMainMenu::MenuLayerMainMenu() { _touchListener = EventListenerTouchOneByOne::create(); _touchListener->setSwallowTouches(true); - _touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); - _touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); - _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); - _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); - + _touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::touchBegan, this); + _touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::touchMoved, this); + _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::touchEnded, this); + _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::touchCancelled, this); _eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1); - + // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); auto spriteSelected = Sprite::create(s_MenuItem, Rect(0,23*1,115,23)); @@ -136,25 +135,26 @@ MenuLayerMainMenu::MenuLayerMainMenu() _disabledItem->setEnabled( false ); addChild(menu); + menu->setTag(100); menu->setPosition(Vec2(s.width/2, s.height/2)); menu->setScale(0); menu->runAction(ScaleTo::create(1,1)); } -bool MenuLayerMainMenu::onTouchBegan(Touch *touch, Event * event) +bool MenuLayerMainMenu::touchBegan(Touch *touch, Event * event) { return true; } -void MenuLayerMainMenu::onTouchEnded(Touch *touch, Event * event) +void MenuLayerMainMenu::touchEnded(Touch *touch, Event * event) { } -void MenuLayerMainMenu::onTouchCancelled(Touch *touch, Event * event) +void MenuLayerMainMenu::touchCancelled(Touch *touch, Event * event) { } -void MenuLayerMainMenu::onTouchMoved(Touch *touch, Event * event) +void MenuLayerMainMenu::touchMoved(Touch *touch, Event * event) { } @@ -166,12 +166,12 @@ MenuLayerMainMenu::~MenuLayerMainMenu() void MenuLayerMainMenu::menuCallback(Ref* sender) { - static_cast(_parent)->switchTo(1); + static_cast(_parent)->switchTo(1, false); } void MenuLayerMainMenu::menuCallbackConfig(Ref* sender) { - static_cast(_parent)->switchTo(3); + static_cast(_parent)->switchTo(3, false); } void MenuLayerMainMenu::allowTouches(float dt) @@ -191,12 +191,12 @@ void MenuLayerMainMenu::menuCallbackDisabled(Ref* sender) void MenuLayerMainMenu::menuCallback2(Ref* sender) { - static_cast(_parent)->switchTo(2); + static_cast(_parent)->switchTo(2, false); } void MenuLayerMainMenu::menuCallbackBugsTest(Ref *pSender) { - static_cast(_parent)->switchTo(4); + static_cast(_parent)->switchTo(4, false); } void MenuLayerMainMenu::onQuit(Ref* sender) @@ -207,7 +207,7 @@ void MenuLayerMainMenu::onQuit(Ref* sender) void MenuLayerMainMenu::menuMovingCallback(Ref *pSender) { - static_cast(_parent)->switchTo(5); + static_cast(_parent)->switchTo(5, false); } //------------------------------------------------------------------ @@ -296,7 +296,7 @@ void MenuLayer2::alignMenusV() void MenuLayer2::menuCallback(Ref* sender) { - static_cast(_parent)->switchTo(0); + static_cast(_parent)->switchTo(0, false); } void MenuLayer2::menuCallbackOpacity(Ref* sender) @@ -336,7 +336,7 @@ MenuLayer3::MenuLayer3() _disabledItem->stopAllActions(); }); auto item2 = MenuItemFont::create("--- Go Back ---", [&](Ref *sender) { - static_cast(_parent)->switchTo(0); + static_cast(_parent)->switchTo(0, false); }); auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); @@ -470,7 +470,7 @@ void MenuLayer4::menuCallback(Ref* sender) void MenuLayer4::backCallback(Ref* sender) { - static_cast(_parent)->switchTo(0); + static_cast(_parent)->switchTo(0, false); } // BugsTest @@ -508,7 +508,7 @@ void BugsTest::issue1410v2MenuCallback(cocos2d::Ref *pSender) void BugsTest::backMenuCallback(cocos2d::Ref *pSender) { - static_cast(_parent)->switchTo(0); + static_cast(_parent)->switchTo(0, false); } RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() @@ -543,7 +543,7 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() void RemoveMenuItemWhenMove::goBack(Ref *pSender) { - static_cast(_parent)->switchTo(0); + static_cast(_parent)->switchTo(0, false); } RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove() diff --git a/tests/cpp-tests/Classes/MenuTest/MenuTest.h b/tests/cpp-tests/Classes/MenuTest/MenuTest.h index 323ccabc3c..9d3aed6310 100644 --- a/tests/cpp-tests/Classes/MenuTest/MenuTest.h +++ b/tests/cpp-tests/Classes/MenuTest/MenuTest.h @@ -50,10 +50,10 @@ public: ~MenuLayerMainMenu(); public: - bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); - void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); - void onTouchCancelled(cocos2d::Touch* touch, cocos2d::Event* event); - void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); + bool touchBegan(cocos2d::Touch* touch, cocos2d::Event* event); + void touchEnded(cocos2d::Touch* touch, cocos2d::Event* event); + void touchCancelled(cocos2d::Touch* touch, cocos2d::Event* event); + void touchMoved(cocos2d::Touch* touch, cocos2d::Event* event); void allowTouches(float dt); void menuCallback(cocos2d::Ref* sender);