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
转载 用事件来驱动的状态迁移
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章