I have two objects, a ball and a platform.
When the screen is touched, the ball’s linear velocity is set to make it jump.
function background:touch( event ) if event.phase == "began" then print( "Touch event began on:bg " ) ball:setLinearVelocity( 0, -200 ) print ("linear velocity set") end return true end background:addEventListener( "touch", background )
I am trying to make a platform jumper like Doodle Jump and my platform logic is as follows. If the ball has a negative y linear velocity than allow it to pass through platforms, but if it has a positive y linear velocity, then allow it to rest on platforms.
I tried to write this function with a listener to replicate the above, but it does not work.
function ball:getLinearVelocity( event ) local vx, vy = ball:getLinearVelocity() if vy \< 0 then physics.removeBody( platform ) if vy \> 0 then physics.addBody( platform, "static", { bounce=0.0, friction=0.3 } ) end background:addEventListener( "getLinearVelocity", ball )
Am I approaching this whole problem incorrectly or do I just have issues with my code? If they are just code issues, how can I fix them?