Alex Alex - 1 year ago 128
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 Source

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:

touching = true

if touching {
    player.physicsBody?.applyImpulse(CGVectorMake(0, 1))

touching = false

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download