spritekit游戏项目中如何让节点围绕节点外的点旋转
How to make a node to rotate around a point outside of the node in a spritekit game project
我看到了这个答案,但我不清楚答案。有人可以提供一些示例代码吗?
创建一个SKNode并将其位置设置为旋转中心。将应该围绕该中心旋转的节点作为子节点添加到中心节点。将子节点的位置设置为所需的偏移量(即半径,比如 x + 100)。更改中心节点的旋转属性,使子节点围绕中心点旋转。
具体来说,"Change the rotation property of the center node" 是什么?
var centerNode: SKSpriteNode = SKSpriteNode(imageNamed: "image1")
centerNode.position = CGPointMake(self.frame.width/2, self.frame.height/2)
self.addChild(centerNode)
var nodeRotateMe: SKSpriteNode = SKSpriteNode(imageNamged: "image2")
nodeRotateMe.position = CGPointMake(self.frame.width/2 + 100, self.frame.height/2 + 100)
centerNode.addChild(nodeRotateMe)
// Change the rotation property of the center node to what??
centerNode.zRotation = ?
您有两个选择:
1) 您可以通过手动更改 zRotation
属性 在 SKScene
的 update:
方法中随时间旋转 centerNode
。您必须在每次调用 update:
时缓慢更改值以实现逐渐轮换。
请注意 zRotation
属性 以弧度为单位,表示旋转 180 度等于 pi (M_PI
)。 SKScene
努力以 60 FPS update:
,这意味着在 5 秒内旋转 360 度,您需要在每次调用更新时增加 1/300 度,即 1/150 pi 每次更新。
centerNode.zRotation = centerNode.zRotation + CGFloat(1.0/150.0 * M_PI)
当然,无法保证您的场景能够以 60 FPS 的速度更新,因此您可能需要监控 update:
中的 currentTime
变量并进行相应调整。
2) 可能更好,您可以使用 SKAction 为您旋转 centerNode
。
let rotateAction = SKAction.rotateByAngle(CGFloat(2 * M_PI), duration: 5.0)
centerNode.runAction(rotateAction)
请注意,旋转角度的单位是弧度,而不是度数。另请注意,如果您不想在此处显示图像,则 centerNode
不必是 Sprite 节点;它可能是一个普通的 SKNode。
我看到了这个答案,但我不清楚答案。有人可以提供一些示例代码吗?
创建一个SKNode并将其位置设置为旋转中心。将应该围绕该中心旋转的节点作为子节点添加到中心节点。将子节点的位置设置为所需的偏移量(即半径,比如 x + 100)。更改中心节点的旋转属性,使子节点围绕中心点旋转。
具体来说,"Change the rotation property of the center node" 是什么?
var centerNode: SKSpriteNode = SKSpriteNode(imageNamed: "image1")
centerNode.position = CGPointMake(self.frame.width/2, self.frame.height/2)
self.addChild(centerNode)
var nodeRotateMe: SKSpriteNode = SKSpriteNode(imageNamged: "image2")
nodeRotateMe.position = CGPointMake(self.frame.width/2 + 100, self.frame.height/2 + 100)
centerNode.addChild(nodeRotateMe)
// Change the rotation property of the center node to what??
centerNode.zRotation = ?
您有两个选择:
1) 您可以通过手动更改 zRotation
属性 在 SKScene
的 update:
方法中随时间旋转 centerNode
。您必须在每次调用 update:
时缓慢更改值以实现逐渐轮换。
请注意 zRotation
属性 以弧度为单位,表示旋转 180 度等于 pi (M_PI
)。 SKScene
努力以 60 FPS update:
,这意味着在 5 秒内旋转 360 度,您需要在每次调用更新时增加 1/300 度,即 1/150 pi 每次更新。
centerNode.zRotation = centerNode.zRotation + CGFloat(1.0/150.0 * M_PI)
当然,无法保证您的场景能够以 60 FPS 的速度更新,因此您可能需要监控 update:
中的 currentTime
变量并进行相应调整。
2) 可能更好,您可以使用 SKAction 为您旋转 centerNode
。
let rotateAction = SKAction.rotateByAngle(CGFloat(2 * M_PI), duration: 5.0)
centerNode.runAction(rotateAction)
请注意,旋转角度的单位是弧度,而不是度数。另请注意,如果您不想在此处显示图像,则 centerNode
不必是 Sprite 节点;它可能是一个普通的 SKNode。