[Cocos2d-x production don't step on the white block] Ninth: Game timing function (with source code)

Source: Internet
Author: User
Tags getcolor

The game has no timing. Isn't it a pitfall?

In this phase, we will join the game timing function in the future.


1. Define variables and functions

Let's first define several variables and functions in helloworldscene. h.

long startTime;bool timeRunning;
Starttime is used to record the start time, And timerunning is used to infer whether the game is in progress.


// Start time void starttimer (); // End Time void stoptimer (); Virtual void Update (float DT );
Starttimer () is used to start timing. stoptimer () is used to end timing, and update () is used to call each frame.


2. implement three functions in detail

Then, let's take a look at the detailed implementation of the three functions.

Void helloworld: Update (float DT) {long offset = clock ()-starttime; timerlabel-> setstring (stringutils: Format ("% G", (double) offset) /1000000);} // start time void helloworld: starttimer () {If (! Timerunning) {scheduleupdate (); starttime = clock (); timerunning = true ;}// void helloworld: stoptimer () {If (timerunning) {unscheduleupdate (); timerunning = false ;}}
The starttimer () function first determines whether the timer is in progress. If no, call the UPDATE function first.

The stoptimer () function first determines whether the timer is in progress. If there is a timer, the update will be uninstalled.

The update function is used to calculate the time difference and display it.


3. Call a function

Of course, after the function is implemented, it must be called. So where to call it?

Of course, starttimer is called after the black block is clicked, and stoptimer is called after the green block is clicked.

                                if(b->getColor()==Color3B::BLACK){b->setColor(Color3B::GRAY);this->moveDown();this->startTimer();}else if(b->getColor()==Color3B::GREEN){this->moveDown();this->stopTimer();}

4. execute the project

After the project is executed, the effect is as follows:


5. generate real random numbers

When the results are executed too much, you will find that Black Blocks do not appear randomly, but often appear in the same position today. At this time, we need to add a line of code

srand(time(NULL));

In this way, random numbers are generated randomly.


6. Change resolution

After the project is executed, the format is a bit large, which is actually a resolution problem. In the official version 3.0, the resolution change is no longer directly changed in the main function. You can add the following two lines of code in appdelegate:

    glview->setFrameSize(320,480);    glview->setDesignResolutionSize(320,480,ResolutionPolicy::SHOW_ALL);


Remember to add the following code

    if(!glview) {        glview = GLView::create("My Game");        director->setOpenGLView(glview);    }

At this time, let's execute the project again. Is it much more comfortable?


So far, the core features of our game have basically been completed. Pick up the keyboard and try to write the program.

7. Source Code


Source Code directly: http://download.csdn.net/detail/legendof1991/7348941







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.