旋转时SKSpritenode旋转太快

发布于 2025-02-04 14:19:24 字数 1365 浏览 4 评论 0原文

当前,当触摸屏幕的一侧时,我试图将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花想c 2025-02-11 14:19:24

添加一个shortestunitarc:true参数:

let rotateAction = SKAction.rotate(toAngle: rotate, duration: 0.2, shortestUnitArc: true)

请注意,rotate

Add a shortestUnitArc: true parameter:

let rotateAction = SKAction.rotate(toAngle: rotate, duration: 0.2, shortestUnitArc: true)

Note that there are many overloads of rotate:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文