Corona SDK and other Game Development Tutorials
@gamebuilding

How to make a character jump with Linear Force

Part 1

In this short video, you’ll learn how to make a character jump using Corona SDK. To make the character jump, you’ll learn how to require physics, start physics, and physic bodies to the game objects.

At a minimum, you’ll need a blank project with Corona, a ground image, and a player image. You can download the project below; the project files include the starter project along with the images and source code.

Download the project files

In case you didn’t want to watch the video and you just want to study the source code, I’ve copied and pasted it below.

display.setStatusBar(display.HiddenStatusBar)
local physics = require "physics"
physics.start()

local ground = display.newImage("ground.png")
    ground.x = 160
    ground.y = 460
    physics.addBody(ground, "static")

local player = display.newImage("player.png")
    player.x = 160
    player.y = 300
    physics.addBody(player)

local function onTouch(event)
    if(event.phase == "began") then         
        player:setLinearVelocity( 30, -200 )
    end
end
Runtime:addEventListener("touch", onTouch)

Thank’s for visiting That’s So Panda!


Part 2

In this next video, you’ll learn how to make a character jump left or right based on where the player touches. If the player touches to the left of the player, we’ll make the player jump left. If the player touches to the right of the player, we’ll make the player jump right.

We only need to change the function onTouch to make this work and here’s the code that was changed in the video:

local function onTouch(event)
    if(event.phase == "began") then 
        if(event.x < player.x) then 
            -- jump left
            player:setLinearVelocity(-30, -200)
        else 
            -- jump right
            player:setLinearVelocity(30, -200)
        end
    end
end

Thanks again for visiting and watching!

 

1 Comment
  1. How can i refine the jumps to just 1 jump because on mine overtime i click it jumps

Leave a Reply