quick-cocos2d-x 学习系列之十五 状态机

quick-cocos2d-x 学习系列之十五 状态机,第1张

概述quick-cocos2d-x 学习系列之十五 状态机     1.  代码 -- create Finite StateMachine     self.fsm_ = {}     cc.GameObject.extend(self.fsm_)         :addComponent("components.behavior.StateMachine")         :exportMet

quick-cocos2d-x 学习系列之十五 状态机

1.代码

-- create Finite StateMachine

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",to = "red" },from = "yellow",

{name = "calm",from = "red",

{name = "clear",

},

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] STARTEVENT: panic!",

onbeforecalm = function(event) self:log("[FSM]START EVENT: calm!",

onbeforeclear = function(event) self:log("[FSM] STARTEVENT: clear!",

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] LEAVESTATE: 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)

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,

})

-- createUI

display.newcolorLayer(cc.c4b(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 = "",

color = display.color_BLACK,

x = display.cx,

y = display.top - 620,

})

:align(display.CENTER)

:addTo(self)

-- preloadtexture

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",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",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",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

程序开始时候设置状态为green.

1.1函数log

打印信息,根据打印状态变化图片。

1.2函数pengding

打印状态信息,并设置LABEL字符串。

2.解释

创建有限状态机

-- createFinite State Machine

self.fsm_ = {}

cc.GameObject.extend(self.fsm_)

:addComponent("components.behavior.StateMachine")

:exportMethods()

设置有限状态机回调函数

self.fsm_:setupState({

events = {

{name = "start",

oncalm =function(event) self:log("[FSM] FINISH EVENT: calm!") end,

})

创建4个button

分别是:clear,calm,warn,panic。

如clear按钮

self.clearbutton_ =

cc.ui.UIPushbutton.new()

:setbuttonLabel(cc.ui.UILabel.new({text= "clear",display.top - 540)

:addTo(self)

是否可以调用clear时间,如果可以则调用。其他3个按钮类似。

点击panic按钮后

先调用函数onbeforepanic 输出 [FSM] START EVENT: panic!

然后调用

onleavegreen = function(event) self:log("[FSM]LEAVE STATE: green") 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,

最后调用

onpanic = function(event) self:log("[FSM]FINISH EVENT: panic!") end,

完成一次状态变化。

总结

以上是内存溢出为你收集整理的quick-cocos2d-x 学习系列之十五 状态机全部内容,希望文章能够帮你解决quick-cocos2d-x 学习系列之十五 状态机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1050179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存