quick 状态机StateMachine

function Player:addStateMachine()
    self.fsm_ = {}
    cc.GameObject.extend(self.fsm_)
    :addComponent("components.behavior.StateMachine")
    :exportMethods()

    self.fsm_:setupState({
        -- 初始状态
        initial = "idle",

        -- 事件和状态转换
        events = {
            -- t1:clickScreen; t2:clickEnemy; t3:beKilled; t4:stop
            {name = "clickScreen", from = {"idle", "attack"},   to = "walk" },
            {name = "clickEnemy",  from = {"idle", "walk"},  to = "attack"},
            {name = "beKilled", from = {"idle", "walk", "attack", "hit"},  to = "dead"},
            {name = "beHit", from = {"idle", "walk", "attack"}, to = "hit"},
            {name = "stop", from = {"walk", "attack", "hit"}, to = "idle"},
        },

        -- 状态转变后的回调
        callbacks = {
            onidle = function (event) self:idle() end,
            onattack = function (event) self:attackEnemy() end,
            onhit = function (event) self:hit() end,
            ondead = function (event) self:dead() end
        },
    })

end

 那我发一个clickScene的事件,它判断如果是idle attack状态的话就执行walk状态 跟if else一样 只是系统帮你写了if else了
 用事件来驱动的状态迁移 
 


posted on 2014-11-19 14:05  防空洞123  阅读(498)  评论(0编辑  收藏  举报

导航