How to increase gravity using an if statement

47 views Asked by At

I have set my gravity so that at the start of my game the objects fall quite slow and as the player collects these items i want the gravity to increase.

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

I have set the gravity like so but i am unsure on how to edit it with an if statement. E.g. if score = 20 gravity increases. any help would be great on how to properly do this

2

There are 2 answers

0
Sid Mani On

You might want to look through a SpriteKit tutorial. Generally, updating properties happens in the update(_ currentTime: TimeInterval) method. You can implement this method in your SKScene subclass:

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