Linear Impulse Help

I currently have a button which when pressed makes a ball jump. This is the code:

local function pressjump (event)
 if event.phase == "began" then
 ball:applyLinearImpulse( 0, 10, ball.x, ball.y )
 end
end
jumpbutton:addEventListener ("touch", pressjump)

Is there any way of making it so that the longer you press the button, the higher the ball goes? [import]uid: 116264 topic_id: 20562 reply_id: 320562[/import]

Yes, you could use a timer and apply a greater impulse each second, or apply impulse again, once per second, while the button was held down. (Depending on if you jump on press or release.)

Peach [import]uid: 52491 topic_id: 20562 reply_id: 80791[/import]

I’ve tried using some timers but I can’t get it to work [import]uid: 116264 topic_id: 20562 reply_id: 80835[/import]

Run this plug and play code, it’s not the greatest but it shows you how this might be done;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 8 )

local jumpHeight = 100

local floor = display.newRect( 0, 470, 320, 10 )
local player = display.newRect( 140, 400, 40, 40 )
player:setFillColor(200,0,0)

physics.addBody(floor, “static”, {bounce = 0})
physics.addBody(player, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0})

local jumpBtn = display.newCircle( 290, 400, 20 )

local function jump(event)
if event.phase == “began” then
jumpTimer = timer.performWithDelay(10, function() jumpHeight=jumpHeight+5 end, -1)
elseif event.phase == “ended” then
player:setLinearVelocity(0,-jumpHeight)
timer.cancel(jumpTimer)
jumpHeight = 100
end
end
jumpBtn:addEventListener(“touch”, jump)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 20562 reply_id: 80981[/import]

Thanks this works great! I was just wondering though if it would be possible to make the ball jump as soon as I touch the button, not when I release it? [import]uid: 116264 topic_id: 20562 reply_id: 81076[/import]

Don’t worry ive solved it. thanks for your help
[import]uid: 116264 topic_id: 20562 reply_id: 81105[/import]

Glad to hear that, not a problem.

Peach :slight_smile: [import]uid: 52491 topic_id: 20562 reply_id: 81181[/import]

I have been using a code to make the ball jump similar to the one you recomended, although I have added a bit so that you can’t jump again until you hit the ground. However, if I press and hold the jump button, wait until the ball has landed, and then release the jump button, the jump button stops working. Is there a way to fix this? The code is :

local jumpCount = 800
local ballIsGrounded = true

local function pressjump(event)
 if (event.phase == "began" and ballIsGrounded == true and game\_end == false ) then
 jumpTimer = timer.performWithDelay(150, function() ball:applyForce(0,1300, ball.x, ball.y); jumpCount = 0 end, 1)
 elseif (event.phase == "ended" and ballIsGrounded == true and game\_end == false ) then
 ball:applyForce(0,jumpCount, ball.x, ball.y)
 timer.cancel(jumpTimer)
 jumpCount = 800
 ballIsGrounded = false
 end
end

jumpbutton:addEventListener ("touch", pressjump)

local function on\_hit (event)
 if event.phase == "began" then
 ballIsGrounded = true
 ball.angularDamping = 0
 print ("ball\_collision")
 end
end

ball:addEventListener("collision", on\_hit)

[import]uid: 116264 topic_id: 20562 reply_id: 81703[/import]

Try moving;

[lua]ballIsGrounded = false[/lua]

up a bit so that it is in the began phase rather than the ended phase - I think that might do it :slight_smile:

Peach [import]uid: 52491 topic_id: 20562 reply_id: 81805[/import]

Thanks I’ve moved it and it works great! [import]uid: 116264 topic_id: 20562 reply_id: 81977[/import]

Not a problem :slight_smile: [import]uid: 52491 topic_id: 20562 reply_id: 82130[/import]