如何在 SKAction 中途反转精灵所遵循的路径方向?
How to reverse the direction of path followed by sprite in an SKAction midway?
我有一个 SKSpriteNode,它使用 SKAction 沿着圆形路径移动:
// 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() 来反转精灵的方向,但这只会从起点开始。
当精灵位于路径中的某个点时,如何反转精灵的方向?
我假设您试图让它在玩家触摸屏幕时朝相反的方向移动。尝试为两个方向创建一个函数,一个用于顺时针和逆时针,在这些函数中添加您制作路径的方法。我会使用此代码来完成此任务,因为我没有发现任何错误:
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()
你的路径定义为 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 是否为 false(这是因为它没有发生任何事情并且它刚刚加载),然后运行 moveClockwise 函数并将 moveClockwise bool 设置为 true 和 gameStarted bool也是真实的。其他一切都是不言自明的。
我有一个 SKSpriteNode,它使用 SKAction 沿着圆形路径移动:
// 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() 来反转精灵的方向,但这只会从起点开始。
当精灵位于路径中的某个点时,如何反转精灵的方向?
我假设您试图让它在玩家触摸屏幕时朝相反的方向移动。尝试为两个方向创建一个函数,一个用于顺时针和逆时针,在这些函数中添加您制作路径的方法。我会使用此代码来完成此任务,因为我没有发现任何错误:
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()
你的路径定义为 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 是否为 false(这是因为它没有发生任何事情并且它刚刚加载),然后运行 moveClockwise 函数并将 moveClockwise bool 设置为 true 和 gameStarted bool也是真实的。其他一切都是不言自明的。