Timers are indispensable in most games, because every certain period of time, the corresponding refresh function must be executed to update the game's screen, time, progress, enemy's instructions, and so on.
Cocos2d-x provides us with timer schedule related operations. The operation function is defined in CCNode, so basically most engine classes can set timers, such as CCLayer, CCSprite, CCMenu, etc.
There are three types of timer update methods:
(1) The default timer: scheduleUpdate();
(2) Custom timer: schedule();
(3) One-time timer: scheduleOnce();
Timer schedule, update.rar
scheduleUpdate
Default timer: scheduleUpdate().
The default refresh times of the timer is related to the screen refresh frequency. If the frequency is 60 frames per second, then scheduleUpdate performs 60 refreshes per second.
The refresh function body corresponding to scheduleUpdate is update(), that is, the update() function is executed once every frame.
The related operations are as follows:
//
//Turn on the default timer. The refresh interval is one frame.
void scheduleUpdate();
void scheduleUpdateWithPriority(int priority); //Give priority priority. The smaller the priority, the higher the priority
virtual void update(float delta); //update is the refresh function body of the scheduleUpdate timer.
//
schedule
Custom timer: schedule().
The timer can customize the designated refresh function body, the number of refresh function bodies, refresh frequency, and the time to start refresh.
The function body is not necessarily update(), you can define it yourself.
The related operations are as follows:
//
//Set a custom timer. The default refresh interval is one frame.
// interval: Execute once every interval seconds.
// repeat: number of repeats.
// delay: Delay time, that is, the refresh will start after the timer delay seconds are created.
//schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
void schedule(SEL_SCHEDULE selector); //The default refresh interval is one frame
void schedule(SEL_SCHEDULE selector, float interval); //Custom refresh interval, unit: second
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
//
scheduleOnce
One-time timer: scheduleOnce().
The timer only executes the refresh function body once after waiting for the delay second delay time, and then does not refresh again.
The related operations are as follows:
//
//Execute only once, execute after delay seconds
//scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 );
void scheduleOnce(SEL_SCHEDULE selector, float delay);
//
Other operations
Cancellation, pause, and resume of the timer.
The related operations are as follows:
//
//this->unscheduleUpdate();
//sprite->unscheduleAllSelectors();
void unscheduleUpdate(void); //Cancel the default timer
void unschedule(SEL_SCHEDULE selector); //Cancel the timer of the custom function
void unscheduleAllSelectors(void); //Cancel all timers
void pauseSchedulerAndActions(void); //Pause all timers and actions
void resumeSchedulerAndActions(void); //Resume all timers and actions
//
Code combat
1. Create five elves in HelloWorld::init()
There is a one-to-one correspondence between the wizard and five methods of defining timers.
//
//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 the wizard
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 the sprite with 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 the sprite with 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 the sprite with 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 the sprite with 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 the sprite with tag=104
sp4->setPosition( sp4->getPosition() + ccp(100,0) ); //move 100
}
//
3. Running results
4. Analysis and summary
(1) ScheduleUpdate() has the same effect as schedule(schedule_selector), except that schedule can customize the refresh function body, not necessarily update(). The refresh function body of scheduleUpdate() can only be update().
(2) schedule(schedule_selector, interval): interval=1.0 is set, so myupdate2() is executed every 1.0 second.
(3) schedule(schedule_selector, interval, repeat, delay): start to execute myupdate3() after 3.0f seconds, and then repeat the execution 5 times, then stop updating.
(4) scheduleOnce(schedule_selector): 5 seconds after the start, myupdate4() was executed only once, and the update stopped.