I am trying to make an object double jump, similar to mario style jumping, but whenever I apply a second force or impulse to an object that is in the air, it just shoots straight down to the ground. Is there any way of allowing multiple impulses to an object that is in the air? [import]uid: 116264 topic_id: 20766 reply_id: 320766[/import]
Yes that is possible. How are you applying your second force/impulse? [import]uid: 84637 topic_id: 20766 reply_id: 81665[/import]
At the minute I have one function which is:
local function pressjump (event)
if event.phase == "began" then
ball:applyForce (0, 800)
end
end
jumpbutton:addEventListener("touch", pressjump)
Ideally, you could press this twice double jump, or even better would be to be able to hold it down for a longer jump. I did use a code that used a timer to make the ball jump higher the longer you held the button, but the ball only moved after you released the button. Is there a code for making it jump higher the longer you hold the button, but start to jump as soon as yo press it? [import]uid: 116264 topic_id: 20766 reply_id: 81686[/import]
Maybe you could use
x, y = obj:getLinearVelocity()
--and then something like
obj:applyLinearImpulse(0, y - \*2, obj.x, obj.y)
(untested code, should give you an idea though) [import]uid: 84637 topic_id: 20766 reply_id: 81690[/import]
Ok I will try this. Do I need to put the “x, y = obj:get…” into a function? [import]uid: 116264 topic_id: 20766 reply_id: 81705[/import]
You would put it inside your collision handler (at the top) [import]uid: 84637 topic_id: 20766 reply_id: 81706[/import]
Thanks this works great for double jump. Could this be used to make it so that if you hold the jump button the ball goes higher? [import]uid: 116264 topic_id: 20766 reply_id: 81707[/import]
Great
You could factor in a held time into your button handler and perhaps factor that in.
Again example idea code
local heldTime = 0
heldTime = heldTime + 1
obj:applyLinearImpulse(0, y - \*2 -heldTime / 2, obj.x, obj.y)
You will probably need to play around with that a bit, but that should get you started. [import]uid: 84637 topic_id: 20766 reply_id: 81708[/import]
I’ve added that but it still jumps the same as before:
local heldtime = 0
x, y = ball:getLinearVelocity()
local function pressjump (event)
if event.phase == "began" then
heldtime = heldtime + 1
ball:applyLinearImpulse(0, y - 2 -heldtime / 2, ball.x, ball.y)
elseif event.phase == "ended" then
heldtime = 0
end
end
jumpbutton:addEventListener ("touch", pressjump)
[import]uid: 116264 topic_id: 20766 reply_id: 81709[/import]
Try moving heldtime + 1 outside of the began statement.
[import]uid: 84637 topic_id: 20766 reply_id: 81710[/import]
Ive done that and it still works the same as before [import]uid: 116264 topic_id: 20766 reply_id: 81712[/import]
Ok then , there are other ways you can go about it
create a flag,
local updateTime = false
then in your began phase set it to true , in ended set it to false.
then in your runtime listener, if updateTime is true update the held time. [import]uid: 84637 topic_id: 20766 reply_id: 81713[/import]
Ive added a timer and it now works. This is the code now:
local heldtime = 0
x, y = ball:getLinearVelocity()
local function pressjump (event)
heldtime = heldtime + 1
if event.phase == "began" then
ball:applyLinearImpulse(0, y - 2 -heldtime / 2, ball.x, ball.y)
jumptimer = timer.performWithDelay(50, function() ball:applyLinearImpulse(0, y - 2 -heldtime , ball.x, ball.y) end, 5)
elseif event.phase == "ended" then
heldtime = 0
timer.cancel(jumptimer)
end
end
thanks [import]uid: 116264 topic_id: 20766 reply_id: 81714[/import]
Awesome, glad to help [import]uid: 84637 topic_id: 20766 reply_id: 81715[/import]
How could i modify this in order to let the ball jump longer as you hold the button, but not to make a double jump?
I mean, the ball can only jump again once it hits the ground, no jumping in mid-air.
Awesome code you got here! [import]uid: 105206 topic_id: 20766 reply_id: 98265[/import]
Try this code (you can remove the linear damping and try without that, but it might jump too high without it):
local ballIsGrounded = true
local hold = false
local function pressjump (event)
if event.phase == "began" and ballIsGrounded == true then
ball.linearDamping = 1
ball:applyForce ( 0, -5.5, ball.x, ball.y)
t1 = timer.performWithDelay(1, function() ball:applyForce(0,-1.3,ball.x,ball.y)end,5)
ballIsGrounded = false
hold = true
elseif event.phase == "ended" then
timer.cancel(t1)
hold = false
end
end
jumpbutton:addEventListener ("touch", pressjump)
local function on\_hit (event)
if event.phase == "began" then
ball.linearDamping = 0
ballIsGrounded = true
ball.angularDamping = 5
end
end
ball:addEventListener("collision", on\_hit)
[import]uid: 116264 topic\_id: 20766 reply\_id: 98372[/import]
Thanks for the code man!
I tried doing something pretty similar but for some reason i failed haha
What would be the difference between making the character jump with an impulse or a force?
Also, i didn’t get much info about linear and angular dumping for coronas documentation, what would that control??
Cheers!
EDIT: also, i have an animation for my jumping character. How could i “detect” which part of the jump has been completed (going up, max height, going down) in order to play the correct jump sequence?? [import]uid: 105206 topic_id: 20766 reply_id: 98834[/import]
An impulse is best used for a sudden force, whereas a force is best applied gradually in small amounts. Linear damping slows down an object moving in a straight line, so for example you could keep a ball at a slow, constant speed even if rolling down a hill. Angular damping is similar, but stops an object from rotating as much.
For detecting the phases of the jump you could try something like:
[code]local function jumpSequence()
vx, vy = character:getLinearVelocity()
if vy < 0 then
– function for “going up”
elseif vy > 0 then
– function for “going down”
end
end
Runtime:addEventListener(“enterFrame”, jumpSequence)
[/code]
I’m not too sure where max height would fit in between “going up” and “going down”, as it would only last for a split second before the character would begin to go down.
[import]uid: 116264 topic_id: 20766 reply_id: 98864[/import]
awesome, that was just what i needed! thanks!
[import]uid: 105206 topic_id: 20766 reply_id: 99265[/import]
hello.
I’m trying to do the same thing (double jump). I’ve tried to implement the code that was mentioned ( "x, y = obj:getLinearVelocity()
–and then something like obj:applyLinearImpulse(0, y - *2, obj.x, obj.y) "). But it’s not working for me.
Any ideas?
Thanks.