我正在尝试沿着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)))
所有动作都是使形状节点消失。我怎样才能让圆圈永远沿着bezier路径移动?
答案 0 :(得分:1)
您将贝塞尔路径circleTrack
放在一个位置,将shapeTrack放在另一个位置。 circleTrack
的来源位于(screenWidth/2,screenHeight/2)
,shapeTrack位于(screenWidth/2,screenHeight/2)
。 shapeTrack.position
是SKShapeNode
中心的位置。请尝试以下代码。
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))