Cocos2d-x learning notes (6) simple application of timer schedule

Source: Internet
Author: User
Zookeeper

The timers in the Cocos2d-x are very easy to use and come together with three types: Schedule, scheduleupdate, and scheduleonce. There are three differences:

SCHEDULE: runs a defined function at a specified time.

Schedule (schedule_selector (custom function name), interval );

Scheduleupdate: run the UPDATE function once every frame (only functions named after update can be used. If you need to call other functions, use Schedule)

Scheduleonce: runs a defined function after a specified time and only runs once.

Scheduleonce (schedule_selector (function name defined by yourself), interval );

Today we will learn how to use schedule. It is very easy to learn the other two methods. First, let's take a simple example to display the current system time and use schedule to refresh the current time.


//labelpLabel = CCLabelTTF::create(CCString::createWithFormat("%d:%d:%d", now->tm_hour, now->tm_min, now->tm_sec)->getCString(), "Thonburi", 34);CCSize s = CCDirector::sharedDirector()->getWinSize();pLabel->setPosition(ccp(s.width / 2, s.height / 2));addChild(pLabel);schedule(schedule_selector(Schedule::showTimebyLabel), 1.0f);

Create a label and display the current system time by default. Then, use the schedule function to call our own function every second.

void Schedule::showTimebyLabel(float dt){time(&tt);now = localtime(&tt);pLabel->setString(CCString::createWithFormat("%d:%d:%d",now->tm_hour,now->tm_min,now->tm_sec )->getCString());}

After running the command, you can see that the screen is continuously beating. In fact, the learning of the Schedule function is almost the same here, but I want to try to implement the countdown of art in many games, therefore, in combination with the previous genie knowledge, we made a system time that was implemented using the art words to dynamically display the system time. The countdown is just a little meaningless.

The implementation principle is almost the same as that shown in the label. You can use schedule to call the function of changing images to the sprite every second. I won't post more code than repeated tokens. Come on with two.

Then there is the digital material used. I couldn't find it on the Internet for a long time. Many Generating tools must be used on Mac, so I couldn't just use PS for it. I wanted to put the plist of this digital material on it, but I couldn't find a place where the attachment could be uploaded ...... It looks like you cannot ......


Cocos2d-x learning notes (6) simple application of timer schedule

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.