Cocos2d-x 3.2 don't step on the white block (first)
Preface:
There are a lot of things coming soon, so you can only find some simple busy jobs,
No one remembers this game =.
Development Environment -- VS2012 + cocos2d-x 3.2
Body:
I don't want to talk much about it. Now let's do nothing about it.
Compared with the previous one, this game will be much simpler,
Create a project, modify its AppDelegate. cpp, and set the display size.
Before generating the APK, you need to set it in android to always portrait.
Outline
> Block class Creation
> Create a start row and end row
> Create black/white blocks
1. The first formal step
This is the playing of the most important category in the game. Block-Block
There are many blocks in this game, black, white, and yellow,
It is encapsulated in a class, that is, Block.
Create this class and set an initialization function for it.
-- Color
-- Size
-- Block content
-- Content font size
-- Content font color
Implement it in cpp
// Block. cppbool Block: init (Color3B color, Size size, std: string label, float fontSize, Color4B textColor) {// first execute Sprite initialization if (! Sprite: init () {return false;} // set the content size of the Block setContentSize (size); // the lower left corner alignment setAnchorPoint (Point: ZERO ); // text display position setTextureRect (Rect (0, 0, size. width, size. height); // color setColor (color); // Add a Text auto l = Label: create (); l-> setString (label ); l-> setSystemFontSize (fontSize); l-> setTextColor (textColor); // Add it to the current scenario addChild (l); l-> setPosition (size. width/2, size. height/2); return true ;}
In the Block class, there is also a static createBlock creation function, a removeBlcok removal function, and a Vector used to put the block
The following is an implementation:
// Block. cpp // initialize VectorVector
* Block: blocks = new Vector
(); Block * Block: createBlock (Color3B color, Size size, std: string label, float fontSize, Color4B textColor) {auto block = new Block (); block-> init (color, size, label, fontSize, textColor); block-> autorelease (); blocks-> pushBack (block); return block;} void Block :: removeBlock () {removeFromParent (); blocks-> eraseObject (this );}
2. Add the start row and end row
The starting line is the yellow stuff at the bottom before the game starts.
Here we will not create a scenario. simply modify the original function in HelloWorldScene,
Delete useless items first,
To start adding rows, we use a function to implement -- addStartLine
// HelloWorldScene. cppvoid HelloWorld: addStartLine () {// Add a square auto block = Block: createBlock (Color3B: YELLOW, Size (visibleSize. width, visibleSize. height/4), 20, Color4B: BLACK); addChild (block );}
In the init function of the scenario, call this function and run it to see that the start line has been completed ~
After the start line is added, the end line is simpler,
The end line is when the game ends, and the entire interface is green,
The same method -- a function: addEndLine
// HelloWorldScene. cpp // Add the end line void HelloWorld: addEndLine () {auto block = Block: createBlock (Color3B: GREEN, visibleSize, Game Over, 32, Color4B: BLACK ); addChild (block );}
You can change addStartLine In the init function of the scenario to addEndLine. Let's take a look at the effect.
3. Add black and white blocks
This is similar to adding a start row or an end row, but it is a little more troublesome than that.
First, add a lineIndex variable to the Block class, and then add the get and set functions related to it.
This variable is used to know which row the block is placed in,
For example, the start row should be placed in the first row (modify it at the corresponding location of addStartLine)
The end row should be placed in 4th rows (same as above, and modified in the corresponding location)
Then add the black/white block function.
// HelloWorldScene. cpp // Add a black/white Block void HelloWorld: addNormalLine (int lineIndex) {block * Block; // rand () is a pseudo-random number int blackIndex = rand () % 4; for (int I = 0; I <4; I ++) {block = Block: createBlock (blackIndex = I? Color3B: BLACK: Color3B: WHITE, Size (visibleSize. width/4-2, visibleSize. height/4-2), 20, Color4B: BLACK); addChild (block); block-> setPosition (I * visibleSize. width/4, lineIndex * visibleSize. height/4); block-> setLineIndex (lineIndex );}}
You must know that the interface is divided into four rows (0, 1, 2, 3) and four columns,
Random numbers are randomly generated ~ 3. Only one of each row will be the same as I. This is the Black Block and the rest will be the white block,
At> blackIndex = I? Color3B: BLACK: Color3B: WHITE <knows one or two.
In the init function of the scenario, instead of directly calling addstartLine, you can set a startGame function to call it.
// HelloWorldScene.cppvoid HelloWorld::startGame(){addStartLine();addNormalLine(1);addNormalLine(2);addNormalLine(3);}
Then run the command to see
OK, the basic work is done. Next, we will continue to describe the logic part of the white block.