How To Restart Game

hi i have asked this question in developer support subforum but apparently nobody know how to do it properly, here’s the link:

http://developer.anscamobile.com/forum/2012/03/11/how-restart-game-mainlua-again

*my problem is not the first post, read my 2nd post. That’s what Im trying to do, here the question again:

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 ("restart") end end restartBtn:addEventListener('touch', restart)
and in restart.lua, i have

[code]display.setStatusBar (display.HiddenStatusBar)

local function main()

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

end

timer.performWithDelay(100, 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

what’s the best way to fix this? Basically i just want to restart game.lua when i touch the restart button in the game.
*PS: I have already included local director = require (“director”) in my main.lua [import]uid: 114765 topic_id: 23404 reply_id: 323404[/import]

It might help to see the whole file. For instance I don’t see where you’re creating and returning “localGroup”.

Frankly unless your restart.lua is going to display something, I wouldn’t restart it this way. I would just stay within game.lua and structure it so that you have a function that resets all the variables to the beginning. you might have to structure your code that your game is a function that gets called within game.lua to start it off.

But if you’re going to do it your way, why not make a “Level Complete” background or “Game Over” and have that restart.lua do something.

[import]uid: 19626 topic_id: 23404 reply_id: 93783[/import]

hey there, i created local director in main.lua which i redirect to menu.lua (main menu for the game).

and im interested in your statement about this:

Frankly unless your restart.lua is going to display something, I wouldn’t restart it this way. I would just stay within game.lua and structure it so that you have a function that resets all the variables to the beginning. you might have to structure your code that your game is a function that gets called within game.lua to start it off.
how to “restart” a game within just game.lua? I dont understand.

so basically i change this line?

director:changeScene ("restart")

to what? [import]uid: 114765 topic_id: 23404 reply_id: 94037[/import]

In my game, I have a simple function that has all my resets (resetting scores, etc etc) and at the end I have this:

director:changeScene (“level1”)

Basically whatever level I am on, I have the template stuff at top one of which is the reset button. Then I have other stuff that is specific to the level but if I hit an enemy or something like that it just re calls the same scene.

The way you are doing it going to another module and doing some weird stuff seems like it’s overly complicated.

To ensure the scene changes correctly, you can add a slight delay (I usually do something like 500 on delay). Not sure why that works on some scenes and not others, but it works :slight_smile:

ng
[import]uid: 61600 topic_id: 23404 reply_id: 107142[/import]