Issue with foot sensor

I used the following tutorial to construct my jump:

https://docs.coronalabs.com/tutorial/games/allowJumps/index.html

And I am having an issue.

When I set the sensorOverlaps to 0 and tap on the screen to jump it does not jump.

When I set the sensorOverlaps to 1 and tap on the screen it jumps, but I can jump infinite times.

I only want my character to be able to jump one time.

Please advise on this problem, thank you.

Also, here is the code: 

anim.sensorOverlaps = 0 local function jump(event) if ( event.phase == "began" and anim.sensorOverlaps \> 0 ) then local vx, vy = anim:getLinearVelocity() anim:setLinearVelocity( vx, 0 ) anim:applyLinearImpulse( nil, -225, anim.x, anim.y ) anim:setSequence("jump") anim:play() end end local function sensorCollide(event) if ( event.element1 == 2 and event.object2.objType == "ground" ) then if ( event.phase == "began" ) then anim.sensorOverlaps = anim.sensorOverlaps + 1 elseif ( event.phase == "ended" ) then anim.sensorOverlaps = anim.sensorOverlaps - 1 end end end Runtime:addEventListener("touch", jump) Runtime:addEventListener("collision", sensorCollide)

Would an extra check with the below solve it?

if ( event.phase == “began” and anim.sensorOverlaps > 0 and anim.setSequence ~=“jump” ) then 

*Edit - just set the jump to something else to let it jump again

@graham07, I did this, but nothing else happened. 

local function jump(event) if ( event.phase == "began" and anim.sensorOverlaps \> 0 and anim.setSequence and anim.setSequence ~= "jump") then local vx, vy = anim:getLinearVelocity() anim:setLinearVelocity( vx, 0 ) anim:applyLinearImpulse( nil, -225, anim.x, anim.y ) anim:setSequence("jump") anim:play() end end

Can you post how you created the character physics body?

Sure:

local anim = display.newSprite(parent, sheet, sequenceData ) anim.x = 90 anim.y = 0 anim:setSequence("idle") physics.addBody(anim, "dynamic", { bounce = 0, density = 2}, { box = {halfWidth = 30, halfHeight = 10, x = 0, y = 45}, isSensor = true} ) anim:play() anim.isSleepingAllowed = false anim.isFixedRotation = true anim.sensorOverlaps = 0

OK, thanks. The foot sensor is used in the tutorial for “can I jump?” but not necessarily “prevent player from jumping again”.

@graham07 had the right idea with checking if – following the initiation of the jump – the sequence is already “jump” and thus prevent the player from jumping again (in mid-air). However, he wrote the wrong syntax… it should be " anim.sequence" not “anim.setSequence” (which actually isn’t anything by Corona terms; the function to set a sequence is the object method “object:setSequence()”).

Here’s the property you need to check:

https://docs.coronalabs.com/api/type/SpriteObject/sequence.html

Hope this helps,

Brent

Thanks, so much! Got it to work.

Would an extra check with the below solve it?

if ( event.phase == “began” and anim.sensorOverlaps > 0 and anim.setSequence ~=“jump” ) then 

*Edit - just set the jump to something else to let it jump again

@graham07, I did this, but nothing else happened. 

local function jump(event) if ( event.phase == "began" and anim.sensorOverlaps \> 0 and anim.setSequence and anim.setSequence ~= "jump") then local vx, vy = anim:getLinearVelocity() anim:setLinearVelocity( vx, 0 ) anim:applyLinearImpulse( nil, -225, anim.x, anim.y ) anim:setSequence("jump") anim:play() end end

Can you post how you created the character physics body?

Sure:

local anim = display.newSprite(parent, sheet, sequenceData ) anim.x = 90 anim.y = 0 anim:setSequence("idle") physics.addBody(anim, "dynamic", { bounce = 0, density = 2}, { box = {halfWidth = 30, halfHeight = 10, x = 0, y = 45}, isSensor = true} ) anim:play() anim.isSleepingAllowed = false anim.isFixedRotation = true anim.sensorOverlaps = 0

OK, thanks. The foot sensor is used in the tutorial for “can I jump?” but not necessarily “prevent player from jumping again”.

@graham07 had the right idea with checking if – following the initiation of the jump – the sequence is already “jump” and thus prevent the player from jumping again (in mid-air). However, he wrote the wrong syntax… it should be " anim.sequence" not “anim.setSequence” (which actually isn’t anything by Corona terms; the function to set a sequence is the object method “object:setSequence()”).

Here’s the property you need to check:

https://docs.coronalabs.com/api/type/SpriteObject/sequence.html

Hope this helps,

Brent

Thanks, so much! Got it to work.