Cocos2d-x Lua StateMachine
onbeforeEvent->onleaveStatus->onenterStatus->onchangeStatus->onafterEvent
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
function MainScene:ctor()
cc(self):addComponent("components.behavior.StateMachine"):exportMethods()
self:setupState({
initial = "NewOne",
events = {
{name = "Grade1", from = "NewOne", to = "Grade1er" },
{name = "Grade2", from = "Grade1er", to = "Grade2er"},
{name = "Grade3", from = "Grade2er", to = "Grade3er"},
{name = "Tuixue", from = {"Grade1er","Grade2er", "Grade3er"}, to = "NewOne"}
},
callbacks = {
-- event
onbeforeGrade1 = function(event) print("Before Grade1: " .. event.from .. " to " .. event.to) end,
onafterGrade1 = function(event) print("After Grade1: " .. event.from .. " to " .. event.to) end,
onbeforeGrade2 = function(event) print("Before Grade2: " .. event.from .. " to " .. event.to) end,
onafterGrade2 = function(event) print("After Grade2: " .. event.from .. " to " .. event.to) end,
onbeforeGrade3= function(event) print("Before Grade3: " .. event.from .. " to " .. event.to) end,
onafterGrade3 = function(event) print("After Grade3: " .. event.from .. " to " .. event.to) end,
onbeforeTuixue= function(event) print("Before Tuixue: " .. event.from .. " to " .. event.to) end,
onafterTuixue = function(event) print("After Tuixue: " .. event.from .. " to " .. event.to) end,
-- status
onenterNewOne = function(event) print("Enter NewOne: " .. event.from .. " to " .. event.to) end,
onleaveNewOne = function(event) print("Leave NewOne: " .. event.from .. " to " .. event.to) end,
onenterGrade1er = function(event) print("Enter Grade1er: " .. event.from .. " to " .. event.to) end,
onleaveGrade1er = function(event) print("Leave Grade1er: " .. event.from .. " to " .. event.to) end,
onenterGrade2er = function(event) print("Enter Grade2er: " .. event.from .. " to " .. event.to) end,
onleaveGrade2er = function(event) print("Leave Grade2er: " .. event.from .. " to " .. event.to) end,
onleaveGrade3er = function(event) print("Enter Grade3er: " .. event.from .. " to " .. event.to) end,
onleaveGrade3er = function(event)
print("Leave Grade3er: " .. event.from .. " to " .. event.to)
-- A state change takes time
self:performWithDelay(function()
print("==Just Finish")
event.transition()
end, 3)
return "async"
end,
},
})
-- event button
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "Grade1", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
if self:canDoEvent("Grade1") then
self:doEvent("Grade1")
else
print("Can't do Grade1, 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 = "Grade2", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
if self:canDoEvent("Grade2") then
self:doEvent("Grade2")
else
print("Can't do Grade2, 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 = "Grade3", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
if self:canDoEvent("Grade3") then
self:doEvent("Grade3")
else
print("Can't do Grade3, 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 = "Tuixue", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
if self:canDoEvent("Tuixue") then
self:doEvent("Tuixue")
else
print("Can't do Tuixue, in Status:%s", self:getState())
end
end)
:align(display.CENTER, display.cx, display.cy - 200)
:addTo(self)
end
function MainScene:onEnter()
end
function MainScene:onExit()
end
return MainScene