Director Class & removeSelf()

So, being new to Corona and all, I’ve decided to learn Corona by means of creating a game, but I stumbled upon a weird problem when it comes to the director class and the removeSelf() function.

A bit of background: The game should go that there’s a continuous spawning of an object, and when it collides with something it removes itself. So I’ve set it up. It works perfectly fine. Problem is, whenever I use the director class and switch scenes, whenever I come back to the game scene, the collision is having some errors. It appears as if it’s detecting each of the new objects as two objects, so it tries to removeSelf() twice (or it detects that it collided twice, that same object). :expressionless: Of course, the second attempt fails, thus causing the error I see below:

leftWall&objectcollided
removing object
done removing object
leftWall&objectcollided
removing object
Runtime error
 \*\*\*.lua:148: attempt to call method 'removeSelf' (a nil value)
stack traceback:
 [C]: in function 'removeSelf'
 \*\*\*:148: in function 
 ?: in function <?:214>

I think that’s how it’s going though. >.<. i still need to get used understanding these error messages but hope some of you will be able help me. :="">
If it helps, my code looks something like this:

[lua]print(e.object1.name … “&” … e.object2.name … “collided”)
if(e.object1.name == “leftWall”) then
print("removing " … e.object2.name)
e.object2:removeSelf(); print(“done removing object”);
end[/lua]

This only happens if I switch scenes using the director class (and by that, I mean I’ve loaded this scene at least once, then I move to a different scene then come back to this [the game’s] scene). I have a code that resets the game without switching scenes and it works fine.

[import]uid: 45459 topic_id: 8033 reply_id: 308033[/import] </.>

I had this crop up on me a couple of days ago.

First thing to check is if you have any references to the objects still hanging around. Make sure everything is nil after you use it.

In my case, I was storing a reference to the spawned object in a variable called “me”, so I could pass it to another function, from a function.

After I passed the variable on, I forgot to nil it, that was causing my errors.

Go through your code, and make sure that all references to the object, are nilled after you are done with them. [import]uid: 5317 topic_id: 8033 reply_id: 28631[/import]

One way to fix this is like so : (added to your sample)

print(e.object1.name .. "&" .. e.object2.name .. "collided") if(e.object1.name == "leftWall") then print("removing " .. e.object2.name) if e.object2.removeSelf then e.object2:removeSelf(); print("done removing object"); end end [import]uid: 6981 topic_id: 8033 reply_id: 28632[/import]

hahaha

thanks for that quick fix! :smiley: it actually worked… sorta :))

while I haven’t found a way for me to avoid the detection to happen twice, this quick solution should do… [import]uid: 45459 topic_id: 8033 reply_id: 28682[/import]