Limiting the jumps?

Hey. I’m new to coding and such, How do I limit my jumps for my character?. If i kinda tap alot on the screen it will go far upp away.

ball = display.newImage (“character.png”)
ball.x = 70
ball.y = 350
physics.addBody(ball, {friction = 2.5, bounce=0.2, radius=35 })

local function onScreenTouch( event )
if event.phase == “began” then
– make ball jump
ball:applyForce( 0, -15, ball.x, ball.y )
end

return true
end

Runtime:addEventListener( “touch”, onScreenTouch )

Thanks.

Oh and,…

Another question…

When my character gets controlled by the accelerometer and it tilts to much to either side. The character goes “off screen” and can’t get back. How do i make it when it goes off screen that it comes back to the other side. So if it would go all the way to left wall(
Thanks! [import]uid: 134147 topic_id: 23317 reply_id: 323317[/import]

[lua]if ball.x < 0 then
ball.x = 480
end
if ball.x > 480 then
ball.x = 0
end[/lua] [import]uid: 10389 topic_id: 23317 reply_id: 93443[/import]

It still doesnt work. :confused:
I tried switching ball.x to ball.y since its about jumping in height.

:confused: [import]uid: 134147 topic_id: 23317 reply_id: 93505[/import]

My answer was for question #2 :slight_smile:
You can stop it going off the top of the screen by testing for ball.y

[lua]if ball.y > 320 then
ball.y = 320
end[/lua]

Or use 380 or a similar number to stop it going TOO far off the top of the screen if you do want it to disappear for a bit. [import]uid: 10389 topic_id: 23317 reply_id: 93635[/import]

Try this… It should make it so that you can only jump while touching another physics body, I.E. the ground.

ball = display.newImage ("character.png")  
ball.x = 70  
ball.y = 350  
physics.addBody(ball, {friction = 2.5, bounce=0.2, radius=35 })  
jumping = false  
  
local function onScreenTouch( event )  
if (event.phase == "began" and jumping == false) then  
-- If ball is currently not jumping, make it jump and set jumping to true.  
ball:applyForce( 0, -15, ball.x, ball.y )  
jumping = true  
end  
  
--Check if the ball can jump (sets jumping to false when colliding with another Body.)  
function jumpCheck(event)  
 if(event.phase == "began") then  
 jumping = false  
 end  
end   
return true  
end  
  
Runtime:addEventListener( "touch", onScreenTouch )  
ball:addEventListener (“collision”, jumpCheck)  

[import]uid: 131400 topic_id: 23317 reply_id: 93840[/import]