该篇主要实现四个菜单button回调功能,切换到相应场景
关于页面
代码:
SnakeSceneManager类管理全部场景切换,仅仅须要实例一个对象调用。关于场景切换:
void SnakeSceneManager::aboutlayer() { CCScene *aboutscene = CCScene::create(); AboutLayer *AL = AboutLayer::create(); aboutscene->addChild(AL); CCDirector::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.5f, aboutscene)); }
进入AboutLayer类。初始化该场景
//图层背景 CCSize size = CCDirector::sharedDirector()->getVisibleSize(); CCSprite *sp1 = CCSprite::create("bckgr.png"); sp1->setAnchorPoint(ccp(0.5f, 0.5f)); sp1->setPosition(ccp(size.width/2, size.height/2)); this->addChild(sp1); //文本 CCLabelTTF *label = CCLabelTTF::create("\t Snake is a classic mobile phone game, \n\ simple and resistance to play.By \n\ controlling the direction of the \n\ snake eating the eggs that snake \n\ longer, to obtain the integral.", "Helvetica", 20, ccp(480, 320), kCCTextAlignmentLeft); //label->setAnchorPoint(ccp(1.0f, 1.0f)); label->setPosition(ccp(size.width/2+100,size.height/2-80)); this->addChild(label); //返回主菜单button CCMenuItemImage *image1 = CCMenuItemImage::create("menuHome.png", "menuHome2.png", this, menu_selector(AboutLayer::toback)); image1->setPosition(ccp(size.width/2, 50)); image1->setScale(0.5f); CCMenu *menu = CCMenu::create(image1, NULL); menu->setPosition(CCPointZero); this->addChild(menu);
切换到主菜单页面
void AboutLayer::toback(CCObject *Obj) { ssm->MenuScene(); }
设置页面
代码:
切换该场景
void SnakeSceneManager::setlayer() { CCScene *setLayer = CCScene::create(); SetLayer *SL = SetLayer::create();//设置图层 setLayer->addChild(SL); CCDirector::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.5f, setLayer)); }
进入SetLayer类,初始化该场景
//设置图层背景 CCSize size = CCDirector::sharedDirector()->getVisibleSize(); CCSprite *spr1 = CCSprite::create("bckgr.png"); spr1->setPosition(ccp(size.width/2, size.height/2)); this->addChild(spr1); //音乐 CCLabelTTF *labe = CCLabelTTF::create("SetMusic:", "Helvetica", 20, ccp(100, 20),kCCTextAlignmentLeft) ; labe->setPosition(ccp(size.width/2-50, size.height/2+50)); this->addChild(labe); //音乐设置button image1 = CCMenuItemImage::create("sound-on-A.png", "sound-off-A.png", this, menu_selector(SetLayer::tosetmusic)); image1->setScale(0.5f); image1->setPosition(ccp(size.width/2+30, size.height/2+50)); MUISC = false; CCMenu *menu = CCMenu::create(image1, NULL); menu->setPosition(CCPointZero); this->addChild(menu); //上一步 CCLabelTTF *labe2 = CCLabelTTF::create("Back:", "Helvetica", 20, ccp(100,20), kCCTextAlignmentLeft); labe2->setPosition(ccp(size.width/2-50, size.height/2)); this->addChild(labe2); //上一步button image3 = CCMenuItemImage::create("backA.png", "backB.png", this, menu_selector(SetLayer::toback)); image3->setPosition(ccp(size.width/2+30, size.height/2)); image3->setScale(0.5f); CCMenu *menu2 = CCMenu::create(image3, NULL); menu2->setPosition(CCPointZero); this->addChild(menu2);设置背景音乐,开关音乐图标的变化
void SetLayer:: tosetmusic(CCObject *pObj) { if (!MUISC)//禁音 ,off图标显示 { image1->setNormalImage(CCSprite::create("sound-off-A.png")); image1->setDisabledImage(CCSprite::create("sound-on-A.png")); CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(); MUISC = true; CCLOG("stop..."); } else //开启, on图标显示 { image1->setNormalImage(CCSprite::create("sound-on-A.png")); image1->setDisabledImage(CCSprite::create("sound-off-A.png")); CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background.mp3", true); MUISC = false; CCLOG("start..."); } }上一步切换至主菜单页面
void SetLayer::toback(CCObject *pObj) { ssm->MenuScene(); }