我正在使用 GKStateMachine 来跟踪我的游戏中瓷砖的状态。
当我放置一个物品时,该物品所覆盖的瓷砖会进入“计划”状态。我怎样才能存储瓷砖,然后在以后将瓷砖恢复到以前的状态?
这是我认为与我的问题相关的代码部分。很高兴分享更多关于特定要求的信息。
//...
// Have previously entered the tiles stored in the global Game object into a state which is not "planned" state
class func movePlannedObject(x: Int, y: Int) {
//DO Some things
for tile in Game.sharedInstance.plannedBuildingTiles {
tile.stateMachine.enterState(tile.previousState!)
}
// Set new position and change state of tiles to planned
}
//...
和状态机:
class TileState : GKState{
var tile : Tile?
init( tile: Tile ) {
super.init()
self.tile = tile
}
}
class TileTileState: TileState {
}
class TileGrassState: TileState {
}
class TilePathState: TileState {
}
class TilePlanState: TileState {
override func didEnterWithPreviousState(previousState: GKState?) {
super.didEnterWithPreviousState(previousState)
tile?.previousState = // What?
print(tile?.previousState)
Game.sharedInstance.plannedBuildingTiles.append(tile!)
}
}
要点:https ://gist.github.com/Relequestual/dac6d51c923e5ce4224e
如果我将previousState设置为previousState,我就不能使用该变量进入状态......
// In the tile class
var previousState: GKState?
// In states function didEnterWithPreviousState
tile?.previousState = previousState
// Elsewhere
tile.stateMachine.enterState(tile.previousState!)
编译时错误:
Cannot convert value of type 'GKState' to expected argument type 'AnyClass' (aka 'AnyObject.Type')