该方法用于向现有的状态机中添加一个新的状态。
stateMachine.add(stateName, stateConfig);
stateName
:String类型,需要添加的状态的名称。stateConfig
:Object类型,包含新状态的相关配置信息。无返回值。
const stateMachine = new StateMachine();
stateMachine.add("state1", {
onEnter: function() {
console.log("进入 state1");
},
onExit: function() {
console.log("离开 state1");
}
});
stateConfig
对象中可以包含多个回调函数,如 onEnter
,onExit
等。stateMachine.getStates()
方法查看当前状态机中所有状态的名称。