如何使用 if 语句增加重力



我已经设置了重力,以便在游戏开始时物体下落得很慢,当玩家收集这些物品时,我希望重力增加。

override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -3.0)
initializeGame()
}

我已经这样设置了重力,但我不确定如何使用 if 语句编辑它。 例如,如果分数 = 20,重力增加。 任何帮助都会很好地说明如何正确执行此操作

你可能想看一个SpriteKit教程。通常,更新属性发生在update(_ currentTime: TimeInterval)方法中。您可以在 SKScene 子类中实现此方法:

override func update(_ currentTime: TimeInterval) {
if (score == 20) { // whatever condition you want
// whatever you want to do
}
}
if (score == 20) { 
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -8.0)
}
Change value of 'dy' as per your requirements

最新更新