如何在中途在SKAction中反转path的方向?

我有一个SKSpriteNode沿着使用SKAction的圆形path移动:

// create the path our sprite will travel along let circlePath = CGPathCreateWithEllipseInRect(CGRect(origin: pathCenterPoint, size: CGSize(width: circleDiameter, height: circleDiameter)), nil) // create a followPath action for our sprite let followCirclePath = SKAction.followPath(circlePath, asOffset: false, orientToPath: false, duration: 2 

我可以添加.ReversedAction()来反转精灵的方向,但是这只会从起点开始。

当它在path上的某个点时,我如何反转精灵的方向?

我假设你试图在玩家触摸屏幕时朝相反的方向前进。 尝试为两个方向创build一个函数,在这些函数中为顺时针和逆时针添加一个函数,添加您的path的方法。 我会用这个代码来完成这个任务,因为我没有发现任何错误:

  func moveClockWise() { let dx = Person.position.x - self.frame.width / 2 let dy = Person.position.y - self.frame.height / 2 let rad = atan2(dy, dx) let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 120, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true) let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 200) Person.runAction(SKAction.repeatActionForever(follow).reversedAction()) } 

这只是我的首选方法,而对于逆时针方向,只需创build另一个函数即可颠倒代码。

现在在didMoveToView之上添加这些variables:

 var Person = SKSpriteNode() var Path = UIBezierPath() var gameStarted = Bool() var movingClockwise = Bool() 

这些基本上将你的Person定义为一个SKSpriteNode()你的Path作为一个UIBezierPath()等等。当然你需要在你的didMoveToView下创build一个Person.position = positionPerson = SKSpriteNode(imageNamed: "name")来创buildsprite。

在此之后,在override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { ,您想要使用gameStarted布尔variables来检测它正在运行,如果它被设置为真,并更改它方向。

 if gameStarted == false { moveClockWise() movingClockwise = true gameStarted = true } else if gameStarted == true { if movingClockwise == true { moveCounterClockWise() movingClockwise = false } else if movingClockwise == false { moveClockWise() movingClockwise = true } } 

基本上,第一行代码检查bool是否为false(这是因为它没有发生任何事情,它刚刚加载),并运行moveClockwise函数,并将moveClockwise布尔值设置为true,并将gameStarted布尔值设置为true。 其他的一切都很自我解释,希望这有助于。

马克斯