我有一个简单的矩形 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/