First time works. After load scene again: attempt to call method 'toFront' (a nil value)

Hi guys! 

I’m having a weird problem with “object:toFront()”. I’m building a game, and I need to show an “explosion” image I called “crashStar” in the “onCollision” method after a collision is detected by the physics engine. 

I create my crashStar as a global variable and I initialize it in the scene:create as follows:

crashStar = display.newImageRect("images/crash.png", 142, 158) crashStar.isVisible = false sceneGroup:insert( crashStar )

After that I’m showing this star in the “onCollision” as follows:

onCollision = function ( event ) crashStar.x = event.x crashStar.y = event.y crashStar.isVisible = true if crashStar then crashStar:toFront() end end

The first time I play the game the crashStar is shown as it should.  The second time I play the game: (I have to go to menu and load the scene again the “scene:create” is actually called again as it should) I get the “attempt to call method ‘toFront’ (a nil value)” message…

I don’t understand what’s happening because the scene is completely loaded again… it’s removed and loaded again ( composer.removeScene(“scene”) ) and the scene:create is called so It’s the same code but once it works and the second time it doesn’t…

The error shown in the terminal says the line where the “crashStar:toFront()” is called is the one with the error of course…

Any Idea??? it’s driving me crazy… (I’ve already read an other post with the same problem but there was no useful data there)

Have you tried making the crashStar object local? What is the reason for making it global?

Yes, I’ve also tried this:

onCollision = function ( event ) local crashStar = display.newImageRect("images/crash.png", 142, 158) sceneGroup:insert( crashStar ) crashStar.x = event.x crashStar.y = event.y end

And just realized the same behavior is happening here… the first time it works, but the second time it says something: “attempt to call method ‘insert’ (a nil value)”

I’m completely lost… no idea what’s going on…

Thanks!

In the second example, where do you initialize the sceneGroup variable?  It’s sometimes important that all values and objects, unless “constants” (in quotes since technically no constants in lua) are initialized within a function instead.  

local sceneGroup; function scene:createScene(event) sceneGroup = display.newGroup(); end

Similar to the above. Another reason might be that the listener associated with onCollision does not actually get removed after transitioning away from the scene the first time. 

Thank you SO MUCH!!! It was the onCollision event that I forgot to remove!!! I spent hours on this. THANK YOU!!!

Runtime:removeEventListener( "collision", onCollision )

Have you tried making the crashStar object local? What is the reason for making it global?

Yes, I’ve also tried this:

onCollision = function ( event ) local crashStar = display.newImageRect("images/crash.png", 142, 158) sceneGroup:insert( crashStar ) crashStar.x = event.x crashStar.y = event.y end

And just realized the same behavior is happening here… the first time it works, but the second time it says something: “attempt to call method ‘insert’ (a nil value)”

I’m completely lost… no idea what’s going on…

Thanks!

In the second example, where do you initialize the sceneGroup variable?  It’s sometimes important that all values and objects, unless “constants” (in quotes since technically no constants in lua) are initialized within a function instead.  

local sceneGroup; function scene:createScene(event) sceneGroup = display.newGroup(); end

Similar to the above. Another reason might be that the listener associated with onCollision does not actually get removed after transitioning away from the scene the first time. 

Thank you SO MUCH!!! It was the onCollision event that I forgot to remove!!! I spent hours on this. THANK YOU!!!

Runtime:removeEventListener( "collision", onCollision )