Reputation: 791
I am trying to create something like Doodle Jump for practice.
For those who do not know the game; you have a player that jumps on bricks that are located on random places and does not collide with them if they are above him and jumps of them whenever he lands on them.
I have troubles achieving the jumping behavior of the original Doodle jumper, explained below.
I have set the collision bit mask to none for both the player and bricks. Whenever player lands on the brick (then the player is above the brick when 'didBeginContact' is triggered) I apply an impulse on the player, but because the collision is detected more than once, the impulse is then really really big and the player jumps really high (I want him to have a limited speed).
I tried this (this function is called when the collision is detected):
func playerCollidedWithBrick(brick: Brick) {
if player.position.y > brick.position.y && brick.physicsBody?.velocity.dy <= 0 {
//the player is above the brick and is falling
player.physicsBody?.applyImpulse(CGVectorMake(0, 20))
}
}
But then the player won't jump of other bricks than the one he jumped off first - not what I want.
I want him to jump of any brick he lands on but with a limited velocity. I also like the effect of applying impulse on him - sort of like a spring effect when he lands on the brick.
How could I achieve this? Any ideas or suggestions are appreciated.
Upvotes: 0
Views: 49
Reputation: 6061
Why don't you put a custom property in your Brick class that designates whether or not the player has already hit that brick
var playerHitBrick: Bool = false
func playerCollidedWithBrick(brick: Brick) {
if player.position.y > brick.position.y && brick.physicsBody?.velocity.dy <= 0 && !brick.playerHitBrick {
//the player is above the brick and is falling
brick.playerHitBrick = true
brick.runAction(SKAction.waitForDuration(1.0), completion: { brick.playerHitBrick = false })
player.physicsBody?.applyImpulse(CGVectorMake(0, 20))
}
}
this way it will fire the first time the player hits a brick but won't count subsequent hits while the player is leaving the brick
Upvotes: 1