Collision and Touch Detection Issue

Hi, i have a problem with my code. i want to disable touching for an object when the object collide with another object. but when i execute the code, it reports a lot of collision and it idoesn’t response any more. :wacko:  for any other button or touching event. Thanks. :slight_smile:

Code is here:

 object1 = display.newImage("tire2.png", centerX, 500)                                          object1.myName = "object1"              object1.bodyType = "dynamic"              object1.isBullet = true              physics.addBody( object1, { density=0.9, friction=0.5, bounce=0.6, radius=60 } )             object1.isSleepingAllowed = false                           object1:addEventListener( "touch", dragBody )     object2 = display.newImage("tire3.png", centerX-centerX/2-180, 500)                              object2.myName = "object2"          object2.bodyType = "dynamic"          object2.isBullet = true          physics.addBody( object2, { density=0.9, friction=0.5, bounce=0.6, radius=60 } )         object2.isSleepingAllowed = false                   object2:addEventListener( "touch", dragBody )               button = display.newImage("button.png", 145,145)                    button.anchorX = 1          button.anchorY = 0          button.x = \_W-52          button.y =72.5          button.myName = "button"               function collision1 (event)                  if ( event.phase == "began" ) then            if (event.object1.myName  == "object1" and  event.object2.myName == "object2" ) then                 print (event.object1.myName .."  and  " .. event.object2.myName )                       object2:removeEventListener( "touch", dragBody )      end        end          end               function buttonfunc(event)               if event.phase == "ended" then          message = display.newText( "Button pressed.", 0, 0, "ContenuBook-Display", 40 )          end       end           Runtime:addEventListener( "collision", collision1)      button:addEventListener("touch", buttonfunc)

Is there anyone for help???

hmm sometimes, due to imperfections in the box 2d library you cant perform complex actions within a collision listener function, use a short timer.performwithdelay with a 32 millisecond or less delay eg.

  local function collision1 (event)        if ( event.phase == "began" ) then              if (event.object1.myName  == "object1" and  event.object2.myName == "object2" ) then                      timer.performWithDelay(32, function(event)             print (event.object1.myName .."  and  " .. event.object2.myName )                          object2:removeEventListener( "touch", dragBody )             end,1)         end      end  end  

Hope this helps, Regards Lemon

Thanks bro. i tried it but i didnt work. Do you have any suggestions?

Hi @johannastephen,

Are you getting any error messages in the console? Have you carefully checked the scope of all variables/functions?

Also, just FYI, you must always set physics properties like “.isBullet” after you create the body. If you attempt this before the object is made physical, Corona will not understand what you’re trying to do (well, Lua will just create that property on the object, but it won’t be regarded as a physics-related property).

Brent

No i dont get any error message in the console. only collision events. when i try to remove event listener with or without timer.performWithDelay , it doesnt response any touch. should i change “isBullet” position?

There’s likely a scope issue, in how/where you’ve declared your objects and functions, so Lua doesn’t understand what you’re trying to do when you remove the listener (it can’t find it in the proper scope, for example). You should focus on making your objects local, not global, and then ensuring that scope is 100% correct across the entire routine. If you’re somewhat new to Lua, then you should watch this video on scope:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Take care,

Brent

i found the solution with this: i create a counter that detects collision events counts. and when event counts become a lot, i stop the function.

Is there anyone for help???

hmm sometimes, due to imperfections in the box 2d library you cant perform complex actions within a collision listener function, use a short timer.performwithdelay with a 32 millisecond or less delay eg.

  local function collision1 (event)        if ( event.phase == "began" ) then              if (event.object1.myName  == "object1" and  event.object2.myName == "object2" ) then                      timer.performWithDelay(32, function(event)             print (event.object1.myName .."  and  " .. event.object2.myName )                          object2:removeEventListener( "touch", dragBody )             end,1)         end      end  end  

Hope this helps, Regards Lemon

Thanks bro. i tried it but i didnt work. Do you have any suggestions?

Hi @johannastephen,

Are you getting any error messages in the console? Have you carefully checked the scope of all variables/functions?

Also, just FYI, you must always set physics properties like “.isBullet” after you create the body. If you attempt this before the object is made physical, Corona will not understand what you’re trying to do (well, Lua will just create that property on the object, but it won’t be regarded as a physics-related property).

Brent

No i dont get any error message in the console. only collision events. when i try to remove event listener with or without timer.performWithDelay , it doesnt response any touch. should i change “isBullet” position?

There’s likely a scope issue, in how/where you’ve declared your objects and functions, so Lua doesn’t understand what you’re trying to do when you remove the listener (it can’t find it in the proper scope, for example). You should focus on making your objects local, not global, and then ensuring that scope is 100% correct across the entire routine. If you’re somewhat new to Lua, then you should watch this video on scope:

http://www.youtube.com/watch?v=2ATlcGP2zMY

Take care,

Brent

i found the solution with this: i create a counter that detects collision events counts. and when event counts become a lot, i stop the function.