[英]Object can't find method
我正在嘗試制作一個狀態機,但是沒有成功。 到目前為止,我已經獲得了以下代碼:
function makeStateMachine() {
this.stateConstructors = new Object();
this.currState = {
update : function(e) {
// Nothing to do here
},
exit : function() {
// Nothing to declare
}
};
this.nextState = null;
var that = this;
this.update = new function(e) {
that.currState.update(e);
that.changeState();
};
this.setNextState = new function(targetState) {
that.nextState = targetState;
};
this.addState = new function(constructor, stateName) {
that.stateConstructors[stateName] = constructor;
};
this.changeState = new function() {
if (that.nextState != null) {
that.currState.exit();
that.currState = new that.stateConstructors[that.nextState]();
that.nextState = null;
}
};
}
當我嘗試運行它時,firebug在更新函數的行上顯示以下錯誤:“ TypeError:that.changeState不是函數”。 當我取消注釋changeState()行時,它開始抱怨EaselJS庫不正確(我知道這是正確的,因為它適用於我的其他項目)。 有人可以幫我嗎? 這可能很簡單(就像往常一樣),但我無法發現錯誤。 如果你們願意,我可以發布其余代碼,但我認為這無關緊要。
提前致謝!
您應該將這些功能放在原型中。 你也應該不會使用= new function(...
;只需使用= function(...
最后,你不需要。 that
試試這個代碼:。
function makeStateMachine() {
this.stateConstructors = {};
this.currState = {
update : function(e) {
// Nothing to do here
},
exit : function() {
// Nothing to declare
}
};
this.nextState = null;
}
makeStateMachine.prototype.update = function(e) {
this.currState.update(e);
this.changeState();
};
makeStateMachine.prototype.setNextState = function(targetState) {
this.nextState = targetState;
};
makeStateMachine.prototype.addState = function(constructor, stateName) {
this.stateConstructors[stateName] = constructor;
};
makeStateMachine.prototype.changeState = function() {
if (this.nextState != null) {
this.currState.exit();
this.currState = new this.stateConstructors[this.nextState]();
this.nextState = null;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.