我假设你试图让它在玩家触摸屏幕时朝相反的方向移动。尝试为这两个方向创建一个函数,一个用于顺时针和逆时针在这些函数中添加您制作路径的方法。我将使用此代码来完成此任务,因为我没有发现任何错误:
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())
}
这只是我的首选方式,对于逆时针方向,只需创建另一个函数,只需反转代码即可。
现在在 didMoveToView 上面添加这些变量:
var Person = SKSpriteNode()
var Path = UIBezierPath()
var gameStarted = Bool()
var movingClockwise = Bool()
这些基本上将您的 Person 定义为 SKSpriteNode() 您的 Path 定义为 UIBezierPath() 等。当然,您可以在 didMoveToView 下使用 Person.position = position 和 Person = SKSpriteNode(imageNamed: "name") 来创建精灵。
在此之后,在override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 下,您想使用gameStarted bool 变量来检测它是否正在运行,如果将bool 设置为true 并改变它的方向。
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 是否为假(这是因为它没有发生任何事情并且它刚刚加载),并运行 moveClockwise 函数并将 moveClockwise bool 设置为 true 并将 gameStarted bool 设置为 true以及。其他一切都非常不言自明。