Inheritance relationship
Principle Introduction
The COCOS2D-X Scheduler provides timed events and timed call services for the game. All node objects know how to dispatch and cancel scheduled events, and there are several benefits to using the scheduler:
- The scheduler stops whenever node is no longer visible or has been removed from the scene.
- The scheduler will also stop when the cocos2d-x is paused. When Cocos2d-x restarts, the scheduler will automatically continue to start.
- The Cocos2d-x encapsulates a scheduler for use on a variety of platforms, which you do not care about and track the destruction and stop of timed objects you set, and the risk of crashes.
Basic usage
In the game we often make some logical judgments over time, such as collision detection. To solve these problems, we introduced the scheduler, which allows the game to handle dynamic events better. Cocos2d-x provides a variety of scheduling mechanisms, and in development we typically use 3 types of schedulers:
- Default Scheduler:
schedulerUpdate()
- Custom Scheduler:
schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
- Single-Time Scheduler:
scheduleOnce(SEL_SCHEDULE selector, float delay)
Let's make a brief introduction to these 3 kinds of schedulers.
Default Scheduler (Schedulerupdate)
The scheduler is a Refresh event Update method that uses node, which is called once before each frame is drawn. Because the time interval between frames is short, each frame refresh is sufficient to complete the logical judgment required for most of the gameplay.
Node Cocos2d-x does not have the Update event enabled by default, so you need to overload the Update method to execute your own logic code.
You can use the method if you need to stop the scheduler by executing the Update method per frame of the schedulerupdate () scheduler unschedulerUpdate()
.
The following code is used to test the scheduler:
HelloWorldScene.hvoid update(float dt) override;
HelloWorldScene.cppbool HelloWorld::init(){ ... scheduleUpdate(); return true;}void HelloWorld::update(float dt){ log("update");}
You will see the console output the following information
cocos2d: updatecocos2d: updatecocos2d: updatecocos2d: update
Custom Scheduler (Scheduler)
Game development, in some cases we may not need frequent logic detection, so as to improve game performance. So Cocos2d-x also provides a custom scheduler that allows you to call a function continuously at a certain interval.
Because of the scheduling mechanism of the engine, the custom interval must be greater than the interval of two frames, otherwise multiple calls within two frames are merged into one call. So the custom time interval should be more than 0.1 seconds.
Similarly, canceling the scheduler can be used unschedule(SEL_SCHEDULE selector, float delay)
.
The following code is used to test the scheduler:
HelloWorldScene.hvoid updateCustom(float dt);
HelloWorldScene.cppbool HelloWorld::init(){ ... schedule(schedule_selector(HelloWorld::updateCustom), 1.0f, kRepeatForever, 0); return true;}void HelloWorld::updateCustom(float dt){ log("Custom");}
In the console you will see output the following information every 1 seconds
cocos2d: Customcocos2d: Customcocos2d: Customcocos2d: Customcocos2d: Custom
Let's take a look at the parameters inside the scheduler (sel_schedule selector, float interval, unsigned int repeat, float delay) function:
- The first parameter, selector, is the event function you want to add.
- The second parameter interval is the event firing interval
- The third parameter, repeat, is the number of times a trigger is triggered after an event, and the default value is Krepeatforever, which indicates the number of infinite triggers
- The fourth parameter, delay, indicates the delay before the first trigger
Single-Time Scheduler (scheduleronce)
On some occasions in the game, you just want to do a logic check, Cocos2d-x also provides a single-time scheduler.
The scheduler fires only once, and is used unschedule(SEL_SCHEDULE selector, float delay)
to cancel the trigger.
The following code is used to test the scheduler:
HelloWorldScene.hvoid updateOnce(float dt);
HelloWorldScene.cppbool HelloWorld::init(){ ... scheduleOnce(schedule_selector(HelloWorld::updateOnce), 0.1f); return true;}void HelloWorld::updateOnce(float dt){ log("Once");}
This time in the console, you'll only see one output.
cocos2d: Once
(5) Scheduler (Scheduler)