Alex Alex - 2 months ago 12
Swift Question

How to make sprite jump higher/smaller depending on the amount of time touching screen?

Hi I'm having difficulties finding the correct code to apply to my sprite to allow small jump when only taped and a higher jump when finger is on screen longer. (Please find current code below)

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



/* Called when a touch begins */

if (gameOver == 0){

//Player Begin Jumping.
player.physicsBody?.applyImpulse(CGVectorMake(0, 200))
player.runAction(SKAction .playSoundFileNamed("sounds/Jump.caf", waitForCompletion: true))

}

}

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

if (gameOver == 0){

//Player End Jump.
player.physicsBody?.applyImpulse(CGVectorMake(0, -120))

Answer

You can use the update-method. and in your touchesBegan-method you set a boolean or something like that to show the update-method that you are still pressing on the screen. For example:

//touchesBegan
touching = true

//update-method
if touching {
    player.physicsBody?.applyImpulse(CGVectorMake(0, 1))
}

//touchesEnded
touching = false

You have to change the applyImpulse so that it fits your needs.

Comments