cocos2d-js浅谈schedule的用法
2016-09-03 16:52
381 查看
展性及实用性上,建议大家统一使用cocos2d自身提供的schedule方法。下面就为大家深入浅出的介绍下schedule:
2. scheduleOnce(callback, delay, key):
Parameters:
{function} callback
A function wrapped as a selector
{Number} delay
The amount of time that the first tick will wait before execution.
{String} key
The only string identifying the callback
3. schedule(callback, interval, repeat, delay, key):
Parameters:
{function} callback
A function wrapped as a selector
{Number} interval
Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
{Number} repeat
The selector will be executed (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
{Number} delay
The amount of time that the first tick will wait before execution.
{String} key
The only string identifying the callback
[javascript]
view plain
copy
this.scheduleUpdate(); //开启每帧调用,对应update
接着重写layer的update方法:(这里先定义了一个layer的成员变量time)
[javascript]
view plain
copy
update: function(dt) {
this.time += dt; //dt 为上一帧到当前帧的时长
if (this.time >= 3) {
cc.log("每3秒在调试框中输出一次");
this.time = 0;
};
},
[javascript]
view plain
copy
//延迟2秒后,只执行一次
this.scheduleOnce(this.once, 2);
接着新建一个once的方法:
[javascript]
view plain
copy
once: function() {
cc.log("延迟2秒后,只执行一次");
},
[javascript]
view plain
copy
//不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
bsp; this.schedule(this.notParameter, 2, 20, 5);
接着新建一个notParameter的方法:
[javascript]
view plain
copy
notParameter: function() {
cc.log("不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
},
view plain
copy
var test = cc.Layer.extend({
time: 0,
ctor: function() {
this._super();
this.time = 0;
this.scheduleUpdate(); //开启每帧调用,对应update
//不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
this.schedule(this.notParameter, 2, 20, 5);
//带参数的回调函数haveParameter,每1.5秒执行一次,累积执行10次
this.schedule(function nothing() {
this.haveParameter("带参数")
}, 1.5, 10);
//每5秒执行一次,无次数限制
this.schedule(this.notRepeat, 5);
//延迟2秒后,只执行一次
this.scheduleOnce(this.once, 2);
return true;
},
notRepeat: function() {
cc.log("每5秒执行一次");
},
notParameter: function() {
cc.log("不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
},
haveParameter: function(s) {
cc.log(s + "的回调函数haveParameter,每1.5秒执行一次,累积执行10次");
},
once: function() {
cc.log("延迟2秒后,只执行一次");
},
update: function(dt) {
this.time += dt; //dt 为上一帧到当前帧的时长
if (this.time >= 3) {
cc.log("每3秒在调试框中输出一次");
this.time = 0;
};
},
onExit: function() {
this._super();
this.unscheduleUpdate(); //移除schedule
this.unschedule(this.notRepeat); //移除没有次数限制的schedule
this.unscheduleAllCallbacks();//移除所有schedule的回调函数
}
});
一、schedule 有三种不同的api:
1. scheduleUpdate():2. scheduleOnce(callback, delay, key):
Parameters:
{function} callback
A function wrapped as a selector
{Number} delay
The amount of time that the first tick will wait before execution.
{String} key
The only string identifying the callback
3. schedule(callback, interval, repeat, delay, key):
Parameters:
{function} callback
A function wrapped as a selector
{Number} interval
Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead.
{Number} repeat
The selector will be executed (repeat + 1) times, you can use kCCRepeatForever for tick infinitely.
{Number} delay
The amount of time that the first tick will wait before execution.
{String} key
The only string identifying the callback
二、schedule三种api的使用:
1. scheduleUpdate()
以layer为例,在初始化的时候(ctor函数return true 前,其他地方也可以,建议在初始化时)添加:[javascript]
view plain
copy
this.scheduleUpdate(); //开启每帧调用,对应update
接着重写layer的update方法:(这里先定义了一个layer的成员变量time)
[javascript]
view plain
copy
update: function(dt) {
this.time += dt; //dt 为上一帧到当前帧的时长
if (this.time >= 3) {
cc.log("每3秒在调试框中输出一次");
this.time = 0;
};
},
2. scheduleOnce(callback, delay, key)
以layer为例,在初始化或其他非解析方法内添加:[javascript]
view plain
copy
//延迟2秒后,只执行一次
this.scheduleOnce(this.once, 2);
接着新建一个once的方法:
[javascript]
view plain
copy
once: function() {
cc.log("延迟2秒后,只执行一次");
},
3. schedule(callback, interval, repeat, delay, key)
同样以layer为例,在初始化或其他非解析方法内添加:[javascript]
view plain
copy
//不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
bsp; this.schedule(this.notParameter, 2, 20, 5);
接着新建一个notParameter的方法:
[javascript]
view plain
copy
notParameter: function() {
cc.log("不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
},
三、schedule的代码示例:
[javascript]view plain
copy
var test = cc.Layer.extend({
time: 0,
ctor: function() {
this._super();
this.time = 0;
this.scheduleUpdate(); //开启每帧调用,对应update
//不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
this.schedule(this.notParameter, 2, 20, 5);
//带参数的回调函数haveParameter,每1.5秒执行一次,累积执行10次
this.schedule(function nothing() {
this.haveParameter("带参数")
}, 1.5, 10);
//每5秒执行一次,无次数限制
this.schedule(this.notRepeat, 5);
//延迟2秒后,只执行一次
this.scheduleOnce(this.once, 2);
return true;
},
notRepeat: function() {
cc.log("每5秒执行一次");
},
notParameter: function() {
cc.log("不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
},
haveParameter: function(s) {
cc.log(s + "的回调函数haveParameter,每1.5秒执行一次,累积执行10次");
},
once: function() {
cc.log("延迟2秒后,只执行一次");
},
update: function(dt) {
this.time += dt; //dt 为上一帧到当前帧的时长
if (this.time >= 3) {
cc.log("每3秒在调试框中输出一次");
this.time = 0;
};
},
onExit: function() {
this._super();
this.unscheduleUpdate(); //移除schedule
this.unschedule(this.notRepeat); //移除没有次数限制的schedule
this.unscheduleAllCallbacks();//移除所有schedule的回调函数
}
});
相关文章推荐
- Cocos2d-JS schedule用法
- Cocos2d-JS schedule用法
- cocos2d-js 越来越慢的定时器schedule 制作不变慢的定时器
- 浅谈JS中this的几种用法
- cocos2d-js下node的暂停schedule
- cocos2d-js cc.DrawNode用法示例
- 浅谈JS中的!=、== 、!==、===的用法和区别
- 浅谈js数组和splice的用法
- 浅谈JS中的!=、== 、!==、===的用法和区别
- [cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)
- 浅谈js之字面量、对象字面量的访问、关键字in的用法
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
- 浅谈JS中逗号运算符的用法
- cocos2d-js cc.RenderTexture几种用法(数字图片、刮刮乐效果)
- Cocos2d-x schedule用法
- 浅谈JS中中括号([])用法
- cocos2d-js listView用法
- js中SetInterval与setTimeout用法
- 常用的JS与XML结合用法 简单示例
- 基础的prototype.js常用函数及其用法