0

I'm working on a jumper iOS game where the 'player' moves on a wave. It should be able to jump and land on the wave. In the current version it jumps but doesn't move back down.

It does work when the platform is not moving, but the moving platform is crucial for the app.

Thanks, guys!

Code

The platform 'characterBaseline' is moving based on this function

wave.bar.position.y = CGFloat(Double(wave.bar.position.y) + createSinWave(angle))

func createSinWave(angle:Double) -> Double {

    var sinWave = sin(angle)

    return sinWave

}

The player's jump and moves are based on the following function

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    if self.onGround {
        self.velocityY = -14.0
        self.onGround = false
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    if self.velocityY < -7.0 {
        self.velocityY = -7.0
    }

}

override func update(currentTime: NSTimeInterval) {

    // Character op wave
    self.character.position.y = CGFloat(Double(self.character.position.y) + createSinWave(angle))
    self.characterBaseline = self.character.position.y

    // Jump
    self.velocityY += self.gravity
    self.character.position.y -= velocityY

    if self.character.position.y < self.characterBaseline {
        self.character.position.y = self.characterBaseline
        velocityY = 0.0
        self.onGround = true
    }

    // Golf
    updateWaveBarPosition()

}
Nick Groeneveld
  • 895
  • 6
  • 18
  • Not really sure if this is what you are looking for but see this question: http://stackoverflow.com/q/28030259/2158465 – Epic Byte May 07 '15 at 20:13

1 Answers1

1

You'll want to find out what self.gravity is set to and what velocityY comes out after touchesEnded, that's more than likely the source of your problem

On a side note, any reason why you're not using the built in physics?

It's very easy to create a jumping action with SpriteKit's native physics engine.

You just need to add a physicsbody to each sprite (the player and the platform)

self.player.physicsBody = SKPhysicsBody(bodyWithRectangleOfSize:self.player.size)

Once this is happening you can just run something like this in touchesBegan:

self.player.physicsBody.applyImpulse(0, 100)

That'll give your player a boost of 100 (you can tweak this to suit how high you want your player to jump). Gravity will sort the rest out for you.

RyanOfCourse
  • 832
  • 1
  • 8
  • 15
  • Thanks, Ryan. I'm using 'self.character.physicsBody = SKPhysicsBody' for collisions and physics. Is that what you mean? I'll try out the way of jumping you've suggested right now. – Nick Groeneveld May 07 '15 at 17:12
  • 1
    Yeah that should help you out a lot. There's a very detailed tutorial of how to set up all the physics stuff here http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners – RyanOfCourse May 07 '15 at 17:16