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

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

},

})

-- createUI

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)

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

程序开始时候设置状态为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", 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,

},

})

创建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()

if
self.fsm_:canDoEvent("clear")
then

self.fsm_:doEvent("clear")

end

end)

:align(display.CENTER, display.cx-
150, 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,

完成一次状态变化。

时间: 2024-11-05 08:42:18

quick-cocos2d-x 学习系列之十五 状态机的相关文章

WP8.1学习系列(第二十五章)——控件样式

XAML 框架提供许多自定义应用外观的方法.通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 路线图: 本主题与其他主题有何关联?请参阅: 使用 C# 或 Visual Basic 的 Windows 运行时应用的路线图 使用 C++ 的 Windows 运行时应用的路线图 应用功能大全系列中突出显示的 Windows 应用商店应用 UI 详细信息 本主题包含下列部分: 先决条件 样式基础知识 应用隐式或显式样式 使用基于样式 使用工具轻松处理样式 修改 Windo

VSTO学习笔记(十五)Office 2013 初体验

原文:VSTO学习笔记(十五)Office 2013 初体验 Office 2013 近期发布了首个面向消费者的预览版本,我也于第一时间进行了更新试用.从此开始VSTO系列全面转向Office 2013平台,即VSTO 5.0. 本系列所有测试代码均在Visual Studio 2012 Ultimate RC + Office 2013 Professional Plus x64 Preview 上测试通过 为了配合Windows 8,微软的很多软件风格都逐渐Metro化,Office作为拳头

Android学习笔记(十五)——碎片的生命周期(附源码)

碎片的生命周期 点击下载源码 与活动类似,碎片具有自己的生命周期.理解了碎片的生命周期后,我们可以在碎片被销毁时正确地保存其实例,在碎片被重建时将其还原到前一个状态. 1.使用上一篇的项目Fragments,在Fragment1.java文件中添加如下代码: package net.zenail.Fragments; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragm

quick-cocos2d-x 学习系列之十四 测试用例

quick-cocos2d-x 学习系列之十四 测试用例 定义变量,创建13个场景名字 local items = { "framework.helper", "framework.native", "framework.display", "framework.crypto", "framework.network", "framework.luabinding", "fra

quick-cocos2d-x 学习系列之十六 塔防完结

quick-cocos2d-x 学习系列之十六 塔防完结 1.  math2d.lua文件 该文件实现了常用的数学函数. Dist函数实现两点的距离. radians4point求两点的夹角(弧度) pointAtCircle求圆上一个点的位置 pointAtLineToPoint求线段上与指定点距离最近的点 degrees2radians角度转换为弧度 radians2degrees弧度转换为角度 2.  utils.lua文件 2.1         drawCircle 返回newCirc

我的MYSQL学习心得(十五)

我的MYSQL学习心得(十五) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL学习心得(五) 我的MYSQL学习心得(六) 我的MYSQL学习心得(七) 我的MYSQL学习心得(八) 我的MYSQL学习心得(九) 我的MYSQL学习心得(十) 我的MYSQL学习心得(十一) 我的MYSQL学习心得(十二) 我的MYSQL学习心得(十三) 我的MYSQL学习心得(十四) 这一篇<我的MYSQL学习心得(十五)>

Android学习路线(十五)Activity生命周期——重新创建(Recreating)一个Activity

先占个位置,下次翻译~ :p There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your

quick-cocos2d-x 学习系列之十二 关于websocket

quick-cocos2d-x 学习系列之十二 关于websocket 1.  概念 百度百科:WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 在浏览器中通过http仅能实现单向的通信,comet可以一定程度上模拟双向通信,但效率较低,并需要服务器有较好的支持; flash中的socket和xmlsocket可以实现真正的双向通信,通过 flex ajax bridge,可以在javascript中使用这两项功能. 可

C++语言笔记系列之十五——派生类、基类、子对象的构造和析构函数调用关系

例子 example 1 注:若一个基类同时派生出两个派生类,即两个派生类从同一个基类继承,那么系统将为每一个简历副本,每个派生类独立地使用自己的基类副本(比如基类中有属于自己类的静态变量等). #include <iostream.h> class Person { public: person() {cout<<"Construction of person."<<endl;} ~person() {cout<<"Destr