gpt4 book ai didi

Swift-SKSpriteNode 速度不变

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:24 25 4
gpt4 key购买 nike

根据此处帖子的说明; Swift-Making an SKNode simply "move forward" on an angle我写了这段代码,让一个角度随机化,并让我的 Sprite 在那个角度移动:

func setDegrees() {
let ball1Degrees:UInt32 = arc4random_uniform(360)
if ball1Degrees > 300 {
ball2Degrees = Int(ball1Degrees) - 360
ball3Degrees = Int(ball1Degrees) - 300
}
else if ball1Degrees > 240 {
ball2Degrees = Int(ball1Degrees) + 60
ball3Degrees = Int(ball1Degrees) - 360
}
else {
ball2Degrees = Int(ball1Degrees) + 60
ball3Degrees = Int(ball1Degrees) + 120
}
}

func myFunction() {
ball1.position = CGPoint(x: 0, y: 0)
ball2.position = CGPoint(x: 0, y: 0)
ball3.position = CGPoint(x: 0, y: 0)
setDegrees()
if ball1Degrees < 90 {
ball1.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball1Degrees) * (.pi / 180)))
}
if ball1Degrees == 90 {
ball1.physicsBody?.velocity = CGVector(dx: 1, dy: 0)
}
if ball1Degrees > 90 && ball1Degrees <= 180 {
ball1.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball1Degrees) * (.pi / 180)))
}
if ball1Degrees > 180 && ball1Degrees <= 270 {
ball1.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball1Degrees - 180) * (.pi / 180)))
}
if ball1Degrees > 270 && ball1Degrees <= 0 {
ball1.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball1Degrees) * (.pi / 180)))
}
if ball2Degrees < 90 {
ball2.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball2Degrees) * (.pi / 180)))
}
if ball2Degrees == 90 {
ball2.physicsBody?.velocity = CGVector(dx: 1, dy: 0)
}
if ball2Degrees > 90 && ball2Degrees <= 180 {
ball2.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball2Degrees) * (.pi / 180)))
}
if ball2Degrees > 180 && ball2Degrees <= 270 {
ball2.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball2Degrees - 180) * (.pi / 180)))
}
if ball2Degrees > 270 && ball2Degrees <= 0 {
ball2.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball2Degrees) * (.pi / 180)))
}
if ball3Degrees < 90 {
ball3.physicsBody?.velocity = CGVector(dx: 1, dy: tan(Double(ball3Degrees) * (.pi / 180)))
}
if ball3Degrees == 90 {
ball3.physicsBody?.velocity = CGVector(dx: 1, dy: 0)
}
if ball3Degrees > 90 && ball3Degrees <= 180 {
ball3.physicsBody?.velocity = CGVector(dx: -1, dy: tan(Double(180 - ball3Degrees) * (.pi / 180)))
}
if ball3Degrees > 180 && ball3Degrees <= 270 {
ball3.physicsBody?.velocity = CGVector(dx: -1, dy: -tan(Double(ball3Degrees - 180) * (.pi / 180)))
}
if ball3Degrees > 270 && ball3Degrees <= 0 {
ball3.physicsBody?.velocity = CGVector(dx: 1, dy: -tan(Double(360 - ball3Degrees) * (.pi / 180)))
}

}

myFunction()

但是当我的游戏运行时,上面的速度变化似乎都没有被应用。重新定位到屏幕中心成功运行,甚至还有一些在这里不是特别相关的缩放 SKAction 命令,它们工作得很好。那么为什么速度没有变化呢?是不是我的计算机模拟器无法处理太多代码?还是我在某处犯了错误?如果有任何反馈,我将不胜感激。

提前致谢。

最佳答案

有几件事你做错了。但它们确实很容易修复。

首先,在我的另一个回答中,我提到了 dx : dy 的比率,而不是 dx 和 dy 的。目前,您的节点速度非常慢,因此您看不出有什么不同。将 dx 和 dy 乘以 100:

ball1.physicsBody?.velocity = CGVector(dx: 100, dy: -tan(Double(360 - ball1Degrees) * (.pi / 180)) * 100)

另外,这个 if 语句不会被执行:

if ball1Degrees > 270 && ball1Degrees <= 0 {

我认为你应该尝试:

if ball1Degrees > 270 && ball1Degrees <= 360 {

关于Swift-SKSpriteNode 速度不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821550/

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