如何围绕其底点旋转SKShapeNode线

时间:2017-06-02 06:56:59

标签: swift sprite-kit

我对如何围绕它的底点(开始点,这里)旋转一条线(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)

    }

1 个答案:

答案 0 :(得分:1)

你的功能旋转了周围的路径 形状public double Accuracy(double[][] testData) { // percentage correct using winner-takes all int numCorrect = 0; int numWrong = 0; double[] xValues = new double[numInput]; // inputs double[] tValues = new double[numOutput]; // targets double[] yValues; // computed Y for (int i = 0; i < testData.Length; ++i) { Array.Copy(testData[i], xValues, numInput); // parse test data into x-values and t-values Array.Copy(testData[i], numInput, tValues, 0, numOutput); yValues = this.ComputeOutputs(xValues); int maxIndex = MaxIndex(yValues); // which cell in yValues has largest value? int tMaxIndex = MaxIndex(tValues); if (maxIndex == tMaxIndex) ++numCorrect; else ++numWrong; } return (numCorrect * 1.0) / (double)testData.Length; } (默认情况下为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))