我有一个函数,我在画布上使用,我试图清除与.animate
函数调用的时间间隔,但是当我打电话给.unbind();
时,它仍然记录未定义,当它记录超时值时,我不确定为什么它不工作,也许你们可以帮助为什么'这'不是它在这种情况下应该是什么?
function Character(model, srcX, srcY, srcW, srcH, destX, destY, destW, destH) {
this.model = model;
this.srcX = srcX;
this.srcY = srcY;
this.srcW = srcW;
this.srcH = srcH;
this.destX = destX;
this.destY = destY;
this.destW = destW;
this.destH = destH;
this.timeout = undefined;
}
Character.prototype = {
draw: function() {
return ctx.drawImage(this.model, this.srcX, this.srcY, this.srcW, this.srcH,
this.destX, this.destY, this.destW, this.destH);
},
animate: function(claymation) {
var top = this; <<<<<--------Set the this variable
var queue = (function() {
var that = this;
var active = false;
if (!this.active) {
(function runQueue(i) {
that.active = true;
var length = claymation.length -1;
>>>>-Set the timeout top.timeout = setTimeout(function() {
claymation[i].action();
if (i < length) {
runQueue(i + 1);
if (i === length - 1) {
that.active = false;
}
}
}, claymation[i].time);
})(0);
}
})();
return queue;
},
update: function(callback) {
callback();
},
unbind: function() {
console.log(this.timeout); < Logs undefined
clearTimeout(this.timeout);
console.log(this.timeout); < Also logs undefined?
}
}
更新:
我打电话解除绑定上:
player = new Character(playerModel, 0, 130, 100, 100, 150, 150, 100, 100)
if (e.which === 39) {
player.unbind();
key = undefined;
}
完整的源代码:https://github.com/Gacnt/FirstGame/blob/master/public/javascripts/superGame.js#L50-L77
你在哪里/如何调用'.unbind'?您可以通过阅读关于“this”的工作方式来找到解决方案:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this。 – 2013-04-28 23:22:53
函数的* this *由调用设置,您需要显示如何调用* unbind *。 – RobG 2013-04-28 23:23:49
@RobG更新.. – Datsik 2013-04-28 23:24:46