Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of updating schedule/update by timer

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of timer update schedule/update". In daily operation, I believe many people have doubts about what is the method of timer update schedule/update. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what is the method of timer update schedule/update"! Next, please follow the small series to learn together!

[Chattering]

Timers are indispensable in most games, that is, every once in a while, the corresponding refresh function is executed to update the game screen, time, progress, enemy commands, etc.

Cocos2dx provides us with timer schedule-related operations. The definition of its operation function is in CCNode, so basically most engine classes can set timers, such as CCLayer, CCSprite, CCMenu, etc.

There are three types of timer updates:

Default timer: scheduleUpdate();

(2) custom timer: schedule();

(3) One-time timer: scheduleOnce();

【3.x】

Almost unchanged.

【scheduleUpdate】

Default timer: scheduleUpdate().

The timer default refresh times are related to the screen refresh frequency. If the frequency is 60 frames per second, scheduleUpdate performs 60 refreshes per second.

The body of the refresh function corresponding to scheduleUpdate is update(), i.e. the update() function is executed once per frame.

The relevant operations are as follows:

// //Start default timer. The refresh interval is one frame. void scheduleUpdate(); void scheduleUpdateWithPriority(int priority); //Give priority. The lower the priority, the higher the priority. virtual void update(float delta); //update is the refresh function body of scheduleUpdate timer.//

【schedule】

Custom timer: schedule().

The timer can customize the specified refresh function body, the number of times to refresh the function body, the refresh frequency, and the time to start refreshing.

The body of the function does not have to be update(), it can be defined by itself.

The relevant operations are as follows:

// //Set custom timer. The default refresh interval is one frame. // interval : Executed once every interval seconds. // repeat : Repeat number. // delay : Delay time, i.e. refresh starts seconds after timer delay is created. //schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 ); void schedule(SEL_SCHEDULE selector); //default refresh interval is one frame void schedule(SEL_SCHEDULE selector, float interval); //Custom refresh interval, in seconds void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);//

【scheduleOnce】

One-time timer: scheduleOnce().

The timer waits for a delay of seconds, then refreshes the function body only once, and then no longer.

The relevant operations are as follows:

// //execute only once,delay seconds later //scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 ); void scheduleOnce(SEL_SCHEDULE selector, float delay);//

[Other Operations]

Timer cancellation, pause, resume.

The relevant operations are as follows:

// //this->unscheduleUpdate(); //sprite->unscheduleAllSelectors(); void unscheduleUpdate(void); //cancel default timer void unschedule(SEL_SCHEDULE selector); //cancel timer of custom function void unscheduleAllSelectors(void); //cancel all timers void pauseSchedulerAndActions(void); //pause all timers and actions void resumeSchedulerAndActions(void); //Restore all timers and actions//

Code Combat

Create five sprites in HelloWorld::init()

Genie and five ways to define a timer, one by one.

// //create five sprites CCSprite* sp = CCSprite::create("Icon.png"); sp->setPosition( ccp(30, mysize.height - 30) ); this->addChild(sp, 0, 100); //tag tag 100 CCSprite* sp1 = CCSprite::create("Icon.png"); sp1->setPosition( ccp(30, mysize.height - 90) ); this->addChild(sp1, 0, 101); //tag tag 101 CCSprite* sp2 = CCSprite::create("Icon.png"); sp2->setPosition( ccp(30, mysize.height - 150) ); this->addChild(sp2, 0, 102); //tag tag 102 CCSprite* sp3 = CCSprite::create("Icon.png"); sp3->setPosition( ccp(30, mysize.height - 210) ); this->addChild(sp3, 0, 103); //tag tag 103 CCSprite* sp4 = CCSprite::create("Icon.png"); sp4->setPosition( ccp(30, mysize.height - 270) ); this->addChild(sp4, 0, 104); //tag tag 104 //Define five timers, update sprites this->scheduleUpdate(); this->schedule( schedule_selector(HelloWorld::myupdate) ); this->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f ); this->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, 5, 3.0f); this->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );//

2. Write the refresh function body corresponding to the timer

// //scheduleUpdate void HelloWorld::update(float dt) { CCSprite* sp = (CCSprite*)this->getChildByTag(100); //get sprite tag=100 sp->setPosition( sp->getPosition() + ccp(1,0) ); //Move 1 per frame } //schedule(schedule_selector) void HelloWorld::myupdate(float dt) { CCSprite* sp1 = (CCSprite*)this->getChildByTag(101); //get sprite tag=101 sp1->setPosition( sp1->getPosition() + ccp(1,0) ); //Move 1 per frame } //schedule(schedule_selector, interval) void HelloWorld::myupdate2(float dt) { CCSprite* sp2 = (CCSprite*)this->getChildByTag(102); //get sprite tag=102 sp2->setPosition( sp2->getPosition() + ccp(60,0) ); //move 60 per second } //schedule(schedule_selector, interval, repeat, delay) void HelloWorld::myupdate3(float dt) { CCSprite* sp3 = (CCSprite*)this->getChildByTag(103); //get sprite tag=103 sp3->setPosition( sp3->getPosition() + ccp(60,0) ); //move 60 per second } //scheduleOnce void HelloWorld::myupdate4(float dt) { CCSprite* sp4 = (CCSprite*)this->getChildByTag(104); //get sprite tag=104 sp4->setPosition(sp4->getPosition() + ccp(100,0) ); //Move 100 }//

3. Operation results

At this point, the study of "what is the method of updating schedule/update timer" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report