0

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!

Ma new L
  • 29
  • 1
  • 9
  • what do you mean by starts to crash? and why not assign the velocity to the falling body itself? – giorashc Apr 06 '15 at 10:32
  • @giorashc it starts to shake and disappear and appear. How can i assign the velocity to the falling body itself? – Ma new L Apr 06 '15 at 10:39

1 Answers1

1

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
    })
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
  • it gives me this error: Cannot invoke 'applyForce' with an argument list of type '(StringLiteralConvertible, usingBlock: (SKNode!, UnsafeMutablePointer) -> Void)' – Ma new L Apr 06 '15 at 13:03
  • According to http://stackoverflow.com/a/26022360/2043580 you need to just add return at the end of the closure. Please check the edit. – ZeMoon Apr 06 '15 at 13:13