I am developing a side scroller game and running into an issue with getting the hero to reliably jump, I have a GameScene class that calls the jump function from the hero class every time the screen is tapped. Below is the code from the hero class that handles jumping.
The problem I'm having is that the hero doesn't always jump when it's supposed to. Anyone have any ideas on how better to implement the jump? I just want it to jump on every tap, if it's already jumpping than a 2nd tap should not cause a double jump.
note: My hero extends SKSpriteNode
// rendered on each frame
func update() {
if isOnGround == true && jumpNow == true && isJumping == false {
isOnGround = false
groundY = position.y - 1
isJumping = true
jumpNow = false
runAnimation()
}
if isJumping == true {
runJumpAnimation()
self.physicsBody!.applyImpulse(CGVector(dx: 0.0, dy: 300))
isJumping = false
}
/
if isJumping == false && isOnGround == false && (position.y == previousY) {
isOnGround = true
}
previousY = position.y
}
// called from game scene on every touches began.
func jump() {
if (position.y == previousY) { // I suspect the problem is here, but this is the only way I can keep the hero from double jumping
jumpNow = true
}
}