gpt4 book ai didi

ios - 如何在 SKAction 中途反转 Sprite 所遵循的路径方向?

转载 作者:可可西里 更新时间:2023-11-01 02:19:03 25 4
gpt4 key购买 nike

我有一个 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() 来反转 Sprite 的方向,但这只会从起点开始。

当 Sprite 位于路径中的某个点时,如何反转 Sprite 的方向?

最佳答案

我假设您试图让它在玩家触摸屏幕时朝相反的方向移动。尝试为两个方向创建一个函数,一个用于顺时针和逆时针,在这些函数中添加您制作路径的方法。我会使用此代码来完成此任务,因为我没有发现任何错误:

 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()等。当然你会有你的Person.position = positionPerson = SKSpriteNode(imageNamed: "name")在您的 didMoveToView 下创建 Sprite 。

在此之后,在 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 设置为 true以及。其他一切都是不言自明的。

关于ios - 如何在 SKAction 中途反转 Sprite 所遵循的路径方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com