# Increasing CGFloat while Tap

This topic is 1070 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I want to increase a CGFloat while a tap is going on.

There is a Player node, which should always jump after the Screen is tapped.

But the Player should have a minimum and a maximum jump, and also can jump between those set constants.

The longer the Screen is touched, the longer the jump until the maximum Jump, but this should go while the Screen is touched,

not after.

With my Code, the Player jumps after the Screen is touched or after the maximum Jump is true.

func jump(force : CGFloat){

if(self.isCharacterOnGround){

self.Player.physicsBody?.applyImpulse(CGVectorMake(0, force))
self.isCharacterOnGround = false
}

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)

self.pressed = true

let timerAction = SKAction.waitForDuration(0.0)

let update = SKAction.runBlock({
if(self.force < Constants.maximumJumpForce){
self.force += 2.0
}else{
self.jump(Constants.maximumJumpForce)
self.force = Constants.maximumJumpForce
}
})
let sequence = SKAction.sequence([timerAction, update])
let repeat = SKAction.repeatActionForever(sequence)
self.runAction(repeat, withKey:"repeatAction")
}
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)

self.removeActionForKey("repeatAction")

self.jump(self.force)

self.force = Constants.minimumJumpForce

self.pressed = false

}
}

override func update(currentTime: NSTimeInterval) {

}
Edited by Nistan

##### Share on other sites
I see no code that runs each frame and checks for self.pressed, adding jump impulses each check that returns true.

L. Spiro

##### Share on other sites

The self.pressed is for whenever the Player is on ground again, while the Screen is still touched, he jumps again.

##### Share on other sites

self.pressed is, according to just the code you posted, whether or not the finger is touching the screen.

It doesn’t matter.  I repeat exactly the same thing I said before.

I see no per-frame code, anywhere.  I see no game loop.  I see no reason you would get the result you expect from your first post.

L. Spiro

##### Share on other sites

With my Code, the Player jumps after the Screen is touched or after the maximum Jump is true.

Thats because in your update block you only call jump once the force is greater than the maximum jump:

if(self.force < Constants.maximumJumpForce){

self.force += 2.0
}else{
self.jump(Constants.maximumJumpForce)
self.force = Constants.maximumJumpForce
}

You also call jump when you end touches.

@ L. Spiro  The code he is posting is using Apples SpriteKit.  He is trying to do the jump logic as an SKAction which is executed by Spritekits internal game loop.

##### Share on other sites

Yes, because I need to keep track of the Minimum and Maximum Force.

The Player does not only jump after maximum Jump, but also in between minimum and maximum.

The only problem I face is that I want to have the Player jumping, and the longer you tap the longer the jump will be,

but you cant change the force the Player jumped after the jump has happend/ while the jump is happening.

##### Share on other sites

but you cant change the force the Player jumped after the jump has happend/ while the jump is happening.

Why?

L. Spiro

##### Share on other sites

but you cant change the force the Player jumped after the jump has happend/ while the jump is happening.

Why?

Thats exactly where my problem is.

The original plan was to increase the force as long as the screen is tapped until the maximum, but the force should be applied meanwhile and not after.

But If I keep track in the update method or elsewhere, the increased force will be implied the next time the screen is touched, not while the screen is touched.

##### Share on other sites
You are setting self.pressed to true when touched, false when not being touched anymore.
Why can’t you, every frame, check this value and add a force if it is true?

L. Spiro

##### Share on other sites

Yes, because I need to keep track of the Minimum and Maximum Force.

The Player does not only jump after maximum Jump, but also in between minimum and maximum.

The only problem I face is that I want to have the Player jumping, and the longer you tap the longer the jump will be,

but you cant change the force the Player jumped after the jump has happend/ while the jump is happening.

Using your code the player will:
Only jump whilst holding the tap AND the maximum force is reached:
OR
If the maximum force is NOT reached AND he is still on the ground he will jump after the tap is released.

You need to rethink how you are doing this.  L. Spiro is offering you good advice.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631709
• Total Posts
3001844
×