Reputation: 29
I would like to know how to change the speed of falling nodes in sprite kit using swift, I have tried by changing the gravity, but when it goes very fast it starts to crash. I have done this, it works, but as I said it crashes:
var velocity:CGFloat = 0
override func update(currentTime: CFTimeInterval) {
velocity = CGFloat(score*3)
self.physicsWorld.gravity = CGVectorMake(0, -velocity)
}
Thank you!
Upvotes: 0
Views: 1487
Reputation: 20274
Instead of changing the scene's gravity, you can apply a force on the nodes.
Disable the gravity
self.physicsWorld.gravity = CGVectorMake(0,0)
Set the name property of each falling node with it's declaration
node.name = @"fallingNode"
Then, in the update Function
self.enumerateChildNodesWithName("fallingNode", usingBlock: {
(node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
// do something with node or stop
node.physicsBody?.applyForce(velocity)
return
})
Upvotes: 1