cocos2d-lua 3.0~3.15通用關(guān)于狀態(tài)機(jī)使用的其他樣例 <1>

轉(zhuǎn)自泰然論壇出書
Cocos2d-x之Lua核心編程_配套代碼\chapter7\7.5 有限狀態(tài)機(jī)
關(guān)于3.3版本以后使用狀態(tài)機(jī) 請(qǐng)參考
http://www.itdecent.cn/p/cd5821fa8ebd

狀態(tài)機(jī)使用 非要多看例子 和源碼 方能領(lǐng)會(huì)其精髓

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function MainScene:ctor()
    cc(self):addComponent("components.behavior.StateMachine"):exportMethods()

    self:setupState({
        initial = "Stranger",
        events = {
            {name = "Fallinlove", from = "Stranger", to = "Lover" },
            {name = "Part", from = {"Lover", "Spouse"}, to = "Stranger"},
            {name = "Marry", from = {"Lover", "Stranger"}, to = "Spouse"}
        },

        callbacks = {
            -- event
            onbeforeFallinlove = function(event) print("Before Fallinlove: " .. event.from .. " to " .. event.to) end,
            onafterFallinlove = function(event) print("After Fallinlove: " .. event.from .. " to " .. event.to) end,
            onbeforePart = function(event) print("Before Part: " .. event.from .. " to " .. event.to) end,
            onafterPart = function(event) print("After Part: " .. event.from .. " to " .. event.to) end,
            onbeforeMarry = function(event) print("Before Marry: " .. event.from .. " to " .. event.to) end,
            onafterMarry = function(event) print("After Marry: " .. event.from .. " to " .. event.to) end,
            -- status
            onenterStranger = function(event) print("Enter Stranger: " .. event.from .. " to " .. event.to) end,
            onleaveStranger = function(event) print("Leave Stranger: " .. event.from .. " to " .. event.to) end,
            onenterLover = function(event) print("Enter Lover: " .. event.from .. " to " .. event.to) end,
            onleaveLover = function(event) print("Leave Lover: " .. event.from .. " to " .. event.to) end,
            onenterSpouse = function(event) print("Enter Spouse: " .. event.from .. " to " .. event.to) end,
            onleaveSpouse = function(event)
                print("Leave Spouse: " .. event.from .. " to " .. event.to)
                -- A state change takes time
                self:performWithDelay(function()
                    print("==Just Finish")
                    event.transition()
                end, 3)
                return "async"
            end,
            -- status change
            onchangestate = function(event) print("CHANGED STATE: " .. event.from .. " to " .. event.to) end
        },
    })

    -- event button
    cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "Fallinlove", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                if self:canDoEvent("Fallinlove") then
                    self:doEvent("Fallinlove")
                else
                    print("Can't do Fallinlove, in Status:%s", self:getState())
                end
            end)
            :align(display.CENTER, display.cx, display.cy + 100)
            :addTo(self)

    cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "Part", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                if self:canDoEvent("Part") then
                    self:doEvent("Part")
                else
                    print("Can't do Part, in Status:%s", self:getState())
                end
            end)
            :align(display.CENTER, display.cx, display.cy)
            :addTo(self)

    cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "Marry", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                if self:canDoEvent("Marry") then
                    self:doEvent("Marry")
                else
                    print("Can't do Marry, in Status:%s", self:getState())
                end
            end)
            :align(display.CENTER, display.cx, display.cy - 100)
            :addTo(self)
end

function MainScene:onEnter()
end

function MainScene:onExit()
end

return MainScene

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容