I can make player jump with the following code. I depend on the gravity to pull the player down to the ground after applying impulse and this really look like real world jump. If I set the gravity to 0,0, then is there any way to make look-alike real world jumping as if we turn gravity on?
local physics = require "physics"
physics.start()
physics.setGravity(0,40)
local grass = display.newRect(0,460,480,20)
grass.name = "grass"
local player = display.newCircle(160,440,20)
physics.addBody(player,{ friction=0, bounce=0})
physics.addBody(grass,"static",{friction=0, bounce=0})
local function onJump(event)
if player.canJump == true and event.phase== "began" then
player:applyLinearImpulse(0, -0.35, player.x, player.y)
end
end
Runtime:addEventListener("touch",onJump)
local function onCollision(self, event )
if ( event.phase == "began" ) then
if event.other.name=="grass" then
player.canJump = true
end
elseif ( event.phase == "ended" ) then
if event.other.name=="grass" then
player.canJump = false
end
end
end
player.collision = onCollision
player:addEventListener( "collision", player )
I hope someone can share me the code. Thank you.
Steve [import]uid: 84159 topic_id: 14153 reply_id: 314153[/import]
[import]uid: 52491 topic_id: 14153 reply_id: 52086[/import]