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: 23314 reply_id: 323314[/import]

try to use the getLinearVelocity and do not apply the force if the character already has a particular threshold velocity. [import]uid: 3826 topic_id: 23314 reply_id: 93415[/import]

and how do i do that :/?
I’m really new to this. [import]uid: 134147 topic_id: 23314 reply_id: 93507[/import]

Disclaimer: I’m also new to this.

For the left and right wall I would do something like this:

[lua]local cWidth = display.contentWidth;
local function resetBall()

–This will check if the ball’s X position is greater than the screen
–If it is it will snap it back
if ball.x >= cWidth then
ball.x = 5 --(or whatever works for you)
end

–This will do the same but it if it leaves on the left side
if ball.x <= 0 then
ball.x = cWidth - 5;
end
end

Runtime:addEventListener(“enterFrame”, resetBall);[/lua]

That might not work 100% for you but it should be close. Basically when you have the ball.x go to a specific point you can just reset the x position to whatever you want.

For the jumping part, I haven’t done anything like that in Corona yet, but in the Unity engine they had something to check if a physics body was “grounded”.

I don’t know how to check in Corona if your character is grounded.

What I did in Unity was to write an if statement to check if the character was NOT grounded then don’t let it jump.

If I wanted to double jump, I wrote the if statement to check if it isn’t grounded to only let it jump twice then disable the jump.

I have no idea how to do that in Corona but hopefully that helps. [import]uid: 123298 topic_id: 23314 reply_id: 95221[/import]

here’s some drag and drop annotated code you can play with. It does everything you asked for.

If you don’t understand something, just ask.

[code]

physics = require(“physics”) – we need the physics model
physics.start()
physics.setGravity(0,9.8)

local _H = display.contentHeight; --cache the height of the screen
local _W = display.contentWidth; – cache the width of the screen

local canJump = false --controls when jumping is allowed, initially the ball is in the air. We will set this to true when the ball collides with the ground.

local ground = display.newRect(0,0,_W,5) – rectangle is the width of the screen.
ground.x = _W/2
ground.y = _H - ground.contentHeight/2
ground:setFillColor(0,255,0) --R, G & B values respectively.
physics.addBody(ground, “static”, {friction = 2.5, bounce=0.2})
ground.myName = “ground” – assigns a name to determine which physics object collides with another.

local function spawnBall() --doesn’t have to be in a function, but you may want multiple balls. each time this function is called a new ball will spawn.
ball = display.newCircle(0,0,35) – create a ball
ball.x = _W/2 – centre of the screen on x axis
ball.y = _H/2 – centre of screen on y axis
ball:setFillColor(255,0,0) --R, G & B values respectively.
physics.addBody(ball, {friction = 2.5, bounce=0.2, radius=35 })
ball.myName = “ball” – assigns a name to determine which physics object collides with another.
end
spawnBall() – spawns a ball using the above function

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

Runtime:addEventListener( “touch”, onScreenTouch )

function stopMovementOffSides() – *** this will stop the ball going off the sides, though it doesn’t work here as it doesn’t move and right!
if ball.x < 0 then
ball.x = 0
end

if ball.x > _W then
ball.x = _W
end
end
Runtime:addEventListener(“enterFrame”, stopMovementOffSides ) – this calls the named function every frame, (30 times per second)

function onLocalCollision( self, event ) – function to work out local collisions.
if ( event.phase == “began” ) then
if (self.myName == “ball” and event.other.myName == “ground”) then
canJump = true – *** when the ball hits the ground, we say he can jump ***
end
print( self.myName … ": collision began with " … event.other.myName )
end
end

ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )
[/code] [import]uid: 67933 topic_id: 23314 reply_id: 95241[/import]