上一部分介绍到片头动画介绍后进入到菜单场景,场景效果如图
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)) ) { 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,Btn_FontSize); }else { settingsGameTTF = LabelTTF::create(MUSIC_OFF,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();d出退出对话框,代码较多,后续编写 break; } default: break; }
代码比较简单
未完待续...............
总结以上是内存溢出为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第三部分 菜单场景全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第三部分 菜单场景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)