How To Restart Game From Main.lua Again

hi, i have a game that show restart button when it’s game over but i dont know how to set up a “restart” function properly. Basically in my game main.lua redirect automatically to menu.lua then if you click “play” button there you will be redirected to game.lua. Here in game.lua i have a restart button with code:

local function restart(event) if event.phase == "ended" then onSystemEvent () Runtime:removeEventListener("touch", stop ) Runtime:removeEventListener("enterFrame", wrapit) Runtime:removeEventListener("enterFrame", bucketfollow) Runtime:removeEventListener("enterFrame", bananacheck) Runtime:removeEventListener("enterFrame", testCollisions) Runtime:removeEventListener("system", onSystemEvent ) audio.stop (gameMusicChannel) director:changeScene ("menu") end end restartBtn:addEventListener('touch', restart)

so basically i add “removeEventListener” one by one before changeScene back to menu.lua. But i believe this is not the best solution because i have to add new “Runtime:removeEventListener” every time i add a new code for different features. How to set it up properly without all these hassles? [import]uid: 114765 topic_id: 23143 reply_id: 323143[/import]

What you are doing, moving to menu, is the logical thing as main goes straight to menu again anyway.

With Runtime removal, you need to remove Runtime listeners as you are, just don’t add a million of them and it wont be too hard to manage. Is just one line per Runtime :slight_smile: [import]uid: 52491 topic_id: 23143 reply_id: 92713[/import]

hey there, i have another question regarding the restart function.

as you can see above, i use:

director:changeScene ("menu")

i want to restart by going to the same scene, which is game.lua, how do I do that? The above code will redirect me to different scene which is menu.lua but i realize it’d be better if the restart button is to actually restart game.lua… [import]uid: 114765 topic_id: 23143 reply_id: 93285[/import]

Some newer versions of director I believe have this function, so you’d ask about that in the director sub forum.

You could also just make another file with one function to go straight back to game and then direct to that as your restart. [import]uid: 52491 topic_id: 23143 reply_id: 93452[/import]

hi there, thanks so much for replying again :wink:

but where’s director sub forum? I couldnt find it…

also i tried to make restart.lua with this code:

[code]display.setStatusBar (display.HiddenStatusBar)

local function main()

director:changeScene(“game”)
–> Change the scene, no effects

end

timer.performWithDelay(0, main, 1)
–> Starts our app

[/code]

but it returned with blank screen after i click the restart button - which has director:changeScene (“restart”) inside the code

strangely, it works if I redirect to “restart” from main.lua but it doesnt work if i redirect to “restart” from game.lua.

so if lets say the whole redirection is like this, it does work:
main -> restart -> game

but if the redirection is like this, it doesnt work:
main -> game -> restart -> game [import]uid: 114765 topic_id: 23143 reply_id: 93673[/import]

Here is link to sub forum: http://developer.anscamobile.com/forums/director-class

That said, you set your delay as 0, possibly try setting it as 100, see if that helps. (Am unsure with director.) [import]uid: 52491 topic_id: 23143 reply_id: 93727[/import]

hey thanks for the link!

as for changing the delay, i have actually edited the number before and it never worked. Thanks btw! [import]uid: 114765 topic_id: 23143 reply_id: 93743[/import]