function Actor:ctor(properties, events, callbacks)
properties.id = "unk"
Actor.super.ctor(self, properties)
self.data = properties
-- 因为角色存在不同状态,所以这里为 Actor 绑定了状态机组件
self:addComponent("components.behavior.StateMachine")
-- 由于状态机仅供内部使用,所以不应该调用组件的 exportMethods() 方法,改为用内部属性保存状态机组件对象
self.fsm__ = self:getComponent("components.behavior.StateMachine")
-- 设定状态机的默认事件
local defaultEvents = {
-- 初始化后,角色处于 idle 状态
{name = "start", from = "none", to = "idle" },
-- 跑步
{name = "run", from = {"attacking", "idle"}, to = "running"},
-- 站立
{name = "stand", from = {"walking", "running"}, to = "idle"},
-- 开火
{name = "attack", from = "idle", to = "attacking"},
-- 开火冷却结束
{name = "ready", from = "attacking", to = "idle"},
-- 角色在正常状态下可被杀死
{name = "kill", from = {"idle", "attacking", "running", "walking"}, to = "dead"},
}
-- 如果继承类提供了其他事件,则合并
table.insertto(defaultEvents, checktable(events))
-- 设定状态机的默认回调
local defaultCallbacks = {
onchangestate = handler(self, self.onChangeState_),
onstart = handler(self, self.onStart_),
onattack = handler(self, self.onAttack_),
onready = handler(self, self.onReady_),
onkill = handler(self, self.onKill_),
}
-- 如果继承类提供了其他回调,则合并
table.merge(defaultCallbacks, checktable(callbacks))
self.fsm__:setupState({
events = defaultEvents,
callbacks = defaultCallbacks
})
self.fsm__:doEvent("start") -- 启动状态机
end
```
上面是我的一段用了状态机的代码,现在我需要添加一个动作,这个动作可以让任何的状态变成我想要的状态,要怎么设定啊?
{name = "killIt", from = "anyState", to = "dead" },-----可以这样么????
状态机有个 方法doEventForce(“kill”)
from="*"
这样写,然后self.fsm_:doEvent(“killlt”) 就行了
— Begin quote from ____
引用第2楼天空光芒于2015-05-28 22:04发表的 :
from="*"
这样写,然后self.fsm_:doEvent(“killlt”) 就行了 http://www.cocoachina.com/bbs/job.php?action=topost&tid=301696&pid=1317116
— End quote
正解!!谢谢!