Ios 如何使用GKStateMachine的状态传递参数
在进入状态时,我希望通过以下状态传递参数: playerStateMachine.enterpauseState.self[分数:123,排名:1] 因此,我可以在dienter方法中得到如下值: 类别:国家{ var userinfo:[字符串:任意]? initplayer:SSGameDelegate{ 超级玩家:玩家 } 覆盖先前状态中的func DIDENTERE:GKState{ super.didEnterfrom:previousState //在此处访问输入值 printuserinfo[分数] printuserinfo[排名] } }Ios 如何使用GKStateMachine的状态传递参数,ios,swift,state-machine,Ios,Swift,State Machine,在进入状态时,我希望通过以下状态传递参数: playerStateMachine.enterpauseState.self[分数:123,排名:1] 因此,我可以在dienter方法中得到如下值: 类别:国家{ var userinfo:[字符串:任意]? initplayer:SSGameDelegate{ 超级玩家:玩家 } 覆盖先前状态中的func DIDENTERE:GKState{ super.didEnterfrom:previousState //在此处访问输入值 printuse
不能以这种方式传递参数。在当前状态下,有一个函数
func willExit(to nextState: GKState)
您可以将参数作为nextState.userinfo[score]=111传递到那里。如果您跟踪状态机状态,则可以在进入状态之前设置参数。我不完全确定这是线程安全的,我将在进一步测试使用情况时进行更新
let alienWaitState = AlienWaitState(game: self)
let alienFlyingState = AlienFlyingState(game: self)
aliensStateMachine = GKStateMachine(states: [
alienWaitState,
alienFlyingState,
AlienAnimateOffState(game: self),
AlienDeadRemovalState(game: self)
])
在这种情况下,可以在进入状态之前设置alienState参数associatedAlienName
alienWaitState.associatedAlienName = newAlien.name
aliensStateMachine.enter(AlienWaitState.self)
AlienState类,alienWaitState是一个子类
class AlienState: GKState {
weak var game:GameScene?
var associatedAlienName:String?
....
谢谢@E.com。如何将初始值(例如[武器:剑]设置为当前状态,以便将其传递到下一个状态?获取错误为:类型为“GKState”的值没有成员“userinfo”。您需要使用开关来确定下一个状态的类型。或者在这里下一个州!PauseState.userinfo[“score”]当您启动当前状态时,您将武器置于初始状态