3

I'm using the following methods to make my character jump:

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

    if onGround && !gameOver {
        self.character.physicsBody?.applyImpulse(CGVectorMake(0, 75))
        self.onGround = false
    }

}

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

}

This works fine, but I want to make the character jump a certain height based on the length of the touch until a maximum. I've tried something with frame durations but this didn't work.

How can I make the character jump based on touch length until a maximum height?

Nick Groeneveld
  • 895
  • 6
  • 18

2 Answers2

1

Start a timer action when your touch begins and end it when the touch ends. Contained inside would be something like the following,

var force = 0.0

let timerAction = SKAction.waitForDuration(1.0)
let update = SKAction.runBlock({
    if(force < 100.0){
        force += 1.0
    }
})
let sequence = SKAction.sequence([timerAction, update])
let repeat = SKAction.repeatActionForever(sequence)
self.runAction(repeat, withKey:"repeatAction")

Then in your touchesEnded, remove the action and use the value of force to execute your jump.

self.removeActionForKey("repeatAction")
self.character.physicsBody?.applyImpulse(CGVectorMake(0, force))
swelsh
  • 154
  • 3
1

You can disable the body's velocity on the y axis as you lift your finger and if you want to cap the maximum jump height use an optional variable to store the initial y position just before the jump and check the delta between your current y poistion to the initial y position:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if onGround && !gameOver {
        if self.initialJumpY == nil {
           self.initialJumpY = self.character.position.y
        }

        if self.character.position.y - self.initialJumpY < <Your cap value> {            
           self.character.physicsBody?.applyImpulse(CGVectorMake(0, 75))              
        } else {
           self.character.physicsBody?.velocity.dy = 0.0
        }

        self.onGround = false
}

and reset it when done:

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
         self.character.physicsBody?.velocity.dy = 0.0
         self.initialJumpY = nil
    }

declare the initialJumpY in your node class where the touch methods are defined:

class MyNode: SKNode {

    var initialJumpY: Float?

}
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • Where (and how) do I declare the initialJumpY value? – Nick Groeneveld May 14 '15 at 10:22
  • My playable character is an SKSpriteNode, not a class. Does this still apply? – Nick Groeneveld May 14 '15 at 10:38
  • I got the following error message: [error message link to image](http://oi57.tinypic.com/1038leh.jpg) – Nick Groeneveld May 14 '15 at 11:15
  • 1
    remove the ; after } and change velocity.y to velocity.dx. – giorashc May 14 '15 at 11:32
  • On the line `if self.character.position.y - self.initialJumpY! < 100 {` I got the following error: `fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)` – Nick Groeneveld May 14 '15 at 11:39
  • 1
    my mistake, see my last edit. you need to set the initialJumpY only when it is nil. Try and understand the logic and you should easily overcome any errors that appear. – giorashc May 14 '15 at 11:47
  • You're right, thanks for that! It works. An additional question, if I may: Can I make the jump more 'natural' with ease in and ease out? – Nick Groeneveld May 14 '15 at 11:58
  • 1
    Yes, you can try using the delta between the initial y position and current y position to affect the impulse value. – giorashc May 14 '15 at 12:05
  • Can you help me out with that? – Nick Groeneveld May 14 '15 at 12:18
  • 1
    Try opening another question and me or anyone else will be happy to help you. But first try doing it yourself and show what you tried. As a start try using the self.character.position.y - self.initialJumpY value as the impulse value – giorashc May 14 '15 at 12:25
  • I have one query regarding this .. What is maximum Impulse rate for iPhone or iPad. Actually I am making a game . In this I have multiple characters , All characters jump decided in % size , Like 30% Jump , 40% Jump . How to get this . Please help me .. – Jogendra.Com May 21 '15 at 11:21