【发布时间】:2019-04-24 07:53:19
【问题描述】:
我正在尝试使用本教程在 Swift 中使用 SpriteKit 制作入侵者游戏:
我完成了第 1 部分和第 2 部分。但是入侵者的移动还是很奇怪。当入侵者到达屏幕的右边缘时,只有一名入侵者下线并向左移动。其他人只是向左移动。
我该如何解决?
enum InvaderMovementDirection {
case right
case left
case downThenRight
case downThenLeft
case none
}
func moveInvaders(forUpdate currentTime: CFTimeInterval) {
if (currentTime - timeOfLastMove < timePerMove) {
return
}
enumerateChildNodes(withName: InvaderType.name) { node, stop in
switch self.invaderMovementDirection {
case .right:
node.position = CGPoint(x: node.position.x + 10, y: node.position.y)
case .left:
node.position = CGPoint(x: node.position.x - 10, y: node.position.y)
case .downThenLeft, .downThenRight:
node.position = CGPoint(x: node.position.x, y: node.position.y - 10)
case .none:
break
}
self.timeOfLastMove = currentTime
self.determineInvaderMovementDirection()
}
}
func determineInvaderMovementDirection() {
var proposedMovementDirection: InvaderMovementDirection = invaderMovementDirection
enumerateChildNodes(withName: InvaderType.name) { node, stop in
switch self.invaderMovementDirection {
case .right:
if (node.frame.maxX >= node.scene!.size.width - 1.0) {
proposedMovementDirection = .downThenLeft
self.adjustInvaderMovement(to: self.timePerMove * 0.8)
stop.pointee = true
}
case .left:
if (node.frame.minX <= 1.0) {
proposedMovementDirection = .downThenRight
self.adjustInvaderMovement(to: self.timePerMove * 0.8)
stop.pointee = true
}
case .downThenLeft:
proposedMovementDirection = .left
stop.pointee = true
case .downThenRight:
proposedMovementDirection = .right
stop.pointee = true
default:
break
}
}
if (proposedMovementDirection != invaderMovementDirection) {
invaderMovementDirection = proposedMovementDirection
}
}
【问题讨论】:
标签: swift sprite-kit