【发布时间】:2017-06-08 20:38:33
【问题描述】:
根据此处这篇文章的指示; Swift-Making an SKNode simply "move forward" on an angle 我已经编写了这段代码,以随机化一个角度并让我的精灵在那个角度移动:
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 命令,它们工作得很好。那么为什么速度没有变化呢?我的计算机模拟器处理的代码太多了吗?还是我在某个地方犯了错误?如有任何反馈,我将不胜感激。
提前致谢。
【问题讨论】:
-
你忘记创建物理实体了吗?将
physicsBody?更改为physicsBody!。看看会不会崩溃。如果是这样,则意味着您忘记创建物理体。 -
在我的 GameScene.sks 中的每个精灵的属性检查器中,我已经分配了边界圆类型的物理体,但仍然没有任何反应。我需要添加一些东西来在我的 GameScene.swift 中创建它们吗?或者我需要确保属性检查器中的设置以某种方式允许它移动?
-
我也刚试过你的建议,不,它不会崩溃?还有其他想法吗?
-
我的回答对你有用吗?
-
请看我下面的评论,所以不,不完全...
标签: swift xcode velocity angle skaction