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

quick 状态机StateMachine

时间:2014-11-19 15:50:21      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   on   div   log   cti   

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了
 用事件来驱动的状态迁移 
 


quick 状态机StateMachine

标签:style   blog   io   color   sp   on   div   log   cti   

原文地址:http://www.cnblogs.com/as3lib/p/4108121.html

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