gpt4 book ai didi

swift - 如何围绕其底点旋转 SKShapeNode 线

转载 作者:行者123 更新时间:2023-11-28 15:35:38 28 4
gpt4 key购买 nike

我对如何围绕它的底部点(起点,这里)旋转一条线(SKShapeNode)感到困惑,想想这里的时钟。

我有以下内容,但它似乎没有按预期旋转。

    public let line = SKShapeNode()
private let linePath = CGMutablePath()

init(begin: CGPoint, end: CGPoint) {
self.begin = begin
self.end = end


linePath.move(to: begin)
linePath.addLine(to: end)

line.path = linePath
line.strokeColor = UIColor.black
line.lineWidth = 3
SceneCoordinator.shared.gameScene.addChild(line)
}


public func rotate(angle: Double) {
var transform = CGAffineTransform(rotationAngle: CGFloat(angle))
line.path = linePath.mutableCopy(using: &transform)

}

最佳答案

您的函数围绕形状 position(默认情况下为 (0, 0)),而不是像预期的那样围绕线的起点。

为了解决这个问题,创建一个位置等于起始位置的形状线的点,以及相对于该点的线:

linePath.move(to: CGPoint.zero)
linePath.addLine(to: CGPoint(x: end.x - begin.x, y: end.y - begin.y))
line.path = linePath
line.position = begin
// ...
SceneCoordinator.shared.gameScene.addChild(line)

请注意,您可以旋转节点而不是变换路径:

line.zRotation = angle

或动画:

line.run(SKAction.rotate(toAngle: angle, duration: 0.2))

您可以计算端点在场景中的位置坐标系与

let endPos = line.convert(CGPoint(x: end.x - begin.x, y: end.y - begin.y), to: line.scene!)

关于swift - 如何围绕其底点旋转 SKShapeNode 线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322662/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com