quick StateMachine 状态机的使用

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。