onCollison event problem!

Hey there,

I have a minor issue, there are 3 variables, the character the ground and the objects, when my character jumps it is detected as if he has collided with the ground. How can i prevent this from happening?

Many thanks,

James

You can obtain the names of the colliding objects and use an if statement to check the name of the object is not the ground. The names of the objects in the collision handler function will be ‘object1’ and ‘object2’ which is not ideal, but you can place a variable on the display objects when you add them which gives them an identifier name. Hopefully this code will make sense:

-- add the display objects and give them a variable called 'name' which stores their name obj1 = display.newImageRect("obj1.png",125,44) obj1.anchorX = 0.5 obj1.anchorY = 0.5 obj1.x = display.contentCenterX obj1.y = display.contentCenterY obj1.myname = "obj1" physics.addBody(obj1) obj2 = display.newImageRect("obj2.png",125,44) obj2.anchorX = 0.5 obj2.anchorY = 0.5 obj2.x = display.contentCenterX obj2.y = display.contentCenterY obj2.myname = "obj2" physics.addBody(obj2) -- the two colliding objects are 'object1' and 'object2' in the onCollision function function onCollision( event ) if ( event.phase == "began" ) then print( "collision began with : " .. event.object1.myname .. " and " .. event.object2.myname ) if event.object1.myname ~= 'floor' and event.object2.myname ~= 'floor' then -- collision code end end end Runtime:addEventListener( "collision", onCollision )

Hey again,

Thank you very much for helping me out I just have one problem heres my code:

ground = display.newImage("newground.png") ground.x = 500 ground.y = 540 ground.name = "louie" physics.addBody( ground, "static", {density=1.0, friction=1.0, bounce=0.0, } ) screenGroup:insert(ground) skater = display.newImage("skater.png") skater.x = 230 skater.y = 540 skater.xScale = 0.3 skater.yScale = 0.3 physics.addBody( skater, { friction=1.0, density=1.0, bounce=0.15, radius=10} ) skater:applyForce( 0, -100, skater.x, skater.y ) skater.name = "james" skater.isFixedRotation = true screenGroup:insert(skater) function onCollision(event)     if event.skater.name == "james" and event.ground.name == "louie" then     print("Hit the ground.") else     introOptions = {     effect = "crossFade",     time = 2000     }     storyboard.gotoScene("intro", introOptions)             end         end  

Tweaked your code a little bit so that it would work for me as I may have more objects for him to jump over in the future.

However it’s giving me an error:

“attempt to index field ‘skater’ (a nil value)”

but skater is quite clearly there and it’s definitely not a nil value?

any help is welcome thank you for the help so far aswell.

James

fixed the problem you were completely right thanks!

James 

You can obtain the names of the colliding objects and use an if statement to check the name of the object is not the ground. The names of the objects in the collision handler function will be ‘object1’ and ‘object2’ which is not ideal, but you can place a variable on the display objects when you add them which gives them an identifier name. Hopefully this code will make sense:

-- add the display objects and give them a variable called 'name' which stores their name obj1 = display.newImageRect("obj1.png",125,44) obj1.anchorX = 0.5 obj1.anchorY = 0.5 obj1.x = display.contentCenterX obj1.y = display.contentCenterY obj1.myname = "obj1" physics.addBody(obj1) obj2 = display.newImageRect("obj2.png",125,44) obj2.anchorX = 0.5 obj2.anchorY = 0.5 obj2.x = display.contentCenterX obj2.y = display.contentCenterY obj2.myname = "obj2" physics.addBody(obj2) -- the two colliding objects are 'object1' and 'object2' in the onCollision function function onCollision( event ) if ( event.phase == "began" ) then print( "collision began with : " .. event.object1.myname .. " and " .. event.object2.myname ) if event.object1.myname ~= 'floor' and event.object2.myname ~= 'floor' then -- collision code end end end Runtime:addEventListener( "collision", onCollision )

Hey again,

Thank you very much for helping me out I just have one problem heres my code:

ground = display.newImage("newground.png") ground.x = 500 ground.y = 540 ground.name = "louie" physics.addBody( ground, "static", {density=1.0, friction=1.0, bounce=0.0, } ) screenGroup:insert(ground) skater = display.newImage("skater.png") skater.x = 230 skater.y = 540 skater.xScale = 0.3 skater.yScale = 0.3 physics.addBody( skater, { friction=1.0, density=1.0, bounce=0.15, radius=10} ) skater:applyForce( 0, -100, skater.x, skater.y ) skater.name = "james" skater.isFixedRotation = true screenGroup:insert(skater) function onCollision(event)     if event.skater.name == "james" and event.ground.name == "louie" then     print("Hit the ground.") else     introOptions = {     effect = "crossFade",     time = 2000     }     storyboard.gotoScene("intro", introOptions)             end         end  

Tweaked your code a little bit so that it would work for me as I may have more objects for him to jump over in the future.

However it’s giving me an error:

“attempt to index field ‘skater’ (a nil value)”

but skater is quite clearly there and it’s definitely not a nil value?

any help is welcome thank you for the help so far aswell.

James

fixed the problem you were completely right thanks!

James