使用CGPath制作半圆路径,并使用SKAction沿着路径移动

8

我不知道如何使用CGPath制作半圆路径。这是我的代码:

  let path = CGPathCreateMutable()
  let Width: CGFloat = 38.0
  let radius: CGFloat = (Width / 2.0) + (Treshold / 2.0)
  CGPathAddArc(pathOne, nil, x, 0.0, radius, firstPoint.position.x, secondPoint.position.x, true)
  let waitAction = SKAction.waitForDuration(1.0)
  let moveAction = SKAction.followPath(pathOne, duration: 3.0)
  capOne.runAction(SKAction.sequence([waitAction, moveAction]))
1个回答

10
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    override init(size: CGSize) {
        super.init(size: size)
        let square = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(20, 20))
        square.position = CGPointMake(self.size.width/2, self.size.height/2)
        addChild(square)

        let bezierPath = UIBezierPath(arcCenter: CGPointMake(0, 0), radius: 100, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(-M_PI_2), clockwise: false)

        let pathNode = SKShapeNode(path: bezierPath.CGPath)
        pathNode.strokeColor = SKColor.blueColor()
        pathNode.lineWidth = 3
        pathNode.position = square.position
        addChild(pathNode)

        square.runAction(SKAction.followPath(bezierPath.CGPath, asOffset: true, orientToPath: true, duration: 2))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

谢谢,hamobi。这正是我在寻找的。 - Nurassyl Nuridin
我该如何从两个精灵的位置计算出起始角度和结束角度,如果我想用半圆路径交换这两个精灵。我尝试了你的示例,但是你的半圆路径并没有解决我的问题。当我将第一个精灵与第二个精灵交换时,我需要在它们上方有半圆路径,当我将第二个精灵移动到第一个精灵时,在它们下方还需要另一个半圆路径。 - Nurassyl Nuridin
我发了另一个问题,你可以看一下吗? - Nurassyl Nuridin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接