旋转时SKSpritenode旋转太快
当前,当触摸屏幕的一侧时,我试图将SKSpritenode旋转到45度,而当另一侧触摸另一侧时-45度,当触摸结束时返回到零。问题在于,它不是从-45到0或-45到45的最快途径,而是多次旋转相反的方向,直到达到该学位。这是我当前的代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.previousLocation(in: self)
let position = touch.location(in: self)
let node = self.nodes(at: location).first
//IF LEFT SCREEN IS TOUCHED
if position.x < 0 {
let rotate = CGFloat(45.0 * .pi / 180)
let rotateAction = SKAction.rotate(toAngle: rotate, duration: 0.2)
car.run(rotateAction, withKey: "rotating")
//IF RIGHT SCREEN IS TOUCHED
} else {
let rotate2 = CGFloat(-45.0 * .pi / 180)
let rotateAction2 = SKAction.rotate(toAngle: rotate2, duration: 0.2)
car.run(rotateAction2, withKey: "rotating")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
car.removeAction(forKey: "rotating")
let rotate4 = CGFloat(0.0 * .pi / 180)
let rotateAction4 = SKAction.rotate(toAngle: rotate4, duration: 0.2)
car.run(rotateAction4, withKey: "rotating4")
}
Currently I am trying to rotate a SKSpriteNode to 45 degrees when one side of the screen is touched, and -45 degrees when the other side is touched, and back to zero when the touch has ended. The problem is instead of going fluidly the fastest route from -45 to 0, or -45 to 45, it seems to spin the opposite direction very quickly multiple times until it reaches said degree. Here is my current code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.previousLocation(in: self)
let position = touch.location(in: self)
let node = self.nodes(at: location).first
//IF LEFT SCREEN IS TOUCHED
if position.x < 0 {
let rotate = CGFloat(45.0 * .pi / 180)
let rotateAction = SKAction.rotate(toAngle: rotate, duration: 0.2)
car.run(rotateAction, withKey: "rotating")
//IF RIGHT SCREEN IS TOUCHED
} else {
let rotate2 = CGFloat(-45.0 * .pi / 180)
let rotateAction2 = SKAction.rotate(toAngle: rotate2, duration: 0.2)
car.run(rotateAction2, withKey: "rotating")
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
car.removeAction(forKey: "rotating")
let rotate4 = CGFloat(0.0 * .pi / 180)
let rotateAction4 = SKAction.rotate(toAngle: rotate4, duration: 0.2)
car.run(rotateAction4, withKey: "rotating4")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一个
shortestunitarc:true
参数:请注意,
rotate
:rotate(toangle:duration:)
旋转(toangle:toangle:shortestunitarc:)
旋转(byangle:持续时间:)
Add a
shortestUnitArc: true
parameter:Note that there are many overloads of
rotate
:rotate(toAngle:duration:)
rotate(toAngle:duration:shortestUnitArc:)
rotate(byAngle:duration:)