cocos2d-x中提供了CClayerMultiplex类(派生自CClayer),它可以管理多个层,并实现不同层间的切换,但是同一时间只能有一个层是激活状态,其他都是不可见的,这样很方便
了解函数
1.添加要进行管理的图层
static CCLayerMultiplex * create(CCLayer* layer, ... );
2.切换到指定层
void switchTo(unsigned int i);
3.切换到指定层,并释放当前层;
void switchToAndReleaseMe(unsigned int n)
个人见解
这个函数有点就是在做界面的时候很方便用切换场景,在原来直接换层的时候按钮不能使用,现在用了这个函数很方便,缺点就是在切换层得时候没有过度效果,让人看起很差劲,好闲话不多说进入代码时刻
代码时刻
首先我们要创建三个图层
CCLayer* layer1 = CCLayer::create();
layer1->addChild(CCUtil::text("layer1","Arial",20.0f,ccc3(255,255,0),ccp(0.5,0.5),ccp(240,160)));
CCLayer* layer2 = CCLayer::create();
layer2->addChild(CCUtil::text("layer2","Arial",20.0f,ccc3(255,255,0),ccp(0.5,0.5),ccp(240,160)));
layer1->addChild(CCUtil::text("layer1","Arial",20.0f,ccc3(255,255,0),ccp(0.5,0.5),ccp(240,160)));
CCLayer* layer2 = CCLayer::create();
layer2->addChild(CCUtil::text("layer2","Arial",20.0f,ccc3(255,255,0),ccp(0.5,0.5),ccp(240,160)));
CCLayer* layer3 = CCLayer::create();
layer3->addChild(CCUtil::text("layer3","Arial",20.0f,ccc3(255,255,0),ccp(0.5,0.5),ccp(240,160)));
然后是三个按钮
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem->setPosition(ccp(50,100));
pCloseItem->setTag(0);
CCMenuItemImage *pCloseItem1 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem1->setPosition(ccp(100,100));
pCloseItem1->setTag(1);
CCMenuItemImage *pCloseItem2 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem2->setPosition(ccp(150,100));
pCloseItem2->setTag(2);
CCMenu* pMenu = CCMenu::create(pCloseItem,pCloseItem1,pCloseItem2, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem->setPosition(ccp(50,100));
pCloseItem->setTag(0);
CCMenuItemImage *pCloseItem1 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem1->setPosition(ccp(100,100));
pCloseItem1->setTag(1);
CCMenuItemImage *pCloseItem2 = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::releaseEvent));
pCloseItem2->setPosition(ccp(150,100));
pCloseItem2->setTag(2);
CCMenu* pMenu = CCMenu::create(pCloseItem,pCloseItem1,pCloseItem2, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu,1);
接着就是关键的代码
m_fragGroup = CCLayerMultiplex::create(layer1,layer2,layer3,NULL);
addChild(m_fragGroup);
addChild(m_fragGroup);
下面就是我们回调函数
void HelloWorld::releaseEvent(CCObject *pSender)
{
CCMenuItemImage* btn = dynamic_cast<CCMenuItemImage*>(pSender);
int tag = btn->getTag();
CCLog("%d",tag);
chageGroup((int)tag);
}
{
CCMenuItemImage* btn = dynamic_cast<CCMenuItemImage*>(pSender);
int tag = btn->getTag();
CCLog("%d",tag);
chageGroup((int)tag);
}
bool HelloWorld::chageGroup(int group)
{
if(m_fragGroup)
{
m_fragGroup->switchTo(group);
}
return true;
}
{
if(m_fragGroup)
{
m_fragGroup->switchTo(group);
}
return true;
}
好就这样我们的任务完成了;
在很多时候我们进入的时候想他恢复到默认的位置对吧,这个时候我们可以在进入场景的时候也就是
void HelloWorld::onEnter()
{
CCLayer::onEnter();
m_fragGroup->switchTo(0);
}
这样我们的问题就解决啦;