quick 的状态机 真是使用简单,功能强大,记录一下使用方式便于以后使用
1:创建一个状态机StateMachine
(1) self.fsm_ = StateMachine.new()
(2)self:addComponent(“component.behavior.StateMachine”)
self.fsm_ = self:getComponent(“component.behavior.StateMachine”)
2:setupState
self.fsm_:setupState({
events= {
{name = "start", from = "none", to = "green" },
{name = "warn", from = "green", to = "yellow"},
{name = "panic", from = "green", to = "red" },
{name = "panic", from = "yellow", to = "red" },
{name = "calm", from = "red", to = "yellow"},
{name = "clear", from = "red", to = "green" },
{name = "clear", from = "yellow", to = "green" },
},
callbacks = {
onbeforestart = function(event) self:log() end,
onstart = function(event) self:log() end,
onwarn = function(event) end,
...
onchangestate = function(event) print(event.from,event.to) end
}
})
执行状态跳转
if self.fsm_:canDoEvent("clear") then
self.fsm_:doEvent("clear")
end
注意一开始一定要doEvent(“start”) 状态从none 转为 green状态
更详细的一些函数使用参照http://www.it165.net/pro/html/201403/10111.html
时间: 2024-10-08 15:20:43