我试图做一个状态机,但它不工作。我有这个代码至今:对象找不到方法
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