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!