Runtime Listeners semms not removed

Hi Guys/Girls i hav a little (maybe huge?) problem with runtime listeners.

During the programmation i found this function to remove runtime listeners:

function removeAllListeners(obj) obj.\_functionListeners = nil obj.\_tableListeners = nil end

And it worked great until i discover that if i call it, the facebook.login() function stop to work.

So, i proceed with a manual removal of all my runtime listeners like this:

local function removeAllListeners() Runtime:removeEventListener("touch",dragpaniere) Runtime:removeEventListener("system", onSystemEvent) Runtime:removeEventListener( "collision", onCollision) Runtime:removeEventListener( "tap", pause) end

Now some of the EventListener like the collision one, seems to remain active, because when i add again the listeners with the code below, i had an error during collision.

Runtime:addEventListener("touch",dragpaniere) Runtime:addEventListener( "collision", onCollision) Runtime:addEventListener( "tap", pause) Runtime:addEventListener( "system", onSystemEvent)

Printing the object of the collision ( print(myObject)) during the collision function i noticed that i have double output like if there are 2 active listeners in my code.

I am sorry if my english is not good but i am Italian. I hope my problem is understandable anyway.

Thanks a lot!

Andrea.

Hello Andrea,

Can you show me your “onCollision” code, and also an example of your physics objects that are colliding (the code where you use addBody() to create the physics body). This should help me narrow down the issue.

Thanks,

Brent Sorrentino

Hi Brent, thanks for your answer. here it is, it’s a little bit messy…

here is the pastebin link: http://pastebin.com/xAU5YdQ0

 local function onCollision(event) local obj1 = event.object1 local obj2 = event.object2 if obj1.myName=="tomato" then obj2 = event.object1 obj1 = event.object2 end if event.phase == "began" then if obj2.id == "life" and obj1.myName == "paniere" then audio.play(myAudio["lifeup"]) --display.remove(obj2) local function removeObj2() local function remove() display.remove(localstar) localstar = nil life=life+1 lifedisplay.xScale = 0.4; lifedisplay.yScale = 0.4 myTransitions["transitionLife"] = transition.to(lifedisplay, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic }) lifedisplay.text=("x"..tostring(life)) end myTimers["removetimer"]=timer.performWithDelay(350,remove) localstar=display.newImageRect("tomato1up.png", 75, 75 ) localstar.x=paniere.x localstar.y=paniere.y localstar.angularVelocity=1900 myTransitions["transitionLife2"] = transition.to( localstar, { time=300,alpha=0.1, x=928, y=43}) end removeObj2() elseif obj2.id == "normale" and obj1.myName == "ground" then local function removeObj() --display.remove(obj2) --table.remove(allTomato,obj2.numero) if life\>0 then life=life-1 lifedisplay.text=("x"..tostring(life)) else lifedisplay.text=("x0") end deadbodies[#deadbodies+1]=display.newImageRect("smashed.png",60,20) local deadbody=deadbodies[#deadbodies] deadbodies[#deadbodies].x=obj2.x deadbodies[#deadbodies].y=700+bonusgiallo group:insert(deadbody) audio.play(myAudio["splash"]) local ghost=display.newImageRect("ghost.png",80,80) ghost.x=deadbodies[#deadbodies].x ghost.y=deadbodies[#deadbodies].y ghost.alpha=0.5 local function removeGhost() display.remove(ghost) ghost = nil --print ("ghost rimosso") end myTransitions["transitionGhost"] = transition.to( ghost, { time=2000,alpha=0.3, y=-100, onComplete=removeGhost}) if life==0 then gameOver() end end removeObj() elseif obj1.myName == "ground" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) elseif obj2.id == "normale" and obj1.myName == "paniere" then playPopCestino = audio.play(myAudio["popcestino"]) local function removeObj1() --display.remove(obj2) punti=punti+1+x2 puntiEffettivi=puntiEffettivi+1 punteggio.text=tostring(punti) if puntiEffettivi%math.round(9000\*livellone/level)==0 and level\>210 then level=level-30 livellone=livellone+1 print (" i millisecondi sono"..level.." a pomodori:"..puntiEffettivi) myTimers["droptomatoes"].\_delay=level end --table.remove(allTomato,obj2.numero) end removeObj1() elseif obj2.id=="special1" and obj1.myName=="paniere" then --display.remove(obj2) audio.play(myAudio["popcestino"]) cambiosfondo() bonus="gravity" --table.remove(allSpecialTomato,obj2.numero) setBonus(bonus) elseif obj2.id=="special2" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) bonus="enlarge" setBonus(bonus) elseif obj2.id=="special3" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) bonus="x2" setBonus(bonus) elseif obj2.id=="legendary1" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=1 achievementunlocked(lnum) elseif obj2.id=="legendary2" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=2 achievementunlocked(lnum) elseif obj2.id=="legendary3" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=3 achievementunlocked(lnum) elseif obj2.id=="legendary4" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=4 achievementunlocked(lnum) elseif obj2.id=="legendary5" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=5 achievementunlocked(lnum) elseif obj2.id=="legendary6" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=6 achievementunlocked(lnum) end display.remove(obj2) end return true end

Hi Andrea,

Are these physics bodies composed of two or more “parts”? Or, just one basic body shape like a circle?

their are simple circles

Hello @ilparola,

This might be an issue of scoping, where you’re trying to remove the collision listener, but the function “onCollision” is below where you try to remove it (and thus, it doesn’t know what that reference is).

Can you show where your “*:removeEventListener” routine is, compared to your “onCollision” function?

Thanks,

Brent

Hi Brent, i have a declaration of onCollision in the declaration section of my program like local onCollision, then i have the removeAllListeners function and then the onCollision function.

Thanks Brent, i switch the 2 functions and now all is working fine. Thanks a lot.

Btw, the declaration is not enough?

Andrea.

Hi Andrea,

Can you show me (in code) what you describe with “i have a declaration of onCollision in the declaration section of my program like local onCollision, then i have the removeAllListeners function and then the onCollision function.”? I’d like to confirm by seeing your code.

Brent

i’ve modified it now but there was something like:

local onCollision local --other declarations -- -- -- local function removeAllListeners() .. .. .. .. end --Some code -- -- -- function onCollision() .. .. .. .. .. .. end

if you want the old code, i think i have an old version in my office but we have to wait monday!

Hello Andrea,

Can you show me your “onCollision” code, and also an example of your physics objects that are colliding (the code where you use addBody() to create the physics body). This should help me narrow down the issue.

Thanks,

Brent Sorrentino

Hi Brent, thanks for your answer. here it is, it’s a little bit messy…

here is the pastebin link: http://pastebin.com/xAU5YdQ0

 local function onCollision(event) local obj1 = event.object1 local obj2 = event.object2 if obj1.myName=="tomato" then obj2 = event.object1 obj1 = event.object2 end if event.phase == "began" then if obj2.id == "life" and obj1.myName == "paniere" then audio.play(myAudio["lifeup"]) --display.remove(obj2) local function removeObj2() local function remove() display.remove(localstar) localstar = nil life=life+1 lifedisplay.xScale = 0.4; lifedisplay.yScale = 0.4 myTransitions["transitionLife"] = transition.to(lifedisplay, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic }) lifedisplay.text=("x"..tostring(life)) end myTimers["removetimer"]=timer.performWithDelay(350,remove) localstar=display.newImageRect("tomato1up.png", 75, 75 ) localstar.x=paniere.x localstar.y=paniere.y localstar.angularVelocity=1900 myTransitions["transitionLife2"] = transition.to( localstar, { time=300,alpha=0.1, x=928, y=43}) end removeObj2() elseif obj2.id == "normale" and obj1.myName == "ground" then local function removeObj() --display.remove(obj2) --table.remove(allTomato,obj2.numero) if life\>0 then life=life-1 lifedisplay.text=("x"..tostring(life)) else lifedisplay.text=("x0") end deadbodies[#deadbodies+1]=display.newImageRect("smashed.png",60,20) local deadbody=deadbodies[#deadbodies] deadbodies[#deadbodies].x=obj2.x deadbodies[#deadbodies].y=700+bonusgiallo group:insert(deadbody) audio.play(myAudio["splash"]) local ghost=display.newImageRect("ghost.png",80,80) ghost.x=deadbodies[#deadbodies].x ghost.y=deadbodies[#deadbodies].y ghost.alpha=0.5 local function removeGhost() display.remove(ghost) ghost = nil --print ("ghost rimosso") end myTransitions["transitionGhost"] = transition.to( ghost, { time=2000,alpha=0.3, y=-100, onComplete=removeGhost}) if life==0 then gameOver() end end removeObj() elseif obj1.myName == "ground" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) elseif obj2.id == "normale" and obj1.myName == "paniere" then playPopCestino = audio.play(myAudio["popcestino"]) local function removeObj1() --display.remove(obj2) punti=punti+1+x2 puntiEffettivi=puntiEffettivi+1 punteggio.text=tostring(punti) if puntiEffettivi%math.round(9000\*livellone/level)==0 and level\>210 then level=level-30 livellone=livellone+1 print (" i millisecondi sono"..level.." a pomodori:"..puntiEffettivi) myTimers["droptomatoes"].\_delay=level end --table.remove(allTomato,obj2.numero) end removeObj1() elseif obj2.id=="special1" and obj1.myName=="paniere" then --display.remove(obj2) audio.play(myAudio["popcestino"]) cambiosfondo() bonus="gravity" --table.remove(allSpecialTomato,obj2.numero) setBonus(bonus) elseif obj2.id=="special2" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) bonus="enlarge" setBonus(bonus) elseif obj2.id=="special3" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) bonus="x2" setBonus(bonus) elseif obj2.id=="legendary1" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=1 achievementunlocked(lnum) elseif obj2.id=="legendary2" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=2 achievementunlocked(lnum) elseif obj2.id=="legendary3" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=3 achievementunlocked(lnum) elseif obj2.id=="legendary4" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=4 achievementunlocked(lnum) elseif obj2.id=="legendary5" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=5 achievementunlocked(lnum) elseif obj2.id=="legendary6" and obj1.myName=="paniere" then --display.remove(obj2) --table.remove(allSpecialTomato,obj2.numero) audio.play(myAudio["popcestino"]) lnum=6 achievementunlocked(lnum) end display.remove(obj2) end return true end

Hi Andrea,

Are these physics bodies composed of two or more “parts”? Or, just one basic body shape like a circle?

their are simple circles

Hello @ilparola,

This might be an issue of scoping, where you’re trying to remove the collision listener, but the function “onCollision” is below where you try to remove it (and thus, it doesn’t know what that reference is).

Can you show where your “*:removeEventListener” routine is, compared to your “onCollision” function?

Thanks,

Brent

Hi Brent, i have a declaration of onCollision in the declaration section of my program like local onCollision, then i have the removeAllListeners function and then the onCollision function.

Thanks Brent, i switch the 2 functions and now all is working fine. Thanks a lot.

Btw, the declaration is not enough?

Andrea.

Hi Andrea,

Can you show me (in code) what you describe with “i have a declaration of onCollision in the declaration section of my program like local onCollision, then i have the removeAllListeners function and then the onCollision function.”? I’d like to confirm by seeing your code.

Brent

i’ve modified it now but there was something like:

local onCollision local --other declarations -- -- -- local function removeAllListeners() .. .. .. .. end --Some code -- -- -- function onCollision() .. .. .. .. .. .. end

if you want the old code, i think i have an old version in my office but we have to wait monday!