How to prevent unlimited jump in game?

okay so I have a jump button and a runtime listener function for when the jump button is pressed.

So whenver the jump button is pressed I apply linear impulse to the game hero.
 

local function controls(event) if(event.phase=="began") then if (buttonpressed.id=="jump") then hero:applyLinearImpulse(0,-10,hero.x,hero.y) end elseif(event.phase=="ended") then end end

    Now problem is if if the user keeps on tapping the jump button then the hero keeps on going up. I cant think of anyhting to counter this. One thing I could do is change the above code to:

local function controls(event) if(event.phase=="began") then if (buttonpressed.id=="jump" and hero.y\>display.contentHeight/2) then hero:applyLinearImpulse(0,-10,hero.x,hero.y) end elseif(event.phase=="ended") then end end

But this would still allow the jumping button to work until half of the screen is reached.
Kindly help me on this.

Thanks

 

When you create hero, set a hero.jumping flag to false. When the user taps, check .jumping is false. If it is, set to true and do the jump.

Then check for the hero colliding with the ground again and set jumping back to false.

clarifying on what nick_sherman said

hero.jumping = false local function controls(event) if(event.phase=="began") then if (buttonpressed.id=="jump") then if hero.jumping == false then hero:applyLinearImpulse(0,-10,hero.x,hero.y) hero.jumping = true end end elseif(event.phase=="ended") then end end

when your hero collides with the ground, add this line of code

hero.jumping = false

Thanks for the reply!

But what if the hero is already on a point above the ground , for instance on a stone wall.
Then the ground flag condition wont allow him to jump.

Thanks

There’s several options - you could measure the vertical velocity of the hero, if it’s below a certain value, he’s not jumping/falling.

You could add a flag to each object, indicating whether it is a ‘ground’ object or not, i.e. the actual ground or a platform. If the hero collides with the top edge of any of these, set jumping to false. 

Thanks!

object:getLinearVelocity()    solved the problem!

When you create hero, set a hero.jumping flag to false. When the user taps, check .jumping is false. If it is, set to true and do the jump.

Then check for the hero colliding with the ground again and set jumping back to false.

clarifying on what nick_sherman said

hero.jumping = false local function controls(event) if(event.phase=="began") then if (buttonpressed.id=="jump") then if hero.jumping == false then hero:applyLinearImpulse(0,-10,hero.x,hero.y) hero.jumping = true end end elseif(event.phase=="ended") then end end

when your hero collides with the ground, add this line of code

hero.jumping = false

Thanks for the reply!

But what if the hero is already on a point above the ground , for instance on a stone wall.
Then the ground flag condition wont allow him to jump.

Thanks

There’s several options - you could measure the vertical velocity of the hero, if it’s below a certain value, he’s not jumping/falling.

You could add a flag to each object, indicating whether it is a ‘ground’ object or not, i.e. the actual ground or a platform. If the hero collides with the top edge of any of these, set jumping to false. 

Thanks!

object:getLinearVelocity()    solved the problem!