【发布时间】:2017-10-24 16:19:55
【问题描述】:
我正在尝试在 SpriteKit 中的游戏中实现左右滑动手势,其中玩家收集从屏幕顶部生成的坠落物体。我面临的问题是试图在手指在屏幕上时保持玩家的连续移动,直到触摸结束并且玩家停留在最后一次触摸结束的地方。除了滑动手势之外,可能还有更好的方法来实现这一点,因此我问你们!任何帮助都会很棒,谢谢大家!
【问题讨论】:
标签: swift sprite-kit move swipe-gesture
我正在尝试在 SpriteKit 中的游戏中实现左右滑动手势,其中玩家收集从屏幕顶部生成的坠落物体。我面临的问题是试图在手指在屏幕上时保持玩家的连续移动,直到触摸结束并且玩家停留在最后一次触摸结束的地方。除了滑动手势之外,可能还有更好的方法来实现这一点,因此我问你们!任何帮助都会很棒,谢谢大家!
【问题讨论】:
标签: swift sprite-kit move swipe-gesture
这不是您想要使用滑动(我认为您正在使用平移)手势的东西。你要做的就是覆盖场景中的touchesBegan、touchesMoved和touchesEnded调用,根据这3个方法规划你的运动。
您可能希望将SKAction.move(to:duration:) 与这些方法一起使用,并计算出保持恒定速度的数学运算。
例如
func movePlayer(to position:CGPoint)
{
let distance = player.position.distance(to:position)
let move = SKAction.move(to:position, duration: distance / 100) // I want my player to move 100 points per second
//using a key will cancel the last move action
player.runAction(move,withKey:"playerMoving")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first
let position = touch.location(in node:self)
movePlayer(to:position)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
{
let touch = touches.first
let position = touch.location(in node:self)
movePlayer(to:position)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let position = touch.location(in node:self)
movePlayer(to:position)
}
【讨论】: