码迷,mamicode.com
首页 > 系统相关 > 详细

quick StateMachine 状态机的使用

时间:2015-03-31 18:05:22      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:quick   状态机   

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

quick StateMachine 状态机的使用

标签:quick   状态机   

原文地址:http://blog.csdn.net/u012102504/article/details/44782113

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!