Single jump help

Hi guys.

 

function onSceneTouch (event) if(event.phase == "began" and playerInAir == false) then playerInAir = true ball:setLinearVelocity( 0, -200 ) print(playerInAir) end end scene:addEventListener("touch",onSceneTouch) -- Detect whether the player is in the air or not function onCollision( event ) if(event.phase == "began") then if(event.object1.myName == "block" and event.object2.myName == "ball") then playerInAir = false; end end end Runtime:addEventListener( "collision", onCollision )

When I try to tap the screen, the ball wont jump.

If I remove the function onCollision, and some of the code in the onSceneTouch it works fine.

Basically I want to allow the ball to jump ONLY if its on the grund ( block)

Hi sindrekristiansen,

Do you only want to check if the ball has collided with the block or do you want any collision to change “playerInAir” to false?

If you want any collision to set it to false, I suggest changing your onCollision function to something simpler, like this:

function onCollision( event ) if ( event.phase == "began" ) then playerInAir = false end end

If you want to set “playerInAir” to false only when the block and ball collide, you must make sure that your block and your ball have the names set. Take a look at this example from the documentation:

local crate1 = display.newImage( "crate.png", 100, 200 ) physics.addBody( crate1, { density = 1.0, friction = 0.3, bounce = 0.2 } ) crate1.myName = "first crate" local crate2 = display.newImage( "crate.png", 100, 120 ) physics.addBody( crate2, { density = 1.0, friction = 0.3, bounce = 0.2 } ) crate2.myName = "second crate" local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " and " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName ) end end Runtime:addEventListener( "collision", onCollision )

If you look at each crate, you will see that they have a property that sets their name. So for your block and ball, you would want to do something like this:

local ball = display.newImage( "ball.png" ) ball.myName = "ball" local block = display.newImage( "block.png" ) block.myName = "block"

This should work. Let us know if it doesn’t.

Hi @sindrekristiansen,

I describe one solution to this in the following tutorial:

http://www.coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best regards,

Brent

Hi sindrekristiansen,

Do you only want to check if the ball has collided with the block or do you want any collision to change “playerInAir” to false?

If you want any collision to set it to false, I suggest changing your onCollision function to something simpler, like this:

function onCollision( event ) if ( event.phase == "began" ) then playerInAir = false end end

If you want to set “playerInAir” to false only when the block and ball collide, you must make sure that your block and your ball have the names set. Take a look at this example from the documentation:

local crate1 = display.newImage( "crate.png", 100, 200 ) physics.addBody( crate1, { density = 1.0, friction = 0.3, bounce = 0.2 } ) crate1.myName = "first crate" local crate2 = display.newImage( "crate.png", 100, 120 ) physics.addBody( crate2, { density = 1.0, friction = 0.3, bounce = 0.2 } ) crate2.myName = "second crate" local function onCollision( event ) if ( event.phase == "began" ) then print( "began: " .. event.object1.myName .. " and " .. event.object2.myName ) elseif ( event.phase == "ended" ) then print( "ended: " .. event.object1.myName .. " and " .. event.object2.myName ) end end Runtime:addEventListener( "collision", onCollision )

If you look at each crate, you will see that they have a property that sets their name. So for your block and ball, you would want to do something like this:

local ball = display.newImage( "ball.png" ) ball.myName = "ball" local block = display.newImage( "block.png" ) block.myName = "block"

This should work. Let us know if it doesn’t.

Hi @sindrekristiansen,

I describe one solution to this in the following tutorial:

http://www.coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

Best regards,

Brent