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)