I’m new to Corona/Lua and am attempting to make a basic platformer using the built in physics engine. I’m encountering an odd problem. My character jumps up and on to a static platform, but when walking off he never falls. He just continues to move across the screen in mid-air until I hit the jump button again at which point he jumps in the air and then gravity takes him back to the ground. Any ideas?
Here are some pieces of my code:
physics.setGravity(0,50)
– the platform character jumps on
–
block = display.newImage(‘images/green_block.png’, 500, _H - 200)
block.myType = ‘ground’
physics.addBody(block,‘static’,{density=4,bounce=0,friction=1})
groundGroup:insert(block)
– my character’s physics properties
–
physics.addBody(mario,“dynamic”,{density=4,bounce=0,friction=1} )
mario.isFixedRotation = true – makes Mario always upgright, physics won’t cause him to fall over
function jumpButton:touch(event)
– jump if pressed and Mario is not in air
if (event.phase == ‘began’ and marioInAir == false) then
mario:setLinearVelocity(0, -800) – was 800
audio.play(jumpSound)
end
marioInAir = true
end
jumpButton:addEventListener(‘touch’, touch )
– detect if Mario hits something
function onCollision(event)
if (event.object1.myType == ‘ground’ and event.object2.myName == ‘mario’) then
– mario is constantly colliding with the ground
– this prevents the animation for running from
– restarting a lot and causing sprite to stutter.
if (marioInAir == true ) then
mario:setSequence(‘marioRun’)
mario:play()
end
– prevent mid-air jump by setting to false
marioInAir = false
end
end
Runtime:addEventListener(‘collision’,onCollision)