转自泰然论坛出书
Cocos2d-x之Lua核心编程_配套代码\chapter7\7.5 有限状态机
关于3.3版本以后使用状态机 请参考
http://www.jianshu.com/p/cd5821fa8ebd
状态机使用 非要多看例子 和源码 方能领会其精髓
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