Cocos2d-x 3.2-2048-Article 5

Source: Internet
Author: User

Cocos2d-x 3.2-2048-Article 5

 

 

 

I don't know. It's all in October 5,

Holiday is coming to an end

Unhappy !!!

 

Forget it. Send another 2048 message. This is the fifth article,

And soon, the 2048 series is coming to an end.

This article focuses on:

-- Determination of game-related mobile and game termination

-- Add Game Sound Effects

-- Add game scores

 

Direct text:

1. Determine whether the game direction can be moved or whether the game ends

Why are these two items put together?

My method is to create four bool variables in. h: whether to move up, whether to move down ,...

In this way, if neither of the four directions can be moved, the game is over.

 

Create a function for this determination:

 

Void GameScene: judgeMove () {int r, c; isMoveUp = false; isMoveDown = false; isMoveRight = false; isMoveLeft = false; // whether to move up for (r = 0; r <GAME_ROWS-1; ++ r) {for (c = 0; c <GAME_COLS; ++ c) {if (map [r + 1] [c] = 0) {if (map [r] [c]! = 0) {isMoveUp = true; break ;}} else {if (map [r] [c]! = 0) {if (m_allTiled.at (map [r] [c]-1)-> m_number = m_allTiled.at (map [r + 1] [c]-1) -> m_number) {isMoveUp = true; break ;}}}if (isMoveUp = true) break;} // whether to move down for (r = GAME_ROWS-1; r> 0; -- r) {for (c = 0; c <GAME_COLS; ++ c) {if (map [r-1] [c] = 0) {if (map [r] [c]! = 0) {isMoveDown = true; break ;}} else {if (map [r] [c]! = 0) {if (m_allTiled.at (map [r] [c]-1)-> m_number = m_allTiled.at (map [r-1] [c]-1)-> m_number) {isMoveDown = true; break ;}}}if (isMoveDown = true) break ;}// whether to move to the left for (c = 0; c <GAME_COLS-1; ++ c) {for (r = 0; r <GAME_ROWS; ++ r) {if (map [r] [c + 1] = 0) {if (map [r] [c]! = 0) {isMoveRight = true; break ;}} else {if (map [r] [c]! = 0) {if (m_allTiled.at (map [r] [c]-1)-> m_number = m_allTiled.at (map [r] [c + 1]-1) -> m_number) {isMoveRight = true; break ;}}}if (isMoveRight = true) break;} // can the right be moved for (c = GAME_COLS-1; c> 0; -- c) {for (r = 0; r <GAME_ROWS; ++ r) {if (map [r] [C-1] = 0) {if (map [r] [c]! = 0) {isMoveLeft = true; break ;}} else {if (map [r] [c]! = 0) {if (m_allTiled.at (map [r] [c]-1)-> m_number = m_allTiled.at (map [r] [C-1]-1)-> m_number) {isMoveLeft = true; break ;}}} if (isMoveLeft = true) break ;}}


 

To see if this method can be moved up:

From the first column in the bottom row to the last column in the last row, perform a two-layer loop:

-- First, determine whether the row above the same column is 0 (that is, whether it is an empty grid with no number blocks)

---- If it is null, check whether this row is empty.

------ If it is not null, it can be moved up

------ If it is null, it cannot be moved

---- If it is not null, determine whether the corresponding column of the row is null.

------ If it is null, it cannot be moved

------ It is not null. Check whether the same column of the row and the previous row are the same

-------- Same, it can be moved

-------- Different, cannot move

According to the preceding "--" length, we can see that the level is determined.

 

Then, create the game end interface class ~ GameOverScene:

 

# Ifndef _ test2048_GameOverScene_H __# define _ test2048_GameOverScene_H __# include cocos2d. hUSING_NS_CC; class GameOver: public Layer {public: virtual bool init (); static Scene * createScene (); CREATE_FUNC (GameOver ); // The callback function void menuRestartCallback (Ref * pObject) for restarting the game; // The exited callback function void menuExitCallback (Ref * pObject);}; # endif

. Cpp:

 

 

# Include GameOverScene. h # include GameDefine. h # include GameScene. hScene * GameOver: createScene () {auto scene = Scene: create (); auto * layer = GameOver: create (); scene-> addChild (layer ); return scene;} bool GameOver: init () {if (! Layer: init () {return false;} // Restart the game auto restartItem = MenuItemFont: create (Restart, CC_CALLBACK_1 (GameOver: menuRestartCallback, this )); restartItem-> setPosition (Point (GAME_SCREEN_WIDTH/2, GAME_SCREEN_HEIGHT/2); // exit the game auto Exit = MenuItemFont: create (exit, CC_CALLBACK_1 (GameOver: menuExitCallback, this); exit-> setPosition (Point (GAME_SCREEN_WIDTH/2, GAME_SCREEN_HEIGHT/3); auto menu = Menu: create (restartItem, exit, NULL ); menu-> setPosition (Point: ZERO); this-> addChild (menu); return true;} void GameOver: menuRestartCallback (Ref * pObject) {auto scene = GameScene :: createScene (); Director: getInstance ()-> replaceScene (CCTransitionProgressRadialCCW: create (1, scene);} void GameOver: menuExitCallback (Ref * pObject) {dire :: getInstance ()-> end ();}

OK. The game end interface has been completed. You can change AppDelegate. cpp to allow the game to go directly to the GameOver interface and see what it looks like:

 

 

The game end interface is like this ~

Do you think this is ugly? Lie XXXX,

Okay, it's ugly,

However, this series of tutorials mainly explain how to develop a 2048,

You can DIY your own things ~

Select a favorite background image and select the image button,

It's pretty ~

However, the premise is how to implement 2048. You need to know what I want to add, where I want to change, and what I want to add.

This series of tutorials explains how to create and parse 2048 ~

 

Let's talk a little bit about it. Continue and change the moveAllTiled function in GameScene ~ :

 

Void GameScene: moveAllTiled (MOVE_DIR dir) {// judge and move all blocks to remove judgeMove (); // if the upper, lower, and left cannot move, the game ends ~ If (! IsMoveLeft &&! IsMoveRight &&! IsMoveUp &&! IsMoveDown) {auto * scene = Scene: create (); GameOver * layer = GameOver: create (); scene-> addChild (layer); Director: shareddire () -> replaceScene (CCTransitionFadeDown: create (1.5f, scene);} // you must first determine whether to move switch (dir) {case MOVE_DIR in that direction:: UP: if (! IsMoveUp) return; moveUp (); break; case MOVE_DIR: DOWN: if (! IsMoveDown) return; moveDown (); break; case MOVE_DIR: LEFT: if (! IsMoveLeft) return; moveLeft (); break; case MOVE_DIR: RIGHT: if (! IsMoveRight) return; moveRight (); break; default: break;} // generate a new newNumberTiled ();}

For the end of the game, this is the end of the game ~

 

 

2. Add sound effects

For June 2048, I don't think we should add any background music. It's awkward to say,

I simply added two sound effects. If there is a merge of numbers, a small sound effect is successful,

If no number is merged, It is a sliding sound effect,

Of course, first put the corresponding file under the Resource folder, and add existing items in VS2012,

 

In the program, you also need to load,

I chose to load the game before the main interface jumps to the game interface:

 

// Load the sound effects SimpleAudioEngine: getInstance ()-> preloadEffect(move.wav); SimpleAudioEngine: getInstance ()-> preloadEffect(move1.wav );

Of course, you should add the following header file for sound effects:

 

 

#include SimpleAudioEngine.husing namespace CocosDenshion;

Then, add audio play to all the block functions of the game mobile:

 

Now, add two bool variables to the header file:

 

// Specifies the music bool m_sound_clear to play;

 

GameScene. cpp -- moveAllTiled:

 

Void GameScene: moveAllTiled (MOVE_DIR dir) {// judge and move all blocks to remove judgeMove (); // if the upper, lower, and left cannot move, the game ends ~ If (! IsMoveLeft &&! IsMoveRight &&! IsMoveUp &&! IsMoveDown) {auto * scene = Scene: create (); GameOver * layer = GameOver: create (); scene-> addChild (layer); Director: shareddire () -> replaceScene (CCTransitionFadeDown: create (1.5f, scene ));}
m_sound_clear = false;
// Move in the direction, but first determine whether the switch (dir) {case MOVE_DIR: UP: if (! IsMoveUp) return; moveUp (); break; case MOVE_DIR: DOWN: if (! IsMoveDown) return; moveDown (); break; case MOVE_DIR: LEFT: if (! IsMoveLeft) return; moveLeft (); break; case MOVE_DIR: RIGHT: if (! IsMoveRight) return; moveRight (); break; default: break;} // if (m_sound_clear) {SimpleAudioEngine: getInstance ()-> playpolict(move.wav);} else {SimpleAudioEngine:: getInstance ()-> playpolict(move1.wav);} // generate a new newNumberTiled ();}

Then, in the corresponding moving function, set the m_sound_clear value:

 

Set the moveUp, moveDown, moveLeft, and moveRight positions.

Under the judgment Statement of number Merge (where numNow = numObj is determined), perform the following operations:

 

m_sound_clear = true;

 

OK. We can run it. Feel it ~

If you have any questions about sound effects, read the article I wrote earlier, specifically the blog post on sound effects: Stamp me ~

 

Next, let's set the score ~

 

3. Add scores

First, on the game interface, set the header file and integer score variable

Then, set the score position in the initialization function:

GameScene. cpp -- init:

 

TTFConfig config (HelloKitty. ttf, 40); // score = 0; auto labelScore = Label: createWithTTF (config, Score: 0); labelScore-> setPosition (Point (GAME_SCREEN_WIDTH/2, GAME_SCREEN_HEIGHT-1.5 * labelScore-> getContentSize (). height); this-> addChild (labelScore); labelScore-> setTag (105 );


 

The increase in our score is that every time a new number is merged, the generated number is added,

For example, if two 2 generate a 4, the score is + 4,

If two 512 generate a 1024, the score is + 1024,

Therefore, the position of the score change is the same as that of the music set,

Because the score is increased only when the merging is successful:

 

if( numNow == numObj ){m_sound_clear = true;score += numObj * 2;


 

Then, after moving all the blocks, update the score:

GameScene. cpp -- moveAllTiled:

 

// Label * labelScore = (Label *) this-> getChildByTag (105); labelScore-> setString (StringUtils: format (Score: % d, score ));

Okay ~,~ The score is also updated.

 

Run the command to check the effect ~

 

Running properly ~

Hey, the fifth article is here,

The next article is the final chapter.

Android platform porting ~

This series is coming to an end.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.