【问题标题】:Rotating an SKShapeNode along its center沿中心旋转 SKShapeNode
【发布时间】:2014-10-09 17:19:40
【问题描述】:

我有一个 SKShapeNode(在这种情况下为矩形),我试图沿其中心旋转。但是它沿着屏幕的左下角旋转。由于我无法为 SKShapeNode 设置锚点,我该如何实现我的要求(沿其中心旋转形状)。这是我正在尝试的代码。

    let rectangle = SKShapeNode()
    rectangle.path = UIBezierPath(rect: CGRectMake(view.frame.width/4, view.frame.height/2, view.frame.width/2, view.frame.width/2)).CGPath
    rectangle.fillColor = UIColor.yellowColor()
    rectangle.strokeColor = UIColor.yellowColor()

    let oneRevolution = SKAction.rotateByAngle(360, duration: 100.0)
    let repeat = SKAction.repeatActionForever(oneRevolution)
    rectangle.runAction(repeat)

【问题讨论】:

    标签: ios swift sprite-kit skshapenode


    【解决方案1】:

    您是否查看过 SKShapeNode 文档:https://developer.apple.com/library/IOs/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html

    尝试使用:

    shapeNodeWithPath:centered:
    

    为居中选择“是”。

    如果是,路径被平移,使得路径的中心 边界框位于节点的原点;否则路径是相对的 到节点的原点。

    【讨论】:

      【解决方案2】:
      SKSpriteNode rectangle = [SKSpriteNode spriteWithSize size: CGSizeMake(20,20)];
      

      您可以制作与 spriteNode 相同的矩形,这样您就可以更改锚点。我的语法可能有点偏离,但我相信它也可以在构造函数中使用颜色。

      【讨论】:

        【解决方案3】:

        当您创建 SKShapeNode 时,您必须以这种方式将其设置为 center

        let shapePath = UIBezierPath(...)
        ...
        ...            
        let shape = SKShapeNode(path: shapePath.cgPath, centered: true)
        

        这样 SKShapeNode 的锚点将成为中心点。

        【讨论】:

          【解决方案4】:

          在应用旋转之前,我们必须明确地将 SKShapeNode 居中。

              import SpriteKit
              import PlaygroundSupport
          
              let bounds = CGRect(x: 0, y: 0, width: 400, height: 200)
              let skview = SKView(frame: bounds)
          
              PlaygroundPage.current.liveView = skview
          

          在下面的第一个和第二个例子中,这是通过在初始化矩形时设置xy参数来完成的,然后设置位置属性。

              let first = SKShapeNode(rect: CGRect(x:-20, y:-20, width: 40, height: 40))
              first.position = CGPoint(x: 70, y: 30)
          
          
              let second = SKShapeNode(rect: CGRect(x:-20, y:-20, width: 40, height: 40))
              second.position = CGPoint(x: 70, y: 30)
              second.zRotation = CGFloat.pi * 0.15
              second.strokeColor = .red
          

          在下面的第三个示例中,这是通过在初始化形状时将centered 设置为true,然后设置位置属性来完成的。

              let path = CGMutablePath();
              path.move(to: CGPoint(x: 50,y: 10))
              path.addLine(to: CGPoint(x: 90, y: 10))
              path.addLine(to: CGPoint(x: 90, y: 50))
              path.addLine(to: CGPoint(x: 50, y: 50))
              path.addLine(to: CGPoint(x: 50, y: 10))
          
              let third = SKShapeNode(path: path, centered: true);
              third.position = CGPoint(x: 70,y: 30);
              third.zRotation = CGFloat.pi * 0.35
              third.strokeColor = .yellow
          
              let scene = SKScene(size: CGSize(width: 400, height: 200))
              scene.scaleMode = SKSceneScaleMode.aspectFill
              scene.size = skview.bounds.size
              scene.addChild(first);
              scene.addChild(second);
              scene.addChild(third);
              skview.presentScene(scene);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-26
            • 2017-10-26
            • 1970-01-01
            • 1970-01-01
            • 2019-10-05
            相关资源
            最近更新 更多