(5) Scheduler (Scheduler)

Source: Internet
Author: User

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:

    1. The scheduler stops whenever node is no longer visible or has been removed from the scene.
    2. The scheduler will also stop when the cocos2d-x is paused. When Cocos2d-x restarts, the scheduler will automatically continue to start.
    3. 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:

    1. Default Scheduler:schedulerUpdate()
    2. Custom Scheduler:schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
    3. 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:

    1. The first parameter, selector, is the event function you want to add.
    2. The second parameter interval is the event firing interval
    3. 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
    4. 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)

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.