Limiting jumping?

I made a successful jump button on my little platform game that I’m working on to help me learn, however whenever I press it I can continue to spam it as much as I want, vanishing wildly in the air.
Is there a way to limit my character’s ability to jump only while touching the ground? I’m using a simple applyForce with no other conditions if that matters.
Thanks.
p.s. Here’s all the code related to my jumping button.

local jumpButton = display.newImage("jumpButton.png")  
jumpButton.x = display.contentWidth - 75  
jumpButton.y = display.contentHeight - 85  
  
function jumpButton:tap(event)  
ball:applyForce(0, -600, ball.x, ball.y)  
end  
  
jumpButton:addEventListener("tap", jumpButton)  

[import]uid: 131400 topic_id: 23081 reply_id: 323081[/import]

@ dustinreeves100

Hey there the reason your character jumps nonstop is because in your function its telling the ball to jump every time the button is pressed and its doing what you set it to do. If you would like to limit the jump you can set a flag and when your character jumps the flag changes and when he lands it changes again to allow it to jump again. If that sounds confusing ill post a quick code snippet I havent tested it but it should work.

1: Set a flag, for example jump = false
2: When you jump change jump = true
3: when you land change jump = false

And in your function if jump = false then you can jump. Here is a quick snippet base on your code above.
[lua]local jump = false
local jumpButton = display.newImage(“jumpButton.png”)
jumpButton.x = display.contentWidth - 75
jumpButton.y = display.contentHeight - 85

function jumpButton:tap(event)
if(jump == false) then
ball:applyForce(0, -600, ball.x, ball.y)
jump = true
end
end

jumpButton:addEventListener(“tap”, jumpButton)

function check(event)
if(event.phase == “began”)then
jump = false
end
end

ball:addEventListener(“collision”, check)
[import]uid: 30314 topic_id: 23081 reply_id: 92291[/import]

Thanks so much! I managed to fix it thanks to you.
Randomly off-topic, but any idea why I’m not able to jump while holding the move button(s) for my character? Here’s the full code.

[code]
local physics = require( “physics” )
physics.start()
–physics.setDrawMode( “hybrid” )

– Shapes
local dirtPlatformShape = { -97,-99, -98,-124, 98,-123, 98,-100 }
dirtPlatformShape.density = 1; dirtPlatformShape.friction = 0.3; dirtPlatformShape.bounce = 0.0;

local smalldirtPlayformShape = { -98.5,-32.5, 99.5,-31, 99,-50, -98.5,-50 }
smalldirtPlayformShape.density = 1; smalldirtPlayformShape.friction = 0.3; smalldirtPlayformShape.bounce = 0.0;
local dirtPlatform_3 = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform_3.x = 211
dirtPlatform_3.y = 475
physics.addBody( dirtPlatform_3, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)

local dirtPlatform_2 = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform_2.x = 529
dirtPlatform_2.y = 475
physics.addBody( dirtPlatform_2, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)

local dirtPlatform = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform.x = 794
dirtPlatform.y = 475
physics.addBody( dirtPlatform, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)

local ball = display.newImageRect( “ball.png”, 32, 32 )
ball.x = 521
ball.y = 304
physics.addBody( ball, { density=1, friction=1, bounce=0.,} )
ball.isFixedRotation = true

local ground = display.newImageRect( “ground.png”, 1024, 50 )
ground.x = 512
ground.y = 600
physics.addBody( ground, “static”, { density=1, friction=1, bounce=0 } )
–Draw the Walls
local leftWall = display.newRect(-8,0, 16, 793)
leftWall:setFillColor(255,255,255)
leftWall.isVisible = false
physics.addBody( leftWall, “static”, { density=1, friction=1, bounce=0 } )

local rightWall = display.newRect(1016,0, 16, 793)
rightWall:setFillColor(255,255,255)
rightWall.isVisible = false
physics.addBody( rightWall, “static”, { density=1, friction=1, bounce=0 } )
–Draw the movement buttons
local leftButton = display.newImage(“leftButton.png”)
leftButton.x = display.contentWidth / 10
leftButton.y = display.contentHeight - 85

local rightButton = display.newImage(“rightButton.png”)
rightButton.x = display.contentWidth / 4
rightButton.y = display.contentHeight - 85

local jumpButton = display.newImage(“jumpButton.png”)
jumpButton.x = display.contentWidth - 75
jumpButton.y = display.contentHeight - 85

– Movement Functions
function moveLeft(event)
ball.x = ball.x - 3
end

function moveRight(event)
ball.x = ball.x + 3
end

–Movement
function leftButton:touch(event)
if (event.phase == “began”) then
Runtime:addEventListener(“enterFrame”, moveLeft)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveLeft)
display.getCurrentStage():setFocus(event.target, nil);
end
end

function rightButton:touch(event)
if (event.phase == “began”) then
Runtime:addEventListener(“enterFrame”, moveRight)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveRight)
display.getCurrentStage():setFocus(event.target, nil);
end
end

function jumpButton:tap(event)
if ball.canJump then
ball:applyForce(0, -600, ball.x, ball.y)
end
end
–Limiting the jump of the player
local function onCollision(self, event )
if ( event.phase == “began” ) then
ball.canJump = true
elseif ( event.phase == “ended” ) then
ball.canJump = false
end
end

–Setting the ball collision to onCollision
ball.collision = onCollision

–Event Listeners
leftButton:addEventListener(“touch”, leftButton)
rightButton:addEventListener(“touch”, rightButton)
jumpButton:addEventListener(“tap”, jumpButton)
ball:addEventListener( “collision”, ball)
[/code] [import]uid: 131400 topic_id: 23081 reply_id: 92295[/import]

Dustin, did you make sure you enabled multitouch somewhere in your app?

system.activate( "multitouch" )  

It looks like you’re on the right track with setFocus otherwise. [import]uid: 87138 topic_id: 23081 reply_id: 92298[/import]

I must have left that snippet out. It’s enabled and the problem remains. :/. Thanks for the quick response, by the way. [import]uid: 131400 topic_id: 23081 reply_id: 92299[/import]

No problem; as I say, you’re on the right track. But I think you probably need to change that jumpButton “tap” event into a “touch” listener so you can control the focus properly. [import]uid: 87138 topic_id: 23081 reply_id: 92301[/import]

Fantastic… Making the following changes, as you said, fixed it. Thanks so much :slight_smile:

function jumpButton:touch(event)  
 if (event.phase == "began" and ball.canJump) then  
 ball:applyForce(0, -600, ball.x, ball.y)  
 display.getCurrentStage():setFocus(event.target, event.id);  
 else display.getCurrentStage():setFocus(event.target, nil);  
 end  
end  

Let’s jump off topic further… How would I go about making a collision check on certain aspects of my “ball”, for example the bottom of it, like making my “ball” able to jump on an enemy and destroy it.

Would I need to create a small invisible object that follows my “ball” that acts as a collision checker between itself and another object, such as the enemy?
Sorry if I’m getting WAY off topic here, but it’s pretty exciting to see this coming together as much as it is. :stuck_out_tongue: [import]uid: 131400 topic_id: 23081 reply_id: 92302[/import]