【发布时间】:2013-10-05 18:26:57
【问题描述】:
我正在尝试计算玩家按住鼠标按钮的时间。我试过了,但没有用:
var Game = cc.Layer.extend({
count: false,
countTimer: null,
init: function () {
var selfPointer = this;
this.canvas.addEventListener('mousedown', function(evt) {
selfPointer.count = true;
selfPointer.countTimer = window.setTimeout(selfPointer.Count(), 1);
});
this.canvas.addEventListener('mouseup', function(evt) {
selfPointer.count= false;
window.clearTimeout(selfPointer.countTimer);
});
},
Count: function() {
if (this.count)
{
window.setTimeout(this.Count(), 1);
}
}
这是我的代码的一部分(为简洁起见),如果玩家按住按钮,我想在任何 1 毫秒内执行一个动作。
此外,这行不通,我认为这比我的方法更好。有什么想法吗?
【问题讨论】:
-
js 时钟通常比 1ms 长很多,所以你不能经常执行代码。 10ms 仍然是 1/100 秒,大约是间隔和超时的最小粒度。我相信 IE 是 11.4 毫秒,而 chrome 接近 8 毫秒。对于大多数游戏来说,即使是 12 毫秒的间隔也足够了;能够达到 ~90FPS。
-
@dandavis 很好的信息!不知道。谢谢
标签: javascript cocos2d-iphone cocos2d-html5