Rae Tucker
Rae Tucker

Reputation: 523

Swift SpriteKit: How to make a sprite gain slight velocity when it collides with an object?

Im creating a game that where you tap to start the player to make him jump, and then as he goes up, more and more objects generate for you to collect to gain points (think of doodle jump). I have it set up so this will happen, but instead of being given a boost by the object, he just floats up out of the scene. Id also like to know how to remove the object from the scene when the player touches it. Thanks in advance(:

Here is my code for making him gain velocity when he touches the object:

func bounceOff() {
    player.physicsBody?.affectedByGravity = false
    player.physicsBody?.applyImpulse(CGVectorMake(0, 10))
    let advance = SKAction.moveByX(0, y: 10, duration: 5)

    runAction(advance)
}

Upvotes: 1

Views: 109

Answers (1)

neo
neo

Reputation: 2042

Your object goes off the screen because you cancel gravity. The impulse you apply pushes up, but there is no force pulling down. As to removing node from the scene. You can use node's removeFromParent method.

Upvotes: 1

Related Questions