Cocos2d-x 3.2-2048-Article 3

Source: Internet
Author: User

Cocos2d-x 3.2-2048-Article 3

 

 

OK. update it now.

To put it short, go straight to the topic, article 3:

-- Create a digital block class

 

What is a digital block?

-- That is, the number you slide, and the number that appears randomly.

First, create a NumberTiled class that inherits from Node:

NumberTiled. h:

 

# Ifndef _ test2048_NumberTiled_H __# define _ test2048_NumberTiled_H __# include cocos2d. hUSING_NS_CC; class NumberTiled: public Node {public: // store the row and column positions and the numeric values of this position: int m_row, m_col; int m_number; // move to r, c: void moveTo (int r, int c); CREATE_FUNC (NumberTiled); bool init () ;}; # endif

 

 

NumberTiled. cpp:

 

# Include NumberTiled. h # include GameDefine. hUSING_NS_CC; bool NumberTiled: init () {if (! Node: init () {return false;} // background layer auto bk = LayerColor: create (Color4B (200,200,200,255), GAME_TILED_WIDTH, GAME_TILED_HEIGHT ); this-> addChild (bk); // number layer -- a random number is generated. If the number is 7, 4 is generated. Otherwise, 2int n = rand () % 10 is generated; this-> m_number = n = 7?; // Assign different colors to switch (this-> m_number) {case 2: bk-> setColor (Color3B (230,220,210); break; case 4: bk-> setColor (Color3B (230,210,190); break; default: break;} // create a font and display the number of this block to TTFConfig config (HelloKitty. ttf, 40); auto label = Label: createWithTTF (config, StringUtils: format (% d, this-> m_number )); label-> setPosition (Point (GAME_TILED_WIDTH/2, GAME_TILED_HEIGHT/2); label-> setColor (Color3B: BLACK); bk-> addChild (label); return true ;} void NumberTiled: moveTo (int r, int c) {this-> m_row = r; this-> m_col = c; this-> setPosition (Point (m_col * GAME_TILED_WIDTH + GAME_TILED_BOARD_WIDTH * (m_col + 1), m_row * GAME_TILED_HEIGHT + GAME_TILED_BOARD_WIDTH * (m_row + 1 )));}

Okay, the basic functions of this class have been completed,

 

It is the location where both initialization and movement (appearance) are located.

 

Next, we will randomly display them on our interface ~

Go to the game interface,

In the previous article, we added a logical array map,

Now, we need to add a Vector (SET) to save all the blocks,

GameScene. h:

 

Vector
 
   m_allTiled;
 

 

 

Then, in init, we created a grid. Next, With a number block,

Then a random block is generated,

Add a method -- newNumberTiled

This function is used to generate a new block:

 

Void GameScene: newNumberTiled () {// create an instance auto tiled = NumberTiled: create (); // find several idle locations: int freeCount = 16-m_allTiled.size (); int num = rand () % freeCount; int row = 0, col = 0, count = 0; bool find = false; // generate a number, which must be in the blank area for (; row <GAME_ROWS; ++ row) {for (col = 0; col <GAME_COLS; ++ col) {if (map [row] [col] = 0) {// record the number of blank areas + + count; if (count> = num) {find = true; break ;}}if (find) {break ;}// note that tiled colorBack-> addChild (tiled); tiled-> moveTo (row, col); m_allTiled.pushBack (tiled); map [row] [col] = m_allTiled.getIndex (tiled) + 1 ;}

Well, the specific explanation has been given in the code.

 

Explain how to randomly generate blocks at an empty location:

First, obtain the number of null locations,

Then, a random number is generated, which is smaller than the total number of null positions.

Set count to 0,

Traverse from the first position to the last position,

When an empty location is encountered, count + 1,

If the value is greater than the random number, place the number block at this position.

 

 

Now, you can run it to see the effect:


 

 

OK, this is the time,

Next time the content is, processing the touch screen ~

Coming soon ~

 

 

This article code: http://pan.baidu.com/s/1sjHLNp7

 

 

 

* *********************************** Please specify the source: bytes ******************************************



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.