【quick-cocos2d-lua】 Finite State Machine
2018-06-20 10:00
267 查看
有限状态机:
例:
开始状态:
点击panic:
点击calm:
代码解析:
[code]local MainScene = class("MainScene", function() return display.newScene("MainScene") end) function MainScene:ctor() -- create Finite State Machine self.fsm_ = {} cc.GameObject.extend(self.fsm_) :addComponent("components.behavior.StateMachine") :exportMethods() self.fsm_:setupState({ events = { {name = "start", from = "none", to = "green" }, {name = "warn", from = "green", to = "yellow"}, {name = "panic", from = "green", to = "red" }, {name = "panic", from = "yellow", to = "red" }, {name = "calm", from = "red", to = "yellow"}, {name = "clear", from = "red", to = "green" }, {name = "clear", from = "yellow", to = "green" }, }, callbacks = { onbeforestart = function(event) self:log("[FSM] STARTING UP") end, onstart = function(event) self:log("[FSM] READY") end, onbeforewarn = function(event) self:log("[FSM] START EVENT: warn!", true) end, onbeforepanic = function(event) self:log("[FSM] START EVENT: panic!", true) end, onbeforecalm = function(event) self:log("[FSM] START EVENT: calm!", true) end, onbeforeclear = function(event) self:log("[FSM] START EVENT: clear!", true) end, onwarn = function(event) self:log("[FSM] FINISH EVENT: warn!") end, onpanic = function(event) self:log("[FSM] FINISH EVENT: panic!") end, oncalm = function(event) self:log("[FSM] FINISH EVENT: calm!") end, onclear = function(event) self:log("[FSM] FINISH EVENT: clear!") end, onleavegreen = function(event) self:log("[FSM] LEAVE STATE: green") end, onleaveyellow = function(event) self:log("[FSM] LEAVE STATE: yellow") end, onleavered = function(event) self:log("[FSM] LEAVE STATE: red") self:pending(event, 3) self:performWithDelay(function() self:pending(event, 2) self:performWithDelay(function() self:pending(event, 1) self:performWithDelay(function() self.pendingLabel_:setString("") event.transition() end, 1) end, 1) end, 1) return "async" end, ongreen = function(event) self:log("[FSM] ENTER STATE: green") end, onyellow = function(event) self:log("[FSM] ENTER STATE: yellow") end, onred = function(event) self:log("[FSM] ENTER STATE: red") end, onchangestate = function(event) self:log("[FSM] CHANGED STATE: " .. event.from .. " to " .. event.to) end, }, }) -- create UI display.newColorLayer(cc.c4b(255, 255, 255, 255)) :addTo(self) cc.ui.UILabel.new({ text = "Finite State Machine", size = 32, color = display.COLOR_BLACK }) :align(display.CENTER, display.cx, display.top - 60) :addTo(self) self.pendingLabel_ = cc.ui.UILabel.new({ text = "", size = 32, color = display.COLOR_BLACK, x = display.cx, y = display.top - 620, }) :align(display.CENTER) :addTo(self) -- preload texture self.stateImage_ = display.newSprite("#GreenState.png") :pos(display.cx, display.top - 300) :scale(1.5) :addTo(self) self.clearButton_ = cc.ui.UIPushButton.new() :setButtonLabel(cc.ui.UILabel.new({text = "clear", size = 32, color = display.COLOR_BLACK})) :onButtonClicked(function() if self.fsm_:canDoEvent("clear") then self.fsm_:doEvent("clear") end end) :align(display.CENTER, display.cx - 150, display.top - 540) :addTo(self) self.calmButton_ = cc.ui.UIPushButton.new() :setButtonLabel(cc.ui.UILabel.new({text = "calm", size = 32, color = display.COLOR_BLACK})) :onButtonClicked(function() if self.fsm_:canDoEvent("calm") then self.fsm_:doEvent("calm") end end) :align(display.CENTER, display.cx - 50, display.top - 540) :addTo(self) self.warnButton_ = cc.ui.UIPushButton.new() :setButtonLabel(cc.ui.UILabel.new({text = "warn", size = 32, color = display.COLOR_BLACK})) :onButtonClicked(function() if self.fsm_:canDoEvent("warn") then self.fsm_:doEvent("warn") end end) :align(display.CENTER, display.cx + 50, display.top - 540) :addTo(self) self.panicButton_ = cc.ui.UIPushButton.new() :setButtonLabel(cc.ui.UILabel.new({text = "panic", size = 32, color = display.COLOR_BLACK})) :onButtonClicked(function() if self.fsm_:canDoEvent("panic") then self.fsm_:doEvent("panic") end end) :align(display.CENTER, display.cx + 150, display.top - 540) :addTo(self) -- debug self.logCount_ = 0 end function MainScene:pending(event, n) local msg = event.to .. " in ..." .. n self:log("[FSM] PENDING STATE: " .. msg) self.pendingLabel_:setString(msg) end function MainScene:log(msg, separate) if separate then self.logCount_ = self.logCount_ + 1 end if separate then print("") end printf("%d: %s", self.logCount_, msg) local state = self.fsm_:getState() if state == "green" then self.stateImage_:setSpriteFrame(display.newSpriteFrame("GreenState.png")) elseif state == "red" then self.stateImage_:setSpriteFrame(display.newSpriteFrame("RedState.png")) elseif state == "yellow" then self.stateImage_:setSpriteFrame(display.newSpriteFrame("YellowState.png")) end -- self.clearButton_:setEnabled(self.fsm_:canDoEvent("clear")) -- self.calmButton_:setEnabled(self.fsm_:c 4000 anDoEvent("calm")) -- self.warnButton_:setEnabled(self.fsm_:canDoEvent("warn")) -- self.panicButton_:setEnabled(self.fsm_:canDoEvent("panic")) end function MainScene:onEnter() self.fsm_:doEvent("start") end return MainScene
阅读更多
相关文章推荐
- quick-cocos2d-x数据存储 UserDefault GameState io
- Quick-Cocos2d-x 2.26 用tolua导出C++类给lua调用WINDOWS篇例子2
- quick-cocos2d-x之testlua之mainMenu.lua
- Quick-Cocos2d-x 创建自定义Lua绑定C++类
- quick-cocos2d-x开发环境Lua for IntelliJ IDEA的安装
- The finite state machine
- Cocos2d-Lua(Quick-Cocos2d-x)集成第三方SDK(一)
- Cocos2d-X 中quick----lua的跳转场景的实现
- Finite State Machine library Introduction
- FSM (Finite State Machine Framework)源码共享
- 【Quick-COCOS2D-X 3.3 如何绑定自定义类至Lua之四】使用绑定C++至Lua的自定义类
- [quick-cocos2d-x lua学习] 各大IDE之体验及选择 ( LDT、Sublime Text 2、IntelliJ IDEA 12、XCode5 )
- 使用lua更新脚本后,xcode下游戏资源没有更新的问题[quick-cocos2d-x]
- 【quick-cocos2d-lua】 水果消除 FruitFest
- 【quick-cocos2d-lua】 UI控件
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(下)
- quick-cocos2d-x 中的 socket 技术选择:LuaSocket 和 WebSocket
- 在Mac+LDT中搭建quick-cocos2d-x的lua开发环境
- 有限状态机——The finite state machine
- Cocos2d-Lua(Quick-Cocos2d-x)集成第三方SDK(二)