上一部分介绍到片头动画介绍后进入到菜单场景,场景效果如图
MenuScene.h头文件如下:
class MenuScene : public LayerColor { public: static Scene* createScene(); virtual bool init(); CREATE_FUNC(MenuScene); private: Size visibleSize; //窗口尺寸 LabelTTF* settingsGameTTF; //场景文字 void menuTouchDown (Object* pSender,Control::EventType event);// Menu点击回调方法 void addBackgroundSprite(); //添加场景背景方法 void addMenuSprites();//添加Menu方法 }; #endif
MenuScene.cpp部分代码如下:
bool MenuScene::init() { if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255)) ) { return false; } visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); addBackgroundSprite();//添加场景背景方法 addMenuSprites(); //添加Menu方法 return true; }
void MenuScene::addBackgroundSprite() { //添加大富翁背景图片,居中显示 Sprite* menuSpriteLogo= Sprite::create(MENU_LOGO); menuSpriteLogo->setPosition(ccp(visibleSize.width/2,visibleSize.height)); menuSpriteLogo->setAnchorPoint(ccp(0.5,1)); menuSpriteLogo->setScale(0.6f); addChild(menuSpriteLogo); //添加左边彩虹图片 Sprite* rainBowSprite= Sprite::create(RAINBOW); rainBowSprite->setPosition(ccp(5,visibleSize.height-20)); rainBowSprite->setAnchorPoint(ccp(0,1)); rainBowSprite->setScale(0.3f); addChild(rainBowSprite); //让彩虹图片左右移动 MoveBy* rainBowMove = MoveBy::create(1,ccp(8,0)); MoveBy* rainBowMoveReverse = rainBowMove->reverse(); Sequence* rainBowAction = Sequence::create(rainBowMove,rainBowMoveReverse,NULL); rainBowSprite->runAction(RepeatForever::create(rainBowAction)); }
void MenuScene:: addMenuSprites() { //添加单机游戏Menu Scale9Sprite* btnNormal = Scale9Sprite::create(NORMAL_MENU); //设置菜单normal图片 Scale9Sprite* btnPress = Scale9Sprite::create(PRESS_MENU);// 设置菜单press图片 LabelTTF* singleGameTTF = LabelTTF::create(SINGLE_GAME ,FONT_MENU,Btn_FontSize);//创建菜单所需的Label对象 ControlButton* singleGameBtn = ControlButton::create(singleGameTTF,btnNormal);//创建controlButton singleGameBtn->setBackgroundSpriteForState(btnPress, Control::State::SELECTED);//添加singleButton菜单的press效果图片 singleGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-200));//设置位置 singleGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));//设置大小 singleGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);//设置点击回调方法 singleGameBtn->setTag(Btn_Single_Game_TAG);//设置Tag addChild(singleGameBtn);//添加menu //其他menu添加方法,同上类似,不再重复. ……………………….. //settings菜单不大一样,该菜单点击都有打开或关闭音效。 Scale9Sprite* btnNormal3 = Scale9Sprite::create(NORMAL_MENU); Scale9Sprite* btnPress3 = Scale9Sprite::create(PRESS_MENU); bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);//获取音效设置 LabelTTF* settingsGameTTF; //如果音效开,则menu显示on,如果关,显示off if(music_on) { settingsGameTTF = LabelTTF::create(MUSIC_ON,FONT_MENU,Btn_FontSize); }else { settingsGameTTF = LabelTTF::create(MUSIC_OFF,FONT_MENU,Btn_FontSize); } ControlButton* settingsGameBtn = ControlButton::create(settingsGameTTF,btnNormal3); settingsGameBtn->setBackgroundSpriteForState(btnPress3, Control::State::SELECTED); settingsGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-320)); settingsGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height)); settingsGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene:: menuTouchDown),Control::EventType::TOUCH_DOWN);//添加setting回调 settingsGameBtn->setTag(Btn_Music_TAG); addChild(settingsGameBtn); ……………………… }
Menu点击的回调方法,根据点击对象tag调用相应方法,此处主要的是case语句需要添加 { } 括号,否则编译报错
void MenuScene:: menuTouchDown(Object* pSender,Control::EventType event) { log("single touched"); ControlButton* button = (ControlButton*)pSender; int tag = button->getTag(); switch(tag) { case Btn_Single_Game_TAG: { log("single game"); //Director::getInstance()->pushScene(MapChooseScene::createScene()); } case Btn_Multi_Game_TAG: { log("multi game"); break; } case Btn_Music_TAG: { bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true); if(music_on) { UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,false);//设置音效关 button->setTitleForState(MUSIC_OFF,Control::State::NORMAL);//让menu文字显示off }else { UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,true);//设置音效开 button->setTitleForState(MUSIC_ON,Control::State::NORMAL);// 让menu文字显示on } break; } case Btn_Quit_Game_TAG: { log("quit game"); //popupLayer();弹出退出对话框,代码较多,后续编写 break; } default: break; }
未完待续...............