我正在使用 Ray Wenderlich 的 2D Game Development Book,我利用这本书中的一个函数来控制 Sprite 的移动,这里称为“乘客”。书中的示例是在 x 轴和 y 轴上移动 Sprite ,因为我只需要沿 x 轴移动 Sprite ,但我无法让它工作。
这是“我的”代码 - 我正在使用的代码
func move(sprite: SKSpriteNode, velocity: CGPoint){
let amountToMove = CGPoint(x: velocity.x * CGFloat(dt),
y: velocity.y * CGFloat(dt))
sprite.position = CGPoint(
x: sprite.position.x + amountToMove.x,
y: sprite.position.y + amountToMove.y)
}
func movePassengerToward(location: CGPoint) {
let offset = CGPoint(x: location.x - passenger.position.x, y: location.y - passenger.position.y)
let length = sqrt(Double(offset.x * offset.x + offset.y * offset.y))
let direction = CGPoint(x: offset.x / CGFloat(length),
y: offset.y / CGFloat(length))
velocity = CGPoint(x: direction.x * passengerMovePointsPerSec,
y: direction.y * passengerMovePointsPerSec)
}
然后我像这样从更新循环中触发这些函数:
if let lastTaxiLocation = lastTaxiLocation {
let diff = lastTaxiLocation - passenger.position
if diff.length() <= passengerMovePointsPerSec * CGFloat(dt){
passenger.position = lastTaxiLocation
velocity = CGPoint.zero
} else {
move(sprite: passenger, velocity: velocity)
print("update loop")
}
}
有人对这个问题有想法吗?
最佳答案
尝试在 move
函数中只设置 X 组件:
sprite.position.x = sprite.position.x + amountToMove.x
关于ios - 仅在 x 轴上移动 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599485/