Cocos2d-x 3.2 大富翁遊戲項目開發-第三部分 菜單情境,cocos2d-x項目開發

來源:互聯網
上載者:User

Cocos2d-x 3.2 大富翁遊戲項目開發-第三部分 菜單情境,cocos2d-x項目開發


上一部分介紹到標題動畫介紹後進入到菜單情境,情境效果




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);//建立controlButtonsingleGameBtn->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);//設定TagaddChild(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,如果關,顯示offif(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;}

代碼比較簡單  


未完待續...............


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.