Making a Character Jump

Hello! In my last post, we learned how to move a character left and right and you can view the post right here – http://thatssopanda.com/corona-sdk-tutorials/moving-a-character-left-and-right-with-corona-sdk/. In today’s post, we will tackle how to make a character jump.

If you haven’t already, jump back to my old post and download the project. This post will be building on the last post.

Our first step in making our character jump is to add a Jump button.

-- Add Jump button
local up = display.newImage ("images/btn_arrow.png")
	up.x = 440; up.y = 280;
	up.rotation = 270;

After we have added the jump button, we will attach an Event Listener to the jump button. When the player touches the jump button, we will make the character jump. Here’s the code.

-- Make character jump
function up:touch(event)
	if(event.phase == "began") then
		guy:setLinearVelocity( 0, -200 )
	end
end
up:addEventListener("touch",up)

Now for the explanation. We attached a listener to the jump button by adding up:addEventListener(“touch”,up). This let’s Corona SDK know that we want to trigger an event when the up button is touched.

Then, we created a function called up:touch and passed the event parameter into the function. The event parameter let’s us get some more information on how the button was interacted with. In our case, we just want to know when the player touched the button, so we use an if statement to see if the event was in the began phase.

Once the event is in the began phase, we apply a small Linear Velocity to the player to simulate a jump. And that’s it! This is one of the simplest jumps that you can implement in your Corona SDK project today.

In later posts, we will refine the jumping to only allow for single jumping or only allow double jumping. We will also tackle some advanced jumping so the player doesn’t ‘float’ down. If you would like to see more tutorials, please leave a comment. This helps keep me motivated to continue to bring high quality tutorials to your site. As always, you can download the full source below.You can download the full project below and Thanks for reading!

Download Source Code


4 Comments
  1. Hi, Just saw this tutorial and it was EXACTLY what I needed for my game. Great post! I`m still having problems with the player “floating down” to the ground after jumping though. Is there a follow-up tutorial for this that can tell me how to fix that? I`m looking online for it now. My game is a Parallax Scroller.

    Thanks

    Walter

  2. Hi,I need some help here.I am trying to make my character jump on tapping the background.But on starting game in simulator, I keep on tapping the background but the character doesn’t jump and suddenly after some time when i tap the character jumps normally.Can you please tell me why there is a lag between my tap and the character’s jump?

  3. Well done tutorial
    My game is using your method. Now I have an idea, how to get condition where user tap and hold then the character will jump and turn slowly to the platform. What i ask is a jump like game Zombie Tsunami. I can use began and ended event, but which better i should change? The velocity or gravity?

    Thanks for ur attention

Leave a Reply