Nil value when removing listener

I’m hoping someone can help me with a couple of questions I have.

  1. I have an image that I put a listener on like this:
local ball = display.newImage("images/ball.png")  
ball:addEventListener( "touch" , removeMe )  

I later remove the ball object in a listener, then I remove that listener

ball:removeSelf()  
.....  
wood:removeEventListener( "touch" , removeMe )  

When I try to remove that listener after the object was removed I get an error

“attempt to call method ‘removeSelf’ (a nil value)”

How do I remove the listener? Just to note I am using the director class.

  1. I am getting the same error:

attempt to call method ‘removeSelf’ (a nil value)

in the following function on the line that is removing the item “chesttop”. I can’t figure out why because it works just as I intend in the simulator.

local closechest = function()  
 chesttop:removeSelf() --error on this line  
 basketball:removeSelf()  
 audio.play( chestSound )  
 physics.pause()  
 if type(SQL:loadVar("highestfinished")) \>= levelNum then  
 SQL:saveVar( "highestfinished", 1 )  
 end  
end  

Hopefully someone can shed some light on this for me? I appreciate everyone’s help as always.
[import]uid: 31262 topic_id: 8366 reply_id: 308366[/import]

I always remove my listeners first then the object, try that. [import]uid: 11809 topic_id: 8366 reply_id: 29923[/import]

you’re only trying to remove in one touch phase arent you? (eg “began”) [import]uid: 6645 topic_id: 8366 reply_id: 29926[/import]

@iDev-Studios:

Sorry I mis-typed my code above and I forgot to mention, I am removing the ball object within the same listener I am trying to remove later.

local function removeMe ( event )  
 if event.phase == "ended" then  
 ball:removeSelf()  
 end  
end  

If I remove the listener first, the ball will never get removed.
@Jmp
I am removing the listeners within a function that is called, these functions are called within the “began” phase. Is this incorrect?

I appreciate your help guys, always helpful as always not only to me but to everyone. [import]uid: 31262 topic_id: 8366 reply_id: 29983[/import]

Anyone know these answers? [import]uid: 31262 topic_id: 8366 reply_id: 30136[/import]

are there any other collisions/transitions that would also remove it?

also if you’re removing the ball touched, why not use [lua]event.target:removeSelf()[/lua] rather than specificially referencing the ball object [import]uid: 6645 topic_id: 8366 reply_id: 30148[/import]

Thanks for replying jmp

There is nothing else that would remove it. This function is only called once during the entire level (when you finish it). I didn’t use event.target because, well honestly I didn’t know you could do that. Still learning :slight_smile: I’ll give it a try and see if it makes a difference. [import]uid: 31262 topic_id: 8366 reply_id: 30150[/import]

post your code.

try [lua]print(ball)[/lua] in your removeMe function. is it nil? try [lua]print(event.target)[/lua]. is that nil? have you structured your code in a way that “ball” can actually be seen by the function? [import]uid: 6645 topic_id: 8366 reply_id: 30156[/import]

I think I know why I’m getting the error in the removeMe function. I just ordered the listeners wrong. Now I’m back to the other issue that has got me stumped.

local closechest = function()  
 print("chesttop")  
 print(chesttop)  
 chesttop:removeSelf() --Error on this line  
 audio.play( chestSound )  
 physics.pause()  
 if (SQL:loadVar("highestfinished")) \>= tostring(levelNum) then  
 SQL:saveVar( "highestfinished", 1 )  
 end  
end  

In this code, chesttop is never called until this function. chesttop contains an image which has already been delcared. I get the following error:

chesttop
table: 0x6d9340
chesttop
table: 0x6d9340
Runtime error
/Users/aaron/Documents/Game Project/level1.lua:60: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
/Users/aaron/Documents/Game Project/level1.lua:60: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>
chesttop
table: 0x6d9340
Runtime error
/Users/aaron/Documents/Game Project/level1.lua:60: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
/Users/aaron/Documents/Game Project/level1.lua:60: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>

The closechest function is called within the following listener:

local function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
 timer.performWithDelay( 700, closechest, 1 )  
 timer.performWithDelay( 1400, endlevel, 1 )  
 end  
end  

Any ideas? Appreciated as always. [import]uid: 31262 topic_id: 8366 reply_id: 30264[/import]

if you’re getting a table address for your object, but it reports “removeSelf” as a nil function, this means your display object has been removed from the table and basically the whole object ready for removal (most likely a previous call to removeSelf). I’m not sure why it’s getting called twice but try setting [lua]chesttop=nil[/lua] after your [lua]:removeSelf[/lua]

Basically display objects are a table with a proxy to the graphic and physics body… when you call removeSelf, the table isn’t removed immediately, hence why you get a table address but no methods. it’s basically an empty table… {}.

maybe you have some other listeners on it that need removing, or most likely you’re getting 2 collisions…

  1. chesttop + other item
  2. other item + chesttop

try printing out your collisions. it’s best to give each item a “name” or a “type” eg [lua]chesttop.me = “chesttop”[/lua] then you can print out [lua]event.other.me[/lua], [lua]event.object1.me[/lua] etc in your collisions to see what’s colliding with what

you’ll most likely be setting your timer to run twice (put a print statement in again to check this), one from each side of the collision.

My guess is that’s what’s calling it to fail as you’re running 2 timers and therefore trying to remove it twice

j
[import]uid: 6645 topic_id: 8366 reply_id: 30278[/import]

Actually chesttop doesn’t have any collision listeners or even physics associated with it. It basically just displays an image then later removes it at the end of the level. I do have other listeners which I haven’t removed yet. Still trying to figure out where to do it properly (opened a separate thread in director forum). I’ll set chesttop to nil as well and give that a try. [import]uid: 31262 topic_id: 8366 reply_id: 30310[/import]