cocos2d-lua 3.10 框架 状态机 具体创建细则 各个参数有详细说明

    self.fsm_ = {}
    cc(self.fsm_)
        :addComponent("components.behavior.StateMachine")
        :exportMethods()
    self.fsm_:setupState({
        --defer 翻译:延迟 状态机初始状态为none 如果这个为false 则状态自动none->normal 反之需setupState结束后 doEvent("normal")
        --{name = "startup",   from = "none",  to = "normal"}, --下面这句可以这么理解
        initial = {state = "normal", event = "startup", defer = false},
        events = {
            {name = "disable", from = {"normal", "pressed"}, to = "disabled"},
            {name = "enable",  from = {"disabled"}, to = "normal"},
            {name = "press",   from = "normal",  to = "pressed"},
            {name = "release", from = "pressed", to = "normal"},
        },
        callbacks = {
    
            -- 在任何事件开始前被激活
            onbeforeevent = handler(self, function() print("111") end),
            -- 在离开任何状态时被激活
            onleavestate = handler(self, function() print("222") end),
            -- 在进入任何状态时被激活
            onenterstate = handler(self, function() print("333") end),
            -- 当状态发生改变的时候被激活
            onchangestate = handler(self, function() print("444") end),
            -- 在任何事件结束后被激活
            onafterevent = handler(self, function() print("555") end),

            --这里是拆开的onbefore onafter onleave onenter是事件名 需要组合上状态名normal 或startup
            onbeforenormal = handler(self, function() print("666") end),
            onafternormal = handler(self, function() print("777") end),
            onleavenormal = handler(self, function() print("888") end),
            onenternormal = handler(self, function() print("999") end),

            onbeforestartup = handler(self, function() print("10") end),
            onafterstartup = handler(self, function() print("11") end),
            onleavestartup = handler(self, function() print("12") end),
            onenterstartup = handler(self, function() print("13") end),

            onbeforepress = handler(self, function() print("14") end),
            onafterpress = handler(self, function() print("15") end),
            onleavepressed = handler(self, function() print("16") end),
            onenterpressed = handler(self, function() print("17") end),
            
        },
        terminal = "normal" --与to的状态做比较 相关函数 self.fsm_:isFinishedState()是否最终状态
    })

    --initial = {state = "normal", event = "startup", defer = false},
    --打印结果
    -- [LUA-print]  10  -- startup 开始前事件
    -- [LUA-print]  111 -- 公共的 开始前 事件
    -- [LUA-print]  222 -- 离开 none 状态被激活
    -- [LUA-print]  999 -- 进入到 normal 状态被激活
    -- [LUA-print]  333 -- 公共的 进入任何状态被激活
    -- [LUA-print]  444 -- 公共的 状态发生改变被激活
    -- [LUA-print]  11  -- startup 结束事件
    -- [LUA-print]  555 -- 公共的 在任何事件结束后结束

你可能感兴趣的:(cocos2d-lua 3.10 框架 状态机 具体创建细则 各个参数有详细说明)