add LayerMultiplex::switch(int ,bool) (#18224)

This commit is contained in:
minggo 2017-09-08 09:28:57 +08:00 committed by GitHub
parent 85456fa1d5
commit ce1c207aee
4 changed files with 37 additions and 28 deletions

View File

@ -1253,10 +1253,16 @@ bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
} }
void LayerMultiplex::switchTo(int n) 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" ); CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
this->removeChild(_layers.at(_enabledLayer), true); this->removeChild(_layers.at(_enabledLayer), cleanup);
_enabledLayer = n; _enabledLayer = n;

View File

@ -727,6 +727,9 @@ public:
* @param n The layer indexed by n will display. * @param n The layer indexed by n will display.
*/ */
void switchTo(int n); 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. /** 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'. The current (old) layer will be removed from it's parent with 'cleanup=true'.
* *

View File

@ -58,11 +58,10 @@ MenuLayerMainMenu::MenuLayerMainMenu()
{ {
_touchListener = EventListenerTouchOneByOne::create(); _touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(true); _touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); _touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::touchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); _touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::touchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::touchEnded, this);
_touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::touchCancelled, this);
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1); _eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1);
// Font Item // Font Item
@ -136,25 +135,26 @@ MenuLayerMainMenu::MenuLayerMainMenu()
_disabledItem->setEnabled( false ); _disabledItem->setEnabled( false );
addChild(menu); addChild(menu);
menu->setTag(100);
menu->setPosition(Vec2(s.width/2, s.height/2)); menu->setPosition(Vec2(s.width/2, s.height/2));
menu->setScale(0); menu->setScale(0);
menu->runAction(ScaleTo::create(1,1)); menu->runAction(ScaleTo::create(1,1));
} }
bool MenuLayerMainMenu::onTouchBegan(Touch *touch, Event * event) bool MenuLayerMainMenu::touchBegan(Touch *touch, Event * event)
{ {
return true; 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) void MenuLayerMainMenu::menuCallback(Ref* sender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(1); static_cast<LayerMultiplex*>(_parent)->switchTo(1, false);
} }
void MenuLayerMainMenu::menuCallbackConfig(Ref* sender) void MenuLayerMainMenu::menuCallbackConfig(Ref* sender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(3); static_cast<LayerMultiplex*>(_parent)->switchTo(3, false);
} }
void MenuLayerMainMenu::allowTouches(float dt) void MenuLayerMainMenu::allowTouches(float dt)
@ -191,12 +191,12 @@ void MenuLayerMainMenu::menuCallbackDisabled(Ref* sender)
void MenuLayerMainMenu::menuCallback2(Ref* sender) void MenuLayerMainMenu::menuCallback2(Ref* sender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(2); static_cast<LayerMultiplex*>(_parent)->switchTo(2, false);
} }
void MenuLayerMainMenu::menuCallbackBugsTest(Ref *pSender) void MenuLayerMainMenu::menuCallbackBugsTest(Ref *pSender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(4); static_cast<LayerMultiplex*>(_parent)->switchTo(4, false);
} }
void MenuLayerMainMenu::onQuit(Ref* sender) void MenuLayerMainMenu::onQuit(Ref* sender)
@ -207,7 +207,7 @@ void MenuLayerMainMenu::onQuit(Ref* sender)
void MenuLayerMainMenu::menuMovingCallback(Ref *pSender) void MenuLayerMainMenu::menuMovingCallback(Ref *pSender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(5); static_cast<LayerMultiplex*>(_parent)->switchTo(5, false);
} }
//------------------------------------------------------------------ //------------------------------------------------------------------
@ -296,7 +296,7 @@ void MenuLayer2::alignMenusV()
void MenuLayer2::menuCallback(Ref* sender) void MenuLayer2::menuCallback(Ref* sender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(0); static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
} }
void MenuLayer2::menuCallbackOpacity(Ref* sender) void MenuLayer2::menuCallbackOpacity(Ref* sender)
@ -336,7 +336,7 @@ MenuLayer3::MenuLayer3()
_disabledItem->stopAllActions(); _disabledItem->stopAllActions();
}); });
auto item2 = MenuItemFont::create("--- Go Back ---", [&](Ref *sender) { auto item2 = MenuItemFont::create("--- Go Back ---", [&](Ref *sender) {
static_cast<LayerMultiplex*>(_parent)->switchTo(0); static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
}); });
auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); 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) void MenuLayer4::backCallback(Ref* sender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(0); static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
} }
// BugsTest // BugsTest
@ -508,7 +508,7 @@ void BugsTest::issue1410v2MenuCallback(cocos2d::Ref *pSender)
void BugsTest::backMenuCallback(cocos2d::Ref *pSender) void BugsTest::backMenuCallback(cocos2d::Ref *pSender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(0); static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
} }
RemoveMenuItemWhenMove::RemoveMenuItemWhenMove() RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
@ -543,7 +543,7 @@ RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
void RemoveMenuItemWhenMove::goBack(Ref *pSender) void RemoveMenuItemWhenMove::goBack(Ref *pSender)
{ {
static_cast<LayerMultiplex*>(_parent)->switchTo(0); static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
} }
RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove() RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove()

View File

@ -50,10 +50,10 @@ public:
~MenuLayerMainMenu(); ~MenuLayerMainMenu();
public: public:
bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); bool touchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); void touchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchCancelled(cocos2d::Touch* touch, cocos2d::Event* event); void touchCancelled(cocos2d::Touch* touch, cocos2d::Event* event);
void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); void touchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
void allowTouches(float dt); void allowTouches(float dt);
void menuCallback(cocos2d::Ref* sender); void menuCallback(cocos2d::Ref* sender);