使用贝塞尔路径动作时动作使形状节点消失

action makes shape node disappear when using bezier path action

我正在尝试沿着 UIBezierPath 移动 SKShapeNode。这是我到目前为止的代码:

// Set up the circle track
    let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2, screenHeight/2, screenWidth/3, screenWidth/3), cornerRadius: 100)
    let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
    shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
    shapeTrack.strokeColor = SKColor.whiteColor()
    self.addChild(shapeTrack)

    // Create the ball
    let circle = SKShapeNode(circleOfRadius: 15)
    circle.position = CGPointMake(screenWidth/2, screenHeight/2)
    circle.fillColor = SKColor.whiteColor()
    self.addChild(circle)

    //Move the circle
    circle.runAction(SKAction.repeatActionForever(SKAction.followPath(circleTrack.CGPath, speed: 3.0)))

所有的动作都是让形状节点消失。我怎样才能让圆圈永远沿着贝塞尔路径移动?

您将贝塞尔曲线路径 circleTrack 放在一个位置,将 shapeTrack 放在另一个位置。 circleTrack 的原点在 (screenWidth/2,screenHeight/2),shapeTrack 以 (screenWidth/2,screenHeight/2) 为中心。 shapeTrack.positionSKShapeNode 的中心位置。试试下面的代码。

let screenWidth = size.width
let screenHeight = size.height

let trackWidth = screenWidth/3

let circleTrack = UIBezierPath(roundedRect: CGRectMake(screenWidth/2 - trackWidth/2, screenHeight/2  - trackWidth/2, trackWidth, trackWidth), cornerRadius: 100)
let shapeTrack = SKShapeNode(path: circleTrack.CGPath, centered: true)
shapeTrack.position = CGPointMake(screenWidth/2, screenHeight/2)
shapeTrack.strokeColor = UIColor.whiteColor()
self.addChild(shapeTrack)

// Create the ball
let circle = SKShapeNode(circleOfRadius: 15)
circle.fillColor = UIColor.whiteColor()
self.addChild(circle)

let followPath = SKAction.followPath(circleTrack.CGPath, asOffset: false, orientToPath: false, speed: 200.0)

//Move the circle
circle.runAction(SKAction.repeatActionForever(followPath))