One object with 2 event listeners

Hi, 

I have a object which spawns random interval. When the object was tapped is should disappear, and when it collides to something, it will disappear too. I actually tried putting 2 event listener on the function that spawns the object.

like this

object.tap = objectOnTap object:addEventListener( "tap" ) physics.addBody( object, "dynamic",{ bounce = 0 ,friction = 0.5 ,filter = M.objectFilter}) object.collision = objectOnCollision object:addEventListener( "collision")

but the collision handler doesn’t work at all.

this keeps me bugging. since I got it to work last time. But this time it doesn’t

I put a print statement on the collision handler to make sure that it does read the code. but it doesn’t.

Thanks in advance,

Jam

Its going to be hard to answer you based on just that code fragment.  What is your M.objectFilter?  What objects are you expecting it to collide with?  How are they being defined?  What filters are being added to them?

Thanks

Rob

I’m away from the computer and can’t test, but I think you’re missing the last parts of your table listeners.

It should be:
object:addEventListener( “tap”, object)
And
object:addEventListener( “collision”, object)

I finally got it to work. but there are still one problem. 

local objectTable = {} local object2Table = {} --to make sure that the two objects wont hit each other and only the bottom can be hit bottomCollisionFilter = {categoryBits=1, maskBits=6} objectCollisionFilter = {categoryBits=2, maskBits=1} object2CollisionFilter = {categoryBits=4, maskBits=1} local function objectOnTap(self,event) objectTable[self] = nil display.remove(self) return true end local function object2OnTap(self,event) if (event.numTaps == 2) then object2Table[self] = nil display.remove(self) return true else return false end end local function objectOnCollision(self,event) objectTable[self] = nil display.remove(self) return true end local function object2OnCollision(self,event) object2Table[self] = nil display.remove(self) end local function onCollision(event) print("Im Hit") end local function spawnObject() local objectTmp = dislay.newRect.... objectTmp.y = (math.random(1,50) \* -1) objectTmp.x = math.random(1,display.contentWidth) objectTmp.tap = objectOnTap objectTmp:addEventListener( "tap" ) objectTmp.collision = objectOnCollision objectTmp:addEventListener( "collision") physics.addBody( objectTmp,{ bounce = 0 ,friction = 0.5 ,filter = objectCollisionFilter}) objectTmp.linearDamping = 1.5 --how fast they go down end --the code of object2 is pretty much the same with spawnobject local bottom = display.newRect..... bottom.width = display.contentWidth bottom.height = 15 physics.addBody( bottom, "static", { friction = 0.5, bounce = 0 ,filter = bottomCollisionFilter} ) bottom:addEventListener( "collision", onCollision ) objectTimer = timer.performWithDelay( 1000, spawnObject, -1) object2Timer = timer.performWithDelay( 1000, spawnObject2, -1 )

if i comment out the bottom.addEventListener, the object’s collision handler works perfectly. But I want to know that if the objects touched bottom or not, and if they touched it. they will simply disappear.

But when I put the bottom.AddEventListener(delete comment) the objects stays above the bottom as if that their collision handler doesn’t work. But I can see in the console that “Im hit” which means the bottom handler is hit by the objects.

P.S I just typed this code. if there are some typepo error it doesnt hurt that much, cuz I typed it

I got it to work guys. Thanks. A guy from other forums suggested that I put a isSensor above the bottom to that it will report that it hits the bottom and remove the collision event listener on the bottom so that when the objects collide with it, their collision listeners will work. We dont now why the bottom collision listener cancels the objects collision listener

Its going to be hard to answer you based on just that code fragment.  What is your M.objectFilter?  What objects are you expecting it to collide with?  How are they being defined?  What filters are being added to them?

Thanks

Rob

I’m away from the computer and can’t test, but I think you’re missing the last parts of your table listeners.

It should be:
object:addEventListener( “tap”, object)
And
object:addEventListener( “collision”, object)

I finally got it to work. but there are still one problem. 

local objectTable = {} local object2Table = {} --to make sure that the two objects wont hit each other and only the bottom can be hit bottomCollisionFilter = {categoryBits=1, maskBits=6} objectCollisionFilter = {categoryBits=2, maskBits=1} object2CollisionFilter = {categoryBits=4, maskBits=1} local function objectOnTap(self,event) objectTable[self] = nil display.remove(self) return true end local function object2OnTap(self,event) if (event.numTaps == 2) then object2Table[self] = nil display.remove(self) return true else return false end end local function objectOnCollision(self,event) objectTable[self] = nil display.remove(self) return true end local function object2OnCollision(self,event) object2Table[self] = nil display.remove(self) end local function onCollision(event) print("Im Hit") end local function spawnObject() local objectTmp = dislay.newRect.... objectTmp.y = (math.random(1,50) \* -1) objectTmp.x = math.random(1,display.contentWidth) objectTmp.tap = objectOnTap objectTmp:addEventListener( "tap" ) objectTmp.collision = objectOnCollision objectTmp:addEventListener( "collision") physics.addBody( objectTmp,{ bounce = 0 ,friction = 0.5 ,filter = objectCollisionFilter}) objectTmp.linearDamping = 1.5 --how fast they go down end --the code of object2 is pretty much the same with spawnobject local bottom = display.newRect..... bottom.width = display.contentWidth bottom.height = 15 physics.addBody( bottom, "static", { friction = 0.5, bounce = 0 ,filter = bottomCollisionFilter} ) bottom:addEventListener( "collision", onCollision ) objectTimer = timer.performWithDelay( 1000, spawnObject, -1) object2Timer = timer.performWithDelay( 1000, spawnObject2, -1 )

if i comment out the bottom.addEventListener, the object’s collision handler works perfectly. But I want to know that if the objects touched bottom or not, and if they touched it. they will simply disappear.

But when I put the bottom.AddEventListener(delete comment) the objects stays above the bottom as if that their collision handler doesn’t work. But I can see in the console that “Im hit” which means the bottom handler is hit by the objects.

P.S I just typed this code. if there are some typepo error it doesnt hurt that much, cuz I typed it

I got it to work guys. Thanks. A guy from other forums suggested that I put a isSensor above the bottom to that it will report that it hits the bottom and remove the collision event listener on the bottom so that when the objects collide with it, their collision listeners will work. We dont now why the bottom collision listener cancels the objects collision listener