Cocos2d-x 3.2 Monopoly game project development-Part 3 menu scene, cocos2d-x Project Development
In the previous section, after introducing titles animation, go to the menu scenario and set the scenario effect.
The MenuScene. h header file is as follows:
Class MenuScene: public LayerColor {public: static Scene * createScene (); virtual bool init (); CREATE_FUNC (MenuScene); private: Size visibleSize; // LabelTTF * settingsGameTTF; // scene text void menuTouchDown (Object * pSender, Control: EventType event); // Menu click callback method void addBackgroundSprite (); // Add scene Background method void addMenuSprites (); // Add the Menu method}; # endif
Some code of MenuScene. cpp is as follows:
Bool MenuScene: init () {if (! LayerColor: initWithColor (Color4B (255,255,255,255) {return false;} visibleSize = Director: getInstance ()-> getVisibleSize (); Vec2 origin = Director: getInstance () -> getVisibleOrigin (); addBackgroundSprite (); // Add the scene Background method addMenuSprites (); // Add the Menu method return true ;}
Void MenuScene: addBackgroundSprite () {// Add a millionaire background image. 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 ); // Add Sprite * rainBowSprite = Sprite: create (RAINBOW); rainBowSprite-> setPosition (ccp (5, visibleSize. height-20); rainBowSprite-> setAnchorPoint (ccp (0, 1); rainBowSprite-> setScale (0.3f); addChild (rainBowSprite ); // move the left and right sides of the rainbow image to 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: runner () {// Add a single-host game Menu Scale9Sprite * btnNormal = Scale9Sprite: create (NORMAL_MENU); // set the normal Menu image Scale9Sprite * btnPress = Scale9Sprite :: create (PRESS_MENU); // set the menu press image LabelTTF * singleGameTTF = LabelTTF: create (SINGLE_GAME, FONT_MENU, Btn_FontSize ); // create the Label object ControlButton * singleGameBtn = ControlButton: create (singleGameTTF, btnNormal) required for the menu; // create controlButtonsingleGameBtn-> SetBackgroundSpriteForState (btnPress, Control: State: SELECTED); // Add a singleGameBtn-> setPosition (ccp (visibleSize. width/2, visibleSize. height-200); // set the location singleGameBtn-> setPreferredSize (Size (Btn_Width, Btn_Height); // set the Size of singleGameBtn-> locate (this, cccontrol_selector (MenuScene :: menuTouchDown), Control: EventType: TOUCH_DOWN); // set the click callback method singleGameBtn-> setTa G (Btn_Single_Game_TAG); // set TagaddChild (singleGameBtn); // Add menu // other menu addition methods, similar to the same method, no longer repeated. ............................. // The settings menu is not the same. The sound effects are enabled or disabled when you click this menu. Optional * labels = Scale9Sprite: create (NORMAL_MENU); Scale9Sprite * btnPress3 = Scale9Sprite: create (PRESS_MENU); bool music_on = UserDefault: getInstance ()-> getBoolForKey (keys, true); // get the sound setting LabelTTF * settingsGameTTF; // if the sound is enabled, the menu displays on. If off, 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-> cancel (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 (Men UScene: menuTouchDown), Control: EventType: TOUCH_DOWN); // Add setting callback settingsGameBtn-> setTag (Btn_Music_TAG); addChild (settingsGameBtn ); ...........................}
The callback Method for Menu clicking. Call the corresponding method based on the tag of the clicked object. Here, {} brackets need to be added for the case statement. Otherwise, an error is reported during compilation.
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 ); // set the sound off button-> setTitleForState (MUSIC_OFF, Control: State: NORMAL); // make the menu Text show off} else {UserDefault: getInstance () -> setBoolForKey (MUSIC_ON_KEY, true); // set the sound to open the button-> setTitleForState (MUSIC_ON, Control: State: NORMAL ); // display the menu text on} break;} case Btn_Quit_Game_TAG: {log ("quit game"); // popupLayer (); the exit dialog box is displayed, with many codes, break;} default: break ;}
Simple code
To be continued ...............