Cocos2d-x 3.2 It 2048--third

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ******************************************



Ok. Hurry up and update it.

Long story short, straight to the subject. Third article:

--Creation of digital block class


The number block is God horse?

--that is, the number you are sliding, and the number that appears randomly.

We first create a class numbertiled, which inherits from node:

NumberTiled.h:

#ifndef __test2048_numbertiled_h__#define __test2048_numbertiled_h__#include "Cocos2d.h" Using_ns_cc;class Numbertiled:public node{public://stores the column position and the numeric value of the position int m_row,m_col;int m_number;//move to r,c this position void moveTo (int r, int c ); Create_func (numbertiled); bool Init ();}; #endif


NumberTiled.cpp:

#include "NumberTiled.h" #include "GameDefine.h" Using_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);//Digital Layer--random out a number. Generates 4 if the number equals 7 otherwise generates 2int n = rand ()%10;this, M_number = n==7?4:2;//depending on the value of the number, assign a different color 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 the font and show the number of this block Ttfconfig config ("Hellokitty.ttf", +); 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)));} 

Well, the function of this kind of foundation is complete,

is to initialize and move (appear) to a certain location.


Next, we'll show it randomly on our interface.

Go to the game interface,

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

Now we have to add a vector (set) to hold all the blocks.

GameScene.h:

vector<numbertiled *> m_alltiled;


And then. In Init. We created the lattice, and then, with a number block,

It's going to produce a random block.

Add a method--newnumbertiled

This function is to create a new block:

void gamescene::newnumbertiled () {//Create an instance of a number block auto tiled = Numbertiled::create ();//Find a location with several spare int freecount = 16-m_allt Iled.size (); int num = rand ()% freecount;int row = 0,col = 0,count = 0;bool Find = false;//produces a number, must be in a blank area for (; row < GA Me_rows; ++row) {for (col = 0; col < game_cols; ++col) {if (map[row][col] = = 0) {//record number of empty areas ++count;if (Count >= num) {fin d = True;break;}}} if (find) {break;}}  Note Add tiled yo colorback-addChild (tiled) in Colorback, tiled-moveTo (row, col); M_alltiled.pushback (tiled); map[ Row] [col] = M_alltiled.getindex (tiled) +1;}

Well, explain it in detail. has been given in the code.

Explain how to randomly generate blocks in an empty location:

First of all. Gets the number of empty locations

And then. Randomly produces a number. Less than the total number of empty locations

Then set the count to 0,

Traverse from the first position to the last position,

Encounter empty position, count+1.

If it is greater than the resulting random number, the number block is placed at that position.



Now, to be able to execute a bit. Take a look at the effect:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhr0cmvl/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast ">


Ok, this time it's here.

The next content is. Handling of touch Screens ~

Please look forward to



This article is code: HTTP://PAN.BAIDU.COM/S/1SJHLNP7



*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ******************************************



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Cocos2d-x 3.2 It 2048--third

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.