版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
quick-cocos2d-x學習系列之十五狀態(tài)機代碼--createFiniteStateMachineself.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]STARTINGUP")end,onstart=function(event)self:log("[FSM]READY")end,onbeforewarn=function(event)self:log("[FSM]STARTEVENT:warn!",true)end,onbeforepanic=function(event)self:log("[FSM]STARTEVENT:panic!",true)end,onbeforecalm=function(event)self:log("[FSM]STARTEVENT:calm!",true)end,onbeforeclear=function(event)self:log("[FSM]STARTEVENT:clear!",true)end,onwarn=function(event)self:log("[FSM]FINISHEVENT:warn!")end,onpanic=function(event)self:log("[FSM]FINISHEVENT:panic!")end,oncalm=function(event)self:log("[FSM]FINISHEVENT:calm!")end,onclear=function(event)self:log("[FSM]FINISHEVENT::onButtonClicked(function()ifself.fsm_:canDoEvent("warn")thenself.fsm_:doEvent("warn")endend):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()ifself.fsm_:canDoEvent("panic")thenself.fsm_:doEvent("panic")endend):align(display.CENTER,display.cx+150,display.top-540):addTo(self)--debugself.logCount_=0程序開始時候設置狀態(tài)為green.函數log打印信息,根據打印狀態(tài)變化圖片。函數pengding打印狀態(tài)信息,并設置LABEL字符串。解釋創(chuàng)建有限狀態(tài)機--createFiniteStateMachineself.fsm_={}cc.GameObject.extend(self.fsm_):addComponent("components.behavior.StateMachine"):exportMethods()設置有限狀態(tài)機回調函數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]STARTINGUP")end,onstart=function(event)self:log("[FSM]READY")end,onbeforewarn=function(event)self:log("[FSM]STARTEVENT:warn!",true)end,onbeforepanic=function(event)self:log("[FSM]STARTEVENT:panic!",true)end,onbeforecalm=function(event)self:log("[FSM]STARTEVENT:calm!",true)end,onbeforeclear=function(event)self:log("[FSM]STARTEVENT:clear!",true)end,onwarn=function(event)self:log("[FSM]FINISHEVENT:warn!")end,onpanic=function(event)self:log("[FSM]FINISHEVENT:panic!")end,oncalm=function(event)self:log("[FSM]FINISHEVENT:calm!")end,onclear=function(event)self:log("[FSM]FINISHEVENT:clear!")end,onleavegreen=function(event)self:log("[FSM]LEAVESTATE:green")end,onleaveyellow=function(event)self:log("[FSM]LEAVESTATE:yellow")end,onleavered=function(event)self:log("[FSM]LEAVESTATE: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]ENTERSTATE:green")end,onyellow=function(event)self:log("[FSM]ENTERSTATE:yellow")end,onred=function(event)self:log("[FSM]ENTERSTATE:red")end,onchangestate=function(event)self:log("[FSM]CHANGEDSTATE:"..event.from.."to"..event.to)end,},})創(chuàng)建4個Button分別是:clear,calm,warn,panic。如clear按鈕self.clearButton_=cc.ui.UIPushButton.new():setButtonLabel(cc.ui.UILabel.new({text="clear",size=32,color=display.COLOR_BLACK})):onButtonClicked(function()ifself.fsm_:canDoEvent("clear")thenself.fsm_:doEvent("clear")endend):align(display.CENTER,display.cx-150,display.top-540):addTo(self)是否可以調用clear時間,如果可以則調用。其他3個按鈕類似。點擊panic按鈕后先調用函數onbeforepanic輸出[FSM]STARTEVENT:panic!然后調用onleavegreen=function(event)self:log("[FSM]LEA
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 保險公司財務審核崗位面試題集
- 律師職位應聘的面試常見問題解析及回答策略
- 交通物流規(guī)劃分析師面試問題集
- 高工面試題集及答案解析
- 2025年5G通信網絡布局可行性研究報告
- 2026屆浙江省七校聯盟高三上學期一模歷史試題(含答案)
- 2025年數字化營銷在企業(yè)轉型中的作用可行性研究報告
- 2025年城市綠地生態(tài)修復項目可行性研究報告
- 2025年旅游與運動結合的休閑項目可行性研究報告
- 協警服務協議書
- 土家族服飾講座3課件
- 高壓供電管理規(guī)定
- 項目監(jiān)理部監(jiān)理周報
- 探槽地質編錄工作方法
- 光伏工程資料表格模板
- GB/T 41123.2-2021無損檢測工業(yè)射線計算機層析成像檢測第2部分:操作和解釋
- GB/T 17636-1998土工布及其有關產品抗磨損性能的測定砂布/滑塊法
- GB/T 17612-1998封閉管道中液體流量的測量稱重法
- GB/T 10609.2-1989技術制圖明細欄
- 配電系統(tǒng)標識
- 基礎醫(yī)學概論復習講義
評論
0/150
提交評論