ios - 当尝试按下按钮将 Sprite 的 zRotation 增加 1 度时,旋转不正确

标签 ios swift swift3 sprite-kit gamekit

我有一个简单的矩形 Sprite ,每次按下屏幕上的按钮时都希望将此 Sprite 旋转 1 度。我有一些简单的代码,我希望可以工作,但它给出了奇怪的结果。每次点击似乎将矩形旋转至少 180-270 度,而不是旋转 1 度。

var degrees = 0

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let touchLocation = touch!.location(in: self)
    let beam = childNode(withName: "beam") as! SKSpriteNode


    if let body = physicsWorld.body(at: touchLocation) {
        if body.node!.name == "leftTap" {
            print("Began touch on Left")
            beam.zRotation = CGFloat(degrees)
            degrees = degrees + 1

        }else{
            print("Began touch on right")
        }
    }
}

在 GameScene.sks Sprite 属性检查器中,您可以通过按“+”或“-”按钮来增加旋转。我认为这会以同样的方式工作,但我一定不理解这里的轮换原理。

如有任何建议,我们将不胜感激。

最佳答案

这不是度数,而是拉德。 3 左右 (pi) 的值是完整旋转。

https://developer.apple.com/documentation/spritekit/sknode/1483089-zrotation

这是一个您可以使用的方便的扩展:

extension SKNode {
  // Usage is node.rotateBy(45) etc:
  func rotateBy(_ degree: CGFloat) {
    let conversionFactor = CGFloat(0.01745329252)
    self.run(.rotate(byAngle: degree * conversionFactor, duration: 0))
  }
}

关于ios - 当尝试按下按钮将 Sprite 的 zRotation 增加 1 度时,旋转不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481037/

相关文章:

c# - ios webview - 如何在应用程序未运行时检查通知

ios - 为什么 XCUITest 在运行 iOS 11.0.1 的 Xcode 上失败

ios - MBProgressHUD 命名约定说明

ios - didselectrowat Indexpath 使用两个发送者和一个公共(public) Segue 标识符

ios - 从选项卡切换时的透明 View

swift - 父类(super class)中的选项导致子类初始化程序出现问题

ios - 如何使用查询从 CouchBaseLite Mobile 中查找数据?

ios - Swift:以编程方式为 subview 设置自动布局约束不调整 View 大小

rotation - 应用旋转后如何将 SCNNode 沿其指向的方向移动

ios - 如何处理协议(protocol)功能覆盖的@available()?