Cocos2d-X development of Chinese chess "12" game results display and hide, cocos2d-x "12"
In the previous section, background music is played in the game. This section displays and hides game results.
Let's take a look.
When a player wins, a dialog box indicating that the player wins is displayed.
When the dialog box is clicked, the dialog box and pawns are hidden.
Click Start to change the color of the player's pawns.
When a player loses, a dialog box indicating that the player loses is displayed.
Implementation of game results:
1. When a player wins, a dialog box is displayed, with the content "Congratulations, you have won" in the dialog box"
2. When a player loses, a dialog box is displayed. The content in the dialog box is "sorry, you lose"
3. When the dialog box is clicked, the dialog box is hidden and the pawns are hidden.
4. After you click Start, you will find that the color of the player's pawns has changed.
Implementation Code:
Add the following code to the member function init () in the SceneGame class to create a game result box.
// Create an genie to display the game result sprite = CCSprite: create ("yingjiemian.png"); sprite1 = CCSprite: create ("shuijiemian.png"); addChild (sprite ); addChild (sprite1); sprite-> setPosition (ccp (winSize. width/2, winSize. height); sprite1-> setPosition (ccp (winSize. width/2, winSize. height); // hide the result sprite-> setVisible (false); sprite1-> setVisible (false); // indicates that the game result visible = false is not displayed;
Create a member function howResult (CCSprite * Sprite, CCSize winSize) in SceneGame to display the game result box.
// Display the game result void SceneGame: ShowResult (CCSprite * Sprite, CCSize winSize) {// display the game result prompt box Sprite-> setVisible (true ); sprite-> setZOrder (1000); visible = true; CCMoveTo * move = CCMoveTo: create (1, ccp (Sprite-> getPositionX (), winSize. height/2); Sprite-> runAction (move );}
Create a member function HideResult (CCSprite * s, CCObject * obj) in SceneGame to hide the game result box.
// Hide the game result void SceneGame: HideResult (CCSprite * s, CCObject * obj) {// obtain the window size CCSize winSize = CCDirector: shareddire () -> getWinSize (); // hide the game result s-> setVisible (false); // set the pawn to the initial position s-> setPosition (ccp (winSize. width/2, winSize. height); visible = false; // change the color of the player's pawns _ redSide =! _ RedSide; // set the red game to go first _ redTrun = true; // click New (obj) again );}
Add the following code to the SceneGame member function void SceneGame: moveComplete (CCNode * movetone, void * _ killid) to kill the red handsome or black ones, show game result dialog box
// If (Stone: JIANG = _ s [killid]-> getType ()) {// if (_ redSide! = _ S [killid]-> getRed () {// display the game result ShowResult (sprite, winSize );} else // when the player's pawn is killed {// display the game result ShowResult (sprite1, winSize );}}
In bool SceneGame: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent), add the following code to hide the result box after you click the game result box to hide the pawns and change the color of the player's pawns.
// When you touch the result box if (sprite-> boundingBox (). containsPoint (ptInWin) & visible = true) {// hide the result HideResult (sprite, obj);} if (sprite1-> boundingBox (). containsPoint (ptInWin) & visible = true) {// hide the result HideResult (sprite1, obj );}
Zookeeper