请求 GKStateMachine 的简单示例?
Request for a simple example for GKStateMachine?
我一直在为 Swift 研究 Apple 的状态机并找到了几个示例,但是 none 其中非常简单。
有人可以创建一个超级简单的 GKStateMachine,也许有两个状态,在 Swift 中,最好是在一个 Swift 文件中吗?谢谢!
这是在美国工作的交通信号灯状态机示例。红绿灯从 green -> yellow -> red -> green
.
移动
在应用中,您可以让 didEnter(from:)
例程更新屏幕图形,或允许其他演员移动。
import UIKit
import GameKit
class Green: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Yellow.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is green")
}
}
class Yellow: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Red.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is yellow")
}
}
class Red: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Green.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is red")
}
}
class ViewController: UIViewController {
var stateMachine: GKStateMachine?
override func viewDidLoad() {
super.viewDidLoad()
// Create the states
let green = Green()
let yellow = Yellow()
let red = Red()
// Initialize the state machine
stateMachine = GKStateMachine(states: [green, yellow, red])
// Try entering various states...
if stateMachine?.enter(Green.self) == false {
print("failed to move to green")
}
if stateMachine?.enter(Red.self) == false {
print("failed to move to red")
}
if stateMachine?.enter(Yellow.self) == false {
print("failed to move to yellow")
}
if stateMachine?.enter(Green.self) == false {
print("failed to move to green")
}
if stateMachine?.enter(Red.self) == false {
print("failed to move to red")
}
}
}
输出:
Traffic light is green
failed to move to red
Traffic light is yellow
failed to move to green
Traffic light is red
我一直在为 Swift 研究 Apple 的状态机并找到了几个示例,但是 none 其中非常简单。
有人可以创建一个超级简单的 GKStateMachine,也许有两个状态,在 Swift 中,最好是在一个 Swift 文件中吗?谢谢!
这是在美国工作的交通信号灯状态机示例。红绿灯从 green -> yellow -> red -> green
.
在应用中,您可以让 didEnter(from:)
例程更新屏幕图形,或允许其他演员移动。
import UIKit
import GameKit
class Green: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Yellow.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is green")
}
}
class Yellow: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Red.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is yellow")
}
}
class Red: GKState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Green.Type
}
override func didEnter(from previousState: GKState?) {
print("Traffic light is red")
}
}
class ViewController: UIViewController {
var stateMachine: GKStateMachine?
override func viewDidLoad() {
super.viewDidLoad()
// Create the states
let green = Green()
let yellow = Yellow()
let red = Red()
// Initialize the state machine
stateMachine = GKStateMachine(states: [green, yellow, red])
// Try entering various states...
if stateMachine?.enter(Green.self) == false {
print("failed to move to green")
}
if stateMachine?.enter(Red.self) == false {
print("failed to move to red")
}
if stateMachine?.enter(Yellow.self) == false {
print("failed to move to yellow")
}
if stateMachine?.enter(Green.self) == false {
print("failed to move to green")
}
if stateMachine?.enter(Red.self) == false {
print("failed to move to red")
}
}
}
输出:
Traffic light is green failed to move to red Traffic light is yellow failed to move to green Traffic light is red