Jump height depending on how long you hold button

Hi Everyone,

Does anyone have any sample code they can share that will make a player in a game jump, but the height of their jump depends on how long they hold the jump button? An example of this would be any of the super mario bros games. a quick tap makes the player jump small and a longer hold will make the player jump higher. Any help is appreciated. Thanks!

[import]uid: 31262 topic_id: 13365 reply_id: 313365[/import]

I don’t have code, but I would look at event.time - http://developer.anscamobile.com/reference/index/eventtime-2

You could set it up in a way that jump is 5 if held for 10 milliseconds and jump is 10 if held any longer. [import]uid: 14218 topic_id: 13365 reply_id: 49039[/import]

Would this only work if your player jumped upon release of the jump button kinda like in ChocoRun? I’m not sure how I would apply this when I need the player to jump as soon as the button is touched.

The jump sample I am using uses applyLinearForce and you seem to have to set the force at the beginning of the jump. [import]uid: 31262 topic_id: 13365 reply_id: 49040[/import]

What if you keep applying force while the button is pressed (until you reach a set limit) and then immediately set the force to zero when the button is let go and let gravity bring the player back down? [import]uid: 22392 topic_id: 13365 reply_id: 49042[/import]

@Chunky I was fooling around with that but I’ve never worked with any type of jumping so I’m very inexperienced. I thought of cancelling the jump force when the button is released however I can’t see any way of cancelling applyLinearForce once it has begun. [import]uid: 31262 topic_id: 13365 reply_id: 49047[/import]

Yeah I don’t think you can cancel it but how about setting it to 0 once the button is realeased or you hit maximum jump height? [import]uid: 22392 topic_id: 13365 reply_id: 49072[/import]

I did try that before and it didn’t work.

I was going through the documentation online and found this:

“A common question is the difference between applying “force” and “impulse” to a body. The difference is that an impulse is meant to simulate an immediate “kick” to the body, but force (and torque) is something exerted over time. Therefore, to get a realistic force simulation, you should reapply the force on every frame, perhaps for several seconds, or as long as you want the force to continue. You can use an “enterFrame” event for this purpose.”
I wonder if I can have it apply a small amount of repeating force while the button is down, then stop it once the button is released? Does that sound right to anyone? Has anyone successfully accomplished what I’m looking to do? [import]uid: 31262 topic_id: 13365 reply_id: 49079[/import]

I still can’t figure this out. Anyone have thoughts or ever get this working? [import]uid: 31262 topic_id: 13365 reply_id: 49307[/import]

Try adding an event listener for “tap” and for “touch”. The “touch” listener runs the selected function as long as the finger is down, so you could have it add height to the object or apply a linear impulse the whole time. The “tap” function only accepts a tap so it can be a different function. Example:
[lua]function jumpFunction
–I use a smaller impulse here because it will be held longer
myBody:applyLinearImpulse( 0, 100, myBody.x, myBody.y )
end

function jumpFunction2
myBody:applyLinearImpulse( 0, 200, myBody.x, myBody.y )
end

jumpBtn:addEventListener(“tap”, jumpFunction)
jumpBtn:addEventListener(“touch”, jumpFunction2)[/lua]

I haven’t tested this code but I think the theory should work If applied correctly. [import]uid: 63320 topic_id: 13365 reply_id: 49404[/import]

@dacriburdan Thanks for replying.

It seems the tap listener fires when you lift your finger off the button. This causes the jump to fire twice, once when you initially put your finger on the screen for the “touch” listener, then once again when you lift your finger off for the “tap” listener. With this method it seems the tap listener will never register on its own. [import]uid: 31262 topic_id: 13365 reply_id: 49468[/import]

Did you figure out how to do this already? [import]uid: 5942 topic_id: 13365 reply_id: 50557[/import]

Hi jbverschoor, unfortunately I haven’t. I’ve messaged a couple Corona developers who have released games that accomplish this but haven’t received any replies yet. I’ve gotten some feedback on how this may be done but haven’t been able to convert it to code yet. If I ever figure it out I will post it here. [import]uid: 31262 topic_id: 13365 reply_id: 50577[/import]

Here is a quick example on how to accomplish that

it looks very long since i took it from one of my projects

can be shorter but longer is more understandable

you can change the speed of the jump from the first variable

local PlayerUpDownSpeed = 300

i put a touch listener for the whole screen

but you can make it for a jump button only

[lua]display.setStatusBar( display.HiddenStatusBar )

local physics = require (“physics”)

local PlayerUpDownSpeed = 300
local MovePlayerUp = 0
local MovePlayerDown = 0


–*******************************************************–
local function SpawnPlayer()

Player = display.newCircle( 100 , 100 , 16 )

physics.addBody( Player , “daynamic” ,{ bounce = 0 , friction = 0 , density = 1 , radius = 16 } )

Player.isFixedRotation = true

end
–*******************************************************–


–*******************************************************–
local function CreateGround()

local ground = display.newRect( 0 , 300 , 480 , 2 )

physics.addBody( ground , “static” ,{ bounce = 0 , friction = 0 , density = 1 } )

end
–*******************************************************–


–*******************************************************–
local function MovePlayerUpOrDown()

if MovePlayerUp == 1 then

Player:setLinearVelocity( 0 , - PlayerUpDownSpeed )

else

vx , vy = Player:getLinearVelocity()

Player:setLinearVelocity( vx , vy )

end

end
–*******************************************************–


–*******************************************************–
local function Touch(event)

if event.phase == “began” then

MovePlayerUp = 1

elseif ( event.phase == “ended” or event.phase == “canceled” ) then

MovePlayerUp = 0

end

end
–*******************************************************–


–*******************************************************–
local function EnterFrame(event)

MovePlayerUpOrDown()

end
–*******************************************************–


–*******************************************************–
local function InitPhysics()

physics.start( true )

physics.setScale( 100 )

physics.setDrawMode( “normal” )

physics.setGravity( 0 , 9.8 )

end
–*******************************************************–


–*******************************************************–
local function InitGame()

InitPhysics()

CreateGround()

SpawnPlayer()

end
–*******************************************************–

InitGame()

Runtime:addEventListener( “touch”, Touch )
Runtime:addEventListener( “enterFrame” , EnterFrame )[/lua] [import]uid: 46546 topic_id: 13365 reply_id: 50590[/import]

Wow thanks Mko, it seems so simple and if this works for me then I was definitely on the right track. I’ll test this out tonight after the kids are in bed and report back. Appreciate your time!

P.S. is this project on the app store? If so what is the name of it? [import]uid: 31262 topic_id: 13365 reply_id: 50633[/import]

you’re most welcome :slight_smile: , i hope it is will work for you
well it is not in the app store yet but it will be some time later. [import]uid: 46546 topic_id: 13365 reply_id: 50665[/import]

@mko2111

OK so just to report back, I put together a test using your code and it works! I also added code to not allow another jump while already in the air, and also added code to limit the jump so if you held the jump button it wouldn’t keep raising the character infinitely. Thanks again! [import]uid: 31262 topic_id: 13365 reply_id: 50749[/import]

@aaaron
Would you mind posting your additional code? [import]uid: 22392 topic_id: 13365 reply_id: 50795[/import]

Sure.

This is what I did to disallow jumping in mid-air. I modified mko2111’s Touch function slightly by putting a check to see if the player is touching the ground. This is accomplished by creating a listener to see if the character is colliding with the ground, if not it sets player.canJump = false. If it is colliding with the ground, player.canJump = true.
If the character is touching the ground, I store the initial Y value of the character before it jumps (playerY)

After I initiated the jump using mko2111’s method, I have an enterFrame listener checking to see when the player’s Y value decreases by 50 pixels. Once it does I cancel the jump and the character lets gravity pull it back down. This is how I set the maximum jump height.

  
local function Touch(event)   
 if event.phase == "began" then  
 --Check to see if character is in the air or not  
 if player.canJump then  
 --This line stores the Y value of the character when the jump button is pressed  
 playerY = player.y  
 MovePlayerUp = 1  
 end  
 elseif ( event.phase == "ended" or event.phase == "canceled" ) then  
 MovePlayerUp = 0  
 end   
end  
--This function cancels the setLinearVelocity when the characters Y value goes up by 50 pixels. You can change the 50 to a higher value to make the jump limit higher.  
  
local function finishJump( event )  
 if player.y \< (playerY - 50) then   
 MovePlayerUp = 0  
 end  
end  
  
Runtime:addEventListener( "enterFrame" , finishJump )  
  

Hope this makes sense. I don’t know if this is necessarily the best way to do it.
[import]uid: 31262 topic_id: 13365 reply_id: 50830[/import]

This is very cool. I was having issue with this.

Basically, I was looking at making (I dropped the idea, got excited about something else) a hot air balloon type thing.

Hold the button to go higher, let go to fall etc.
This is perfect for that. Might have to work on it again :slight_smile:
Oh btw @.aaaron could you post code from mko2111 and what you took out or merged with? I’m trying to understand the not allowing to jump part with ground.

I was never a programmer until corona sdk 89 days ago (none, zero, nada, etc lol).

ng [import]uid: 61600 topic_id: 13365 reply_id: 50985[/import]

@nicholasclayg

mko2111’s code will probably work well for you then. I actually don’t have to post it. He so kindly posted it in this very thread :slight_smile: Scroll up to post #12. Good luck with your idea! [import]uid: 31262 topic_id: 13365 reply_id: 50989[/import]