Make An Object Jump Only Once

I am making an app that is a simple game with a ball. I have it set up that when you touch anywhere on the screen the ball bounces. The only problem that I have is that you can double bounce and keep pressing the button and the object will always stay in the air. Here is my code for jumping…

physics.addBody( circle, “dynamic”,{ density=1, friction=0.5, bounce=0.3 })
local onTouch = function(event)
timer.performWithDelay(1000, event )
circle:applyLinearImpulse(5,-3, circle.x, circle.y)
end
Runtime:addEventListener(“touch”, onTouch) [import]uid: 82872 topic_id: 19601 reply_id: 319601[/import]

its fairly easy, i use a function like that:

[lua]local function jump(event)
print(person.y)
if event.phase == “ended” then
if person.y > person.y0 - 20 then
person:applyForce(0,-50, person.x, person.y)
end
end
return true
end[/lua] [import]uid: 16142 topic_id: 19601 reply_id: 75720[/import]

Thank you for responding to my post. I am still confused on how to combine the code together to make the ball bounce. I am using the variable “circle” as an object. [import]uid: 82872 topic_id: 19601 reply_id: 75727[/import]

That was my first post on the Corona Blogs and I did not understand the tags(but now I do). Thanks for everyones help so far but I still have a question about the code. I would like the ball to only bounce once (I don’t want the ability to double or triple jump). For some reason the (jump_complete) is not working . Instead of using an “and” statement I used an “or” statement and it would let me double and triple jump. With the code right now it does not do anything. Thanks for everyones help and I apologize for being so new to Corona. This is my first iPhone App and I am just getting used to Corona.

[code]

local function jump(event)

if (event.phase == “ended” and jump_complete == “false”)then
jump_complete = false;
circle:setLinearVelocity(0, -250)

end
end
Runtime:addEventListener(“touch”, jump)
[/code] [import]uid: 82872 topic_id: 19601 reply_id: 75845[/import]

@ BWernsman

Did you get it to work? I havent been working on my apps because of the holiday so I did not check out the forums just wondering if you still need help? [import]uid: 30314 topic_id: 19601 reply_id: 76079[/import]

I have not gotten it to work, can you check out the code in my post above and see why it won’t let the ball jump. If I change the statement to “or” it will let me double and even triple jump (what I don’t want to happen). Thanks for your help. [import]uid: 82872 topic_id: 19601 reply_id: 76113[/import]

@ BWernsman

You keep saying you want the ball to bounce which I will assume it means to jump if thats the case here is a quick code I did for you and tested and it jumps 1x then cant jump again. You can always change things like gravity, bounce, friction ect but this should get you going.

Hope this helps.

[lua]local physics = require (“physics”)
physics.start()
physics.setGravity(0, 10)

local jump_completed = false

local circle = display.newCircle(0, 0, 20)
circle.x = 240
circle.y = 290
physics.addBody(circle, “dynamic”, {bounce=0.1})

local floor = display.newRect(0, 0, 480, 1)
floor.x = _W
floor.y = 320
physics.addBody(floor, “static”, {bounce = 0, friction = 0.1} )
floor.isVisible = true

local function circle_jump(event)
if(event.phase == “ended” and jump_completed == false)then

jump_completed = true
circle:setLinearVelocity(0, -250)

end
end

Runtime:addEventListener(“touch”, circle_jump) [import]uid: 30314 topic_id: 19601 reply_id: 76182[/import]

@ BWernsman

Please use lua tags when posting your code its a lot easier to read. Base on your question you said you wanted make it jump? but you said “I have it set up that when you touch anywhere on the screen the ball bounces.” I will assume thats the same as jumping. Its fairly easy to do and there are few ways to do this, for instance is your ball in a specific place and you want a regular jump or every time is on air you dont want it to jump but when it falls jump again? Here is a quick sample hope this helps.If you only need 1 jump you can add a flag like local jump_complete = false and once you jump you add jump_complete = true.

[lua]local function jump(event)
if(event.phase == “ended”)then
– if (event.phase == “ended” and jump_complete == false)then – This is if you want 1 jump
ball:setLinearVelocity(0, -250)
– jump_complete = true – this would be the flag
end
end

background:addEventListener(“touch”, jump) [import]uid: 30314 topic_id: 19601 reply_id: 75774[/import]

I am terrible at describing things but I meant to have the object jump once and then when it goes back to it started, you can jump again. I am going to link a video to the post and you can see what I meant. Just click the link and it will take you to my dropbox folder where you can watch the video.

http://db.tt/v62L6nwZ [import]uid: 82872 topic_id: 19601 reply_id: 76314[/import]

Here is What i came up to for making object jump continuously on hitting the ground

[lua]require(“physics”)
physics.start()
local background=display.newRect(0,0,768,1024)
background:setFillColor(255,255,255)

local wall=display.newRect(0,1000,1024,15)
wall:setFillColor(0,0,0)
physics.addBody(wall,“static”,{bounce=.5,friction=.5})

local ball = display.newCircle( 400, 400, 30 )
ball:setFillColor(128,128,128)
physics.addBody(ball,“dynamic”,{bounce=.5,friction=.5})
local function onCollision(event)
if event.phase == “ended” then
ball:setLinearVelocity(0, -250)
end
end

ball:addEventListener(“collision”,onCollision)[/lua] [import]uid: 82446 topic_id: 19601 reply_id: 76348[/import]

@ BWernsman

You have a few different ways to do this you could set a collision function like rahulsingh2k10 explained the only problem with his code and I dont think you want that is every there is a collision the ball will jump and im going to assume you want to make it jump by touching the screen or button or ball ect? This sample should give you the right idea there are other ways to doing this you could keep a Runtime checking for a collision event or a timer for when ball jumps in X seconds you can jump again or instead of the ball having a collision function the floor can so every time ball hits floor you jump again ect… it all depends on you but this should get you started. Im a bit late on reply been busy hope this helps

Best of Luck,
LeivaGames

[lua]local physics = require (“physics”)
physics.start()
physics.setGravity(0, 10)

local jump_completed = false

local circle = display.newCircle(0, 0, 20)
circle.x = 240
circle.y = 290
physics.addBody(circle, “dynamic”, {bounce=0.1})

local floor = display.newRect(0, 0, 480, 1)
floor.x = _W
floor.y = 320
physics.addBody(floor, “static”, {bounce = 0, friction = 0.1} )
floor.isVisible = true

local function circle_jump(event)
if(event.phase == “ended” and jump_completed == false)then

jump_completed = true
circle:setLinearVelocity(0, -250)

end
end
Runtime:addEventListener(“touch”, circle_jump)

local function on_hit(event)
if(event.phase == “began”)then

jump_completed = false

end
end

circle:addEventListener(“collision”, on_hit) [import]uid: 30314 topic_id: 19601 reply_id: 76378[/import]

could you tell me please which is the best method to do such things? [import]uid: 82446 topic_id: 19601 reply_id: 76385[/import]

That is exactly what I wanted, thank you so much. Can you please clarify what you are asking rahulsingh2k10? [import]uid: 82872 topic_id: 19601 reply_id: 76446[/import]