我試圖做一個狀態機,但它不工作。我有這個代碼至今:對象找不到方法
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;
}
};
}
當我嘗試運行它螢火顯示此錯誤:「類型錯誤:that.changeState不是一個函數」在更新功能就行了。當我取消註釋changeState()行時,它開始抱怨EaselJS庫不正確(我知道這是正確的,因爲它適用於我的其他項目)。有人可以幫助我嗎?這可能很簡單(就像往常一樣),但我不能發現錯誤。如果你們喜歡,我可以發佈其餘的代碼,但我認爲它不相關。
在此先感謝!
您是否試過從您的所有函數定義中刪除'new'關鍵字... – searlea 2013-04-09 20:48:43
如何創建一個新的狀態機?它應該可以工作,如果你這樣做:'var machine = new makeStateMachine();' – Bart 2013-04-09 20:48:54