Collision listener attemt to index object2 error

Hi,
I am using a physics body with and storyboard.
The first time the scene is displayed, the physics litener for collisions works fine. But after destroying and creating again the scene i displays an error
.lua:66: attempt to index field ‘object2’ (a nil value)

I have made many attempts but i have no idea about what is causing this error. the scene is removed completely by removeScene and all the listeners transitions are cancelled.

I put my code.
THANKS FOR ANY SUGGESTION

local function novaPilota(x)  
 local radi=34\*0.417  
 local tipusPilota=math.random(1,6)  
 suma=math.random(-1,1)  
 local pilota=display.newImageRect("ARCADE PILOTA "..tipusPilota..".png", 28, 28)  
--pilotes belongs to the scene group  
 pilotes:insert(pilota)  
 pilota.name="pilota"  
 pilota.x, pilota.y = (x-radi+suma)\*0.417,320-672\*0.417  
--HERE ADD THE BODY  
 physics.addBody(pilota,{ friction=0.3, bounce=0.2, radius=radi })  
 pilota.inremove=false  
 function pilota:remove()  
 if (not self.inremove) then  
 self.inremove=true  
 table.insert(transitionList,  
 transition.to( self, { time=1000, transition=easing.inExpo, delay=1500, alpha=0,  
 onComplete=function()  
 self:removeSelf() end }   
 )  
 )  
 end  
 end  
end  
  
function onCollision( event )  
 if ( event.phase == "began" ) then  
 local name1=event.object1.name  
--crashes when trying to access object2  
 local name2=event.object2.name  
 print(name1.." "..name2)  
 if ( event.object1.name=="marc" ) then  
 event.object2:remove()  
 elseif ( event.object2.name=="marc" ) then  
 event.object1:remove()  
 elseif ( event.object1.name=="cistell" ) then  
 event.object2:removeSelf()  
 event.object2=nil  
 animacioMarcador:actualitzar()  
 elseif ( event.object2.name=="cistell" ) then  
 event.object1:removeSelf()  
 event.object1=nil  
 animacioMarcador:actualitzar()  
 end  
 end  
end  
  
--everything is destroyed  
function scene:exitScene( event )  
 local group = self.view  
  
 -----------------------------------------------------------------------------  
  
 Runtime:removeEventListener( "retornjoc", self )  
 timer.cancel(timer1)  
 timer.cancel(timer2)  
 timer.cancel(timer3)  
 spriteFinal:dispose()  
 CIL:destruir()  
 for i,t in ipairs(transitionList) do  
 print (i)  
 transition.cancel(t)  
 end  
  
 -----------------------------------------------------------------------------  
  
end  

thanks!
[import]uid: 76413 topic_id: 20244 reply_id: 320244[/import]

It sounds as though something isn’t getting cancelled or cleaned properly, although without plug and play code I can’t test this.

We do offer a debugging service that could assist you in combing through your code and fixing it; http://www.anscamobile.com/corona/support/

Beyond that I would suggest you try to further isolate the problem, that’s a fair amount of code above. [import]uid: 52491 topic_id: 20244 reply_id: 79097[/import]

Sorry, I am just trying to help.

Look this part of your code:

[lua]–crashes when trying to access object2
local name2=event.object2.name
print(name1…" "…name2)
if ( event.object1.name==“marc” ) then
event.object2:remove() < elseif ( event.object2.name==“marc” ) then
event.object1:remove() < elseif ( event.object1.name==“cistell” ) then
event.object2:removeSelf()
event.object2=nil
animacioMarcador:actualitzar()
elseif ( event.object2.name==“cistell” ) then
event.object1:removeSelf()
event.object1=nil
animacioMarcador:actualitzar()
end
end
end[/lua]

I ask because Ive never seen any remove() called like that and so it would get Nil value (without any argument - PS: Im not speaking about removeSelf() ). ?! Am I much crazy? :\

Wish Luck!
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 20244 reply_id: 79112[/import]

object2:remove is a method of pilota as shown here. I think is correct

pilota.inremove=false  
 function pilota:remove()  
 if (not self.inremove) then  
 self.inremove=true  
 table.insert(transitionList,  
 transition.to( self, { time=1000, transition=easing.inExpo, delay=1500, alpha=0,  
 onComplete=function()  
 self:removeSelf() end }   
 )  
 )  
 end  
 end  

I you made me notice that i don need to set nil in here

--crashes when trying to access object2  
 local name2=event.object2.name  
 print(name1.." "..name2)  
 if ( event.object1.name=="marc" ) then  
 event.object2:remove() \< elseif ( event.object2.name=="marc" ) then  
 event.object1:remove() \< elseif ( event.object1.name=="cistell" ) then  
 event.object2:removeSelf()  
 event.object2=nil animacioMarcador:actualitzar()  
 elseif ( event.object2.name=="cistell" ) then  
 event.object1:removeSelf()  
 event.object1=nil animacioMarcador:actualitzar()  
 end  
 end  
end  

now the error is different:
Runtime error
/Users/ernest/Desktop/IMATGES APLIKS/MENU/ARCADE.lua:73: ERROR: Attempt to remove an object that’s already been removed from the stage or whose parent/ancestor group has already been removed.
stack traceback:
[C]: ?
[C]: in function ‘removeSelf’
/Users/ernest/Desktop/IMATGES APLIKS/MENU/ARCADE.lua:73: in function
?: in function <?:215>

But still no idea!!

Thanks for your help Rodrigo

Ernest. [import]uid: 76413 topic_id: 20244 reply_id: 79143[/import]

You`re welcome Ernest.

But sorry…I am unable to help you in this one. :\

Surely someone else will get in here and work it with you.
Sorry,
Rodrigo. [import]uid: 89165 topic_id: 20244 reply_id: 79149[/import]

To get rid of that error, use :

display.remove(whatever)

rather than removeSelf

:slight_smile: [import]uid: 84637 topic_id: 20244 reply_id: 79169[/import]

Oh it doesn’t work yet.
I think i’ll have to find the way to debug this.

by the way, which is the difference between removeSelf() and display.remove()?

Thanks! [import]uid: 76413 topic_id: 20244 reply_id: 79286[/import]

Hi,
There is the same problem when I use Director class.
First time (game.lua in my case), collision event works fine,
Then I move another scene, and I back to the game.lua again. Collision detection fails: “attempt to index field ‘object2’ (a nil value)”
I don’t know how to solve it now. [import]uid: 96013 topic_id: 20244 reply_id: 99020[/import]

I managed to fix the problem.

-- (before) This code doesn't work fine when the scene is re-visited. (error: object2 is nil)   
local function onCollision( event )  
 if ( event.phase == "began" ) then  
 if(event.object2.name == "enemy" and event.object1.name == "myChare")then  
 print("hit the enemy")  
 end  
 end  
end  
Runtime:addEventListener( "collision", onCollision )  
-- (after) This code works fine.  
local onMyCollision = function( self, event )  
 if event.phase == "began" then  
 if(self.name == "myChara" and event.other.name == "enemy")then  
 print("hit the enemy")  
 end  
 end  
end  
me.collision = onMyCollision  
me:addEventListener( "collision", me )  

NOTE: Both codes inside of function are simplified for illustrative purposes.

I saw Jonathan Beebe’s code(http://jonbeebe.tumblr.com/post/2160877073/angry-birds-clone-with-corona-in-36-hours) which works its collision perfectly in Director class. Even if I move and re-visit the another physics scene. He used this method. (me:addEventListener( “collision”, me )) [import]uid: 96013 topic_id: 20244 reply_id: 99127[/import]