Check if character is done jumping

I have a jump  button that on touch will make my character jump. I want to allow only a single jump at a time, I am not sure how to achieve this. I tried checking getLinearVelocity() and see if it is set to 0 but no luck so far.

Here is my code, please help.

local canJump; local function isJumping(self, event) canJump = sprite:getLinearVelocity() end --Runtime Even local function isJump( event ) if(canJump == 0 ) then print( sprite:getLinearVelocity( ) ) sprite:setLinearVelocity( 0, -200 ) sprite:pause() sprite:setSequence("jump") sprite:play( ) else sprite:setLinearVelocity( 0, 0 ) sprite:pause() sprite:setSequence("rest") sprite:play( ) end end -- Touch even

This is the code I used to determine my player’s jump and it worked pretty well in my case:

function getVelocities() vx, vy = character:getLinearVelocity() end function endJump() if character ~= nil then character:applyForce(0, 0, character.x, character.y) else end end physics.setDrawMode("hybrid") function touchScreen(event) if (event.phase == "began") then if (vy \> -25 and vy \< 25) then character:applyForce(0, -85, character.x, character.y) timer.performWithDelay(400, endJump, 1) end end end

I had to make a Runtime Listener for touchScreen() and getVelocities().

Some changes you should make:

First, getLinearVelocity returns two values, so you need two variables.

Secondly, do not check whether it is equal to 0, as this is rarely the case try [lua] vy > -10 and vy < 10 [/lua] for a range, making the range smaller can allow a larger challenge to the player, and the possibility of a lucky double jump.

Third, when you stop the jump, do not set the linearVelocity to zero all of a sudden, try applyForce().

Another option is to use a “foot sensor” body element which I outlined quite some time ago in this tutorial:

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

It’s an old tutorial but the principle remains the same as back then.

Brent

This is the code I used to determine my player’s jump and it worked pretty well in my case:

function getVelocities() vx, vy = character:getLinearVelocity() end function endJump() if character ~= nil then character:applyForce(0, 0, character.x, character.y) else end end physics.setDrawMode("hybrid") function touchScreen(event) if (event.phase == "began") then if (vy \> -25 and vy \< 25) then character:applyForce(0, -85, character.x, character.y) timer.performWithDelay(400, endJump, 1) end end end

I had to make a Runtime Listener for touchScreen() and getVelocities().

Some changes you should make:

First, getLinearVelocity returns two values, so you need two variables.

Secondly, do not check whether it is equal to 0, as this is rarely the case try [lua] vy > -10 and vy < 10 [/lua] for a range, making the range smaller can allow a larger challenge to the player, and the possibility of a lucky double jump.

Third, when you stop the jump, do not set the linearVelocity to zero all of a sudden, try applyForce().

Another option is to use a “foot sensor” body element which I outlined quite some time ago in this tutorial:

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

It’s an old tutorial but the principle remains the same as back then.

Brent