Cocos2dx game development-do not step on the white block learning notes (2)-Implementation of the Classic Mode

Source: Internet
Author: User
Tags add time getcolor

1. Create gamescene and gamelayer

Simply create a scene. I will not talk about it here ~, For more information, see my study notes (2 ).

2. Add a start Column

It's easy to call the create method in the block ~, You only need to input parameters such as size and color.

Void gamelayer: addstartline () {auto block = block: createwithargs (color3b: yellow, size (visiblesize. width, visiblesize. height/4), "Touch to start", 40, color4b: Black); // call the method. This-> addchild (Block); // Add it to the scene. If no position is set, it is the default position in vec2: zero}

Then look at the effect (remember to call this method in init ).

 

3. Add an ending Column

void GameLayer::addEndLine(){    auto block = Block::createWithArgs(Color3B::GREEN, visibleSize, "Game Over", 40, Color4B::BLACK);    block->setBlockCol(4);    this->addChild(block);    }

The effect is as follows:

4. Add normalline and implement initialization

(1) The normalline method is to add four blocks, one black and three white blocks. The implementation is as follows:

Void gamelayer: addnormalline (INT blockcol) // Col refers to the row {int blackrow = ccrandom_0_1 () * 4; // random number, random block for (INT I = 0; I <4; I ++) {auto block = block: createwithargs (blackrow = I? Color3b: Black: color3b: white, size (visiblesize. width/4-1, visiblesize. height/4-1), "", 20, color4b: Black); block-> setposition (vec2 (I * visiblesize. width/4, blockcol * visiblesize. height/4); block-> setblockcol (blockcol); // The row where the storage is located this-> addchild (Block );}}

(2) initialization Interface

void GameLayer:: startGame(){    this->addStartLine();    this->addNormalLine(1);    this->addNormalLine(2);    this->addNormalLine(3);    }

So, we don't have welcomescene. Don't step on the starting interface of the white block. That's OK !!

 

5. Implementation of game touch events.

The interaction between games is very simple, that is, if you click the black button, it turns gray, and then moves down (this is put in gameloop). If you click the white button, you can worship.

The implementation is as follows:

(1) inherit the layer touch method first.

Virtual bool ontouchbegan (touch * touch, event * unused_event );

This is a single point of touch method. There are three other methods. If we don't use them here, let's just talk about them. This method can help us implement the touch interaction function.

Then we need to add this to the event listener in init.

Auto touchlistener = eventlistenertouchonebyone: Create (); // create a single-touch method touchlistener-> ontouchbegan = cc_callback_2 (gamelayer: ontouchbegan, this); // Add method dire :: getinstance ()-> geteventdispatcher ()-> addeventlistenerwithscenegraphpriority (touchlistener, this); // set the priority

(2) Implementation of the touch Method

Bool gamelayer: ontouchbegan (touch * touch, event * unused_event) {auto BS = block: getblocks (); // retrieve the array block that stores the block * B; for (Auto it = BS-> begin (); it! = BS-> end (); It ++) {B = * it; // It is an array pointer, and * It is the content stored in it, that is, the block class pointer if (B-> getblockcol () = 1 & B-> getboundingbox (). containspoint (touch-> getlocation () {If (B-> getcolor () = color3b: Black) {B-> setcolor (color3b: Gray ); // gray break;} else {MessageBox ("gameover", "failed"); // after a failure, you can force a new start .. I am saving it. }}} Return true ;}

As follows:

 

6. Implementation of gameloop

(1) Implementation of movedown

Void gamelayer: movedown () {This-> addnormalline (4); // Add a column auto BS = block: getblocks (); for (Auto it = BS-> begin (); it! = BS-> end (); It ++) {(* It)-> movedown (); // all blocks move down. }}

(2) Implementation of gameloop

If you only implement (1) =, you won't be able to stop it until it reaches the ground. =, so we need to add a limit when the game ends.

Or when the hand is broken to the white, or if the collection is full of 25 blocks ...... (You know ).

Therefore, we need to add a counting Variable _ linecount and the bool Variable _ showendline,

1. Definition Initialization

Define an int variable in gamelayer. h, and then initialize it to 0 in that initialization, and initialize it to false in _ showendline.

2. Then count in addnormalline () method ++

3. perform an end judgment in movedone.

if(_lineCount<25)    {        this->addNormalLine(4);    }        else if(!_showEndLine)    {       this-> addEndLine();       _showEndLine = true;    }

4. Then, in the touch event, you need to make another judgment, that is, although the end column is clicked to the point, it will not become gray =

If (B-> getcolor () = color3b: Black) {B-> setcolor (color3b: Gray); this-> movedown (); break ;} else if (B-> getcolor () = color3b: Green) {This-> movedown () ;}else {MessageBox ("gameover", "failed ");}

5. Final

 

7. add time.

(1) Of course, you need to create a label, which is defined in the header file:

Label * _ timerlabel; long _ starttime; // bool _ timerunning at the start time; // whether the node is running

(2) related methods

Void gamelayer: Update (float DT) // The inherited update method will automatically update the screen 60 times a second {long offset = clock ()-_ starttime; // calculate the time _ timerlabel-> setstring (stringutils: Format ("% G", (double) offset)/1000000); // change the label value .} Void gamelayer: starttimer () // start time {If (! _ Timerunning) {scheduleupdate (); _ starttime = clock (); _ timerunning = true ;}} void gamelayer: stoptimer () // end {If (_ timerunning) {unscheduleupdate (); _ timerunning = false ;}}

Void inittimelabel () // initialize _ timerlabel
{

_ Timerlabel = label: Create ();

_ Timerlabel-> setcolor (color3b: Blue );

_ Timerlabel-> setsystemfontsize (50 );
_timerLabel->setString("0.0000");_timerLabel->setPosition(visibleSize.width / 2, visibleSize.height - 100);this->addChild(_timerLabel,10)
}

Then. See the results. This is done in the Classic mode. I will share it with you in other modes later, because I also learn from others and I am a little white, I only want to strengthen my impression by writing a blog every time I study and hope other tips can be used for reference. After all, only openness can lead to faster and better progress.

 

 

 

 

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.