Avoid a double jump.

Hello, I have been trying to avoid the double jump, I have look across many forums posts and still can’t figured it out why i cant make it to work. I have 2 problems, The first one is that i am able to double jump when I keep tapping to jump, the second is that if the character goes down and I keep tapping I can slow down the “gravity” so it takes longer for my character to get on the floor. What am i doing wrong?

 local perso = display.newImage( "perso.png" ) perso:translate(0,display.contentHeight- 150) physics.addBody(perso, "dynamic", { density=1.0, friction=0,bounce=0}) perso:setLinearVelocity( 0, 0 ) local canJump = false local vx, vy = perso:getLinearVelocity() function bougerPerso(event) if event.phase == "began" and canJump==true then if event.x \>= display.contentCenterX then perso:setLinearVelocity( 200, 0 ) elseif event.x \<= display.contentCenterX then perso:setLinearVelocity( -200, 0 ) end elseif event.phase == "ended" then perso:setLinearVelocity( 0, 0 ) end end local function jump(event) if event.numTaps == 2 and canJump==true then canJump=false if event.x \>= display.contentCenterX then perso:setLinearVelocity( 100, -300 ) elseif event.x \<= display.contentCenterX then perso:setLinearVelocity( -100, -300 ) elseif perso:LinearVelocity(0 , 0 ) then perso:setLinearVelocity( 0, -300 ) end end end function perso:collision(event) if event.other.type == 'sensor' then --remove camera self.parent :kill() self:kill() pageFin() elseif event.other.unObstacle == 'plancher' then canJump = true elseif event.other.type =='portalfin' then self.parent :kill() self:kill() pageFinNiveau() end end

Based on a quick look through, is it possible that before your character has a chance to leave the floor, the collision detection resets canJump to true?

One way to solve that is only to set canJump to true if the character’s linear velocity means he is moving downwards and he hits the ground, rather than just being in contact with the ground. You can getLinearVelocity on each frame, store it on the object and then compare it to the previous value. If it’s greater than the previous frame, he is/was falling and you can safely set canJump to true on contact with the ground.

Try putting in print statements to check where and how often canJump is set to false or set to true.

That does make sense, I tried adding this to my collision

 function perso:collision(event) local vx,vy= perso:getLinearVelocity() if event.other.type == 'sensor' then --remove camera self.parent :kill() self:kill() pageFin() elseif event.other.unObstacle == 'plancher'and vy \>=0 then if event.phase=='began'then canJump = true end end

It does work now, because i do not have a double jump, but if i keep jumping without stopping it still slow down the character to move down. Any ideas? 

Thanks again for the reply!

Hi @maximus-007,

I wrote the following tutorial a long time ago (4+ years) but it still holds up in practice. Basically, I like using a “foot sensor” element with a multi-element body to determine when my character is touching the ground, and whether he/she should be allowed to jump. Check it out and see if this can help you.

https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best,

Brent

Hey guys, thanks for your answer!

Sorry for taking so long to respond, but yet I still haven’t managed to make this work!

Brent the way you did it looks really similar too mine, do you have any idea what i am doing wrong? If you look the above I have the same variable as you do wich is: canJump. 

Thanks again!

Hi @maximus-007,

The “canJump” property is just one aspect… the “foot sensor” is the other aspect, and it’s essential to getting it working like I wrote in the tutorial. Basically, that “foot sensor” is what intersects with the ground to tell Corona when to allow a jump and when to prevent a double-jump.

Best regards,

Brent

Based on a quick look through, is it possible that before your character has a chance to leave the floor, the collision detection resets canJump to true?

One way to solve that is only to set canJump to true if the character’s linear velocity means he is moving downwards and he hits the ground, rather than just being in contact with the ground. You can getLinearVelocity on each frame, store it on the object and then compare it to the previous value. If it’s greater than the previous frame, he is/was falling and you can safely set canJump to true on contact with the ground.

Try putting in print statements to check where and how often canJump is set to false or set to true.

That does make sense, I tried adding this to my collision

 function perso:collision(event) local vx,vy= perso:getLinearVelocity() if event.other.type == 'sensor' then --remove camera self.parent :kill() self:kill() pageFin() elseif event.other.unObstacle == 'plancher'and vy \>=0 then if event.phase=='began'then canJump = true end end

It does work now, because i do not have a double jump, but if i keep jumping without stopping it still slow down the character to move down. Any ideas? 

Thanks again for the reply!

Hi @maximus-007,

I wrote the following tutorial a long time ago (4+ years) but it still holds up in practice. Basically, I like using a “foot sensor” element with a multi-element body to determine when my character is touching the ground, and whether he/she should be allowed to jump. Check it out and see if this can help you.

https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best,

Brent

Hey guys, thanks for your answer!

Sorry for taking so long to respond, but yet I still haven’t managed to make this work!

Brent the way you did it looks really similar too mine, do you have any idea what i am doing wrong? If you look the above I have the same variable as you do wich is: canJump. 

Thanks again!

Hi @maximus-007,

The “canJump” property is just one aspect… the “foot sensor” is the other aspect, and it’s essential to getting it working like I wrote in the tutorial. Basically, that “foot sensor” is what intersects with the ground to tell Corona when to allow a jump and when to prevent a double-jump.

Best regards,

Brent