1.scheduleUpdate

节点中有scheduleUpdate接口,通过这个接口,可以让游戏在每帧执行都执行update方法

var ScheduleUpdateLayer = cc.Layer.extend({
ball:null,
ctor:function () {
this._super();
this.scheduleUpdate(); // 开启定时器 var winSize = cc.director.getWinSize();
var ball = new cc.Sprite("res/item_2.png");
ball.x = winSize.width/2;
ball.y = winSize.height/2;
this.addChild(ball);
this.ball = ball; cc.eventManager.addListener({ // 监听鼠标事件
event:cc.EventListener.MOUSE,
onMouseDown:function (event) {
var action = cc.moveTo(1,event.getLocation().x,event.getLocation().y);
ball.runAction(action);
}
},this)
}, update : function () { // 重写update方法
console.log(this.ball.x+"---"+this.ball.y);
}
})

2. scheduleOnce

scheduleOnce和setTimeout类似,接受两个参数,第一个参数是回调函数,第二个参数是事件,scheduleOnce接受的时间以秒为单位。
节点都有scheduleOnce接口。

var ScheduleLayer = cc.Layer.extend({
ctor:function () {
this._super(); this.scheduleOnce(function () { // 2秒后打印日志
console.log("scheduleOnce");
},2);
}
})

3. schedule

schedule和setInterval类似,实现固定时间间隔不断触发某个函数的功能。
node.schedul(callback, interval, repeat, delay)
interval触发间隔,以秒为单位
repeat重复次数,会执行repeat+1次
delay是第一次出发前的延迟时间,以秒为单位
如果希望schedule无限循环,可以省略后两个参数,也可以设置repeat为常量cc.REPEATE_FOREVER

this.schedule(function () {
console.log("schedule");
},2,cc.REPEAT_FOREVER,2);

schedule基于帧数控制,当帧频降低时,schedule会积累大量的误差
一个平衡的定时器

schedule2:function (callback,interval) {
var then = Date.now();
interval = interval*1000;
this.schedule(function () {
var now = Date.now();
var delta = now-then;
if(delta > interval){
then = now - (delta % interval); //如果本次触发延迟了,就让下次触发早一点来抵消误差
callback.call(this);
}
}.bind(this),0); // 0表示每帧触发
}

4. 取消定时器

  • 取消scheduleUpdate ,使用 node.unscheduleUpdate()
  • 取消scheduleOnce和schedule,使用node.unschedule()
var ScheduleLayer = cc.Layer.extend({
ctor:function () {
this._super();
this.schedule(this.tick,1,cc.REPEAT_FOREVER,1);
this.tickCount = 0;
},
tick:function () {
console.log("tick");
this.tickCount++;
if(this.tickCount == 5){
this.unschedule(this.tick);
}
}
})

5.暂停/恢复定时器

node.pause();  //暂停
node.resume(); //恢复

作者:写java的逗比叫z1
链接:http://www.jianshu.com/p/df26c8ef1671

cocos2d-js 定时器的更多相关文章

  1. js定时器的使用(实例讲解)

    在javascritp中,有两个关于定时器的专用函数,分别为: 1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...

  2. 移动Web与js定时器暂停或不准确计时的问题解决

    PC 上的 Firefox.Chrome 和 Safari 等浏览器,都会自动把未激活页面中的 JavaScript 定时器(setTimeout.setInterval)间隔最小值改为 1 秒以上: ...

  3. js定时器 特定时间执行某段程序的例子

    定时器想必大家并不陌生吧,在本文为大家详细介绍下js中是如何实现定时器的,具体原理及代码如下. 例子: $(function(){ var handler = function(){ //www.jb ...

  4. js定时器setInterval()与setTimeout()

    js定时器setInterval()与setTimeout() 1.setTimeout(Expression,DelayTime),在DelayTime过后,将执行一次Expression,setT ...

  5. C#-WebForm JS定时器

    JS定时器: 1.window.setTimeout(function(){},3000) 延迟3秒执行 2.window.setInterval(function(){},3000) 也叫重复器,每 ...

  6. cocos2d js jsb XMLHttpRequest 中文乱码

    1.首先讲下怎样使用XMLHttpRequest 下面所说的是在cocos2d-x 2.2.2 或者 2.3 版本号中. 首先要明确cocos2d js事实上分两个版本号,一个是html5的版本号,另 ...

  7. cocos2d js的一些tip

    cocos2d-js-v3.2-rc0 cc.director.end();//退出app cc.Application.getInstance().openURL("http://www. ...

  8. Vue清除所有JS定时器

    Vue清除所有JS定时器 在webpack + vue 的项目中如何在页面跳转的时候清除所有的定时器 JS定时器会有一个返回值(数字),通过这个返回值我们可以找到这个定时器 在vue项目中可以使用路由 ...

  9. cocos2d js ClippingNode 制作标题闪亮特效

    1.效果图: 之前在<Android 高仿 IOS7 IPhone 解锁 Slide To Unlock>中制作了文字上闪亮移动的效果,这次我们来看下怎样在cocos2d js 中做出类似 ...

  10. js定时器关闭,js定时器停止,一次关闭所有正在运行的定时器,自定义函数clearIntervals()一次关闭所有正在运行的定时器

    js定时器关闭,一次关闭所有正在运行的定时器,自定义函数clearIntervals()一次关闭所有正在运行的定时器,原理:利用数组存储定时器id,然后遍历数组,关闭定时器 附上页面的截图,代码在截图 ...

随机推荐

  1. Mac开机启动

    1. Finder打开资源库的LaunchAgents目录. 打开Finder,按⇧⌘G,输入 /Library/LaunchAgents/ 以及 ~/Library/LaunchAgents/ 2. ...

  2. 05-THREE.JS 产生大雾的效果

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  3. hbase_异常_04_util.FSUtils: Waiting for dfs to exit safe mode...

    一.异常现象 启动hbase的时,hbase的日志中可以发现: Waiting for dfs to exit safe mode... 然后就抛异常了 2018-03-22 17:00:28,994 ...

  4. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. L117

    Hoover has become a household word for a vacuum cleaner through the world.Economics are slowly killi ...

  6. python主函数

    Python的人会很不习惯Python没有main主函数. 这里简单的介绍一下,在Python中使用main函数的方法 #hello.py def foo(): str="function& ...

  7. 2016 ACM-ICPC 区域赛(大连站)题解

    题目链接 A - Wrestling Match (二分图染色) 题意略坑(没有说好的玩家一定能打过差的玩家啊啊~~) 典型的二分图染色问题,每个玩家看成一个点,把相互较量过的玩家之间连边,好的玩家染 ...

  8. UVA - 11922 Permutation Transformer (splay)

    题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...

  9. bzoj 4712 洪水——动态DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4712 因为作为动态DP练习而找到,所以就用动态DP做了,也没管那种二分的方法. 感觉理解似乎 ...

  10. 由于簇计数比预计的高,格式化操作无法完成——Allocation Unit Size Adjustments for Larger NTFS Volumes.

    Allocation Unit Size Adjustments for Larger NTFS Volumes.   Problem: When trying to format a new vol ...