使用CGPath创建半圆路径,并使用SKAction跟随路径

时间:2016-03-07 09:25:46

标签: swift sprite-kit skaction cgpath

我不知道如何使用CGPath创建半圆路径。有我的代码:

{{1}}

1 个答案:

答案 0 :(得分:9)

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")
    }
}