我有一個函數,我在畫布上使用,我試圖清除與.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