我正在尝试用键盘让宇宙飞船在屏幕上移动。我设法处理关键事件,但我注意到,当同时按下多个键时,它的行为将不正确,只有一个具有优先级。我正在使用一个switch
语句,因为我认为该keyDown
函数为每个键调用一次,但即使我fallthrough
在案例中明确添加 a ,它也不是更好。有没有人经历过这样的事情,有没有更好的方法将键盘用作控制器?
override func keyDown(with event: NSEvent) {
switch event.keyCode {
case 0x31:
if let playerShip = self.playerShip {
playerShip.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut")
}
case 123:
if let playerShip = self.playerShip {
playerShip.run(SKAction.applyAngularImpulse(0.001, duration: 0.1))
}
case 124:
if let playerShip = self.playerShip {
playerShip.run(SKAction.applyAngularImpulse(-0.001, duration: 0.1))
}
case 125:
if let playerShip = self.playerShip {
playerShip.run(SKAction.applyImpulse(CGVector.init(angle: playerShip.zRotation).opposite(), duration: 0.1))
}
case 126:
if let playerShip = self.playerShip {
playerShip.run(SKAction.applyImpulse(CGVector.init(angle: playerShip.zRotation), duration: 0.1))
}
default:
print("keyDown: \(event.characters!) keyCode: \(event.keyCode)")
}
}