[Cocos2d-html5 game engine learning notes (12)] schedule timer

Source: Internet
Author: User

I haven't updated the Cocos2d-html5 blog for a long time, these two months are busy with some games, so it is delayed, hey, but there are still a lot of chapters to write about this column, so I am going to update it slowly.

 

Schedule is a "timetable" in terms of translation. It is almost a scheduled plan. The scenario used in cocos2d is generally like how many times you want a genie to execute an animation at intervals, or if you want to perform the jump for a long time in a scenario, OK should be used. The following is a simple example of this usage. Because this function is executed at intervals, it is difficult to display the screen, so I will explain it using code and text.

 

1. schedule (callback_fn, interval, repeat, delay)

The meanings of the four parameters are as follows:

Callback_fn: name of the called Method

Interval: Call interval

Repeat: number of repeated times

Delay: how long will the call be delayed?

 

2. scheduleOnce (callback_fn, delay)

This function only calls the callback_fn method once.

 

3. scheduleUpdate ()
This function is called every frame. The called method is "update"


Note: 1. the time units involved here are "seconds"!
2. For the method to be called, remember to have a float type parameter. To put it bluntly, add a parameter and the javascript parameter types are automatically recognized.

 

var Helloworld = cc.Layer.extend({    init:function () {        this._super();        var size = cc.Director.getInstance().getWinSize();        this.schedule(this.timeCallback,2,2,1);        this.scheduleOnce(this.timeCallback,2);        this.scheduleUpdate();        return true;    },    timeCallback:function(dt) {    },    update:function(dt){    },});

The schedule (callback_fn, interval, repeat, delay) function does not need to take all four parameters every time. It has a heavy load.

 

There are two other forms:

Schedule (callback_fn) is called once every frame. It is called countless times and immediately
Schedule (callback_fn, interval) is called once at intervals. It is called countless times and immediately

 

Therefore, you need to use functions and parameters as needed.

 

Of course, we can not only execute the timer, but also stop our timer at any time according to our own ideas. There are several methods:

Unschedule (callback_fn) stops the specified timer method

UnscheduleAllCallbacks () Stop all timer

UnscheduleUpdate () Stop update Method

 

The timer content is almost the same. My biggest feeling is that, for example, there was a 321 countdown before the game started, which can be used here, or the logo is displayed for a certain period of time to jump to our main menu interface, the timer can be used in these areas.

 

 

Related Article

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.