Techority Challenge: Issues with restarting a game

Hiyas!

I have the game set up with :

MainMenu
Game

When I start the app, it works fine. Then I hit START and the Game starts.
I play the game fine and when I get GameOver I have a button to hit to go back to MainMenu. Now, to start a new game, I hit START again.

Here’s where it goes weird…

Physics stop working properly.
Transitions don’t fire.
Objects which interact with each other don’t.
Functions don’t fire when they are supposed to.

It works 100% when I play the game the first time, but going to menu and back into the game screws it all up with errors about unable to PLAY animations which are NIL, Unable to do transitions, Collisions giving errors.
It says values cannot be accessed as they are NIL, but these are all created again when the game starts again.
attempt to index upvalue ‘gameObject’ (a nil value)
attempt to index upvalue ‘mainGroup’ (a nil value)

mainGroup DOES exist! It gets re-created when the game restarts.
Any ideas?
Thanks.
I’m using Director, btw, so it should clean up stuff. I even have cleanup() functions. [import]uid: 10389 topic_id: 22427 reply_id: 322427[/import]

if the mainGroup you are talking about is the director one that should be in ur main.lua file and only should be created once when the game is loaded.
[lua]–main.lua
director = require (“director”)
local mainGroup = display.newGroup()
local function main()

mainGroup: insert (director.directorView)

–Director changes scene for first Scene that will be shown
director: changeScene (“firstScene”)

return true
end[/lua]

also the function for cleaning in director is not cleanup its clean and has to be a global function

[lua]–level clean up
function clean ()

–Cancel Transitions
cancelTrans()

–Remove runtime listeners
Runtime: removeEventListener ( “key”, onKeyEvent )
Runtime: removeEventListener ( “enterFrame”, moveObjects )

end[/lua]

hope this helps, otherwise if you could post some code then maybe i could help further
[import]uid: 126161 topic_id: 22427 reply_id: 89445[/import]

Thanks [import]uid: 10389 topic_id: 22427 reply_id: 89504[/import]

Okay now I understand the mainGroup/localGroup thing and fixed that, but still get this starting the game again:

Runtime error
…/waulok/Dropbox/Corona Projects/gamename/game.lua:303: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
…/waulok/Dropbox/Corona Projects/Sneaker Run/game.lua:303: in function <… projects>
(tail call): ?
?: in function <?:1155>
?: in function <?:215>

The ‘cleanup’ I was talking about is this code I found on the forum:

[lua]local function cleanGroups ( curGroup, level )
if curGroup.numChildren then
while curGroup.numChildren > 0 do
cleanGroups ( curGroup[curGroup.numChildren], level+1 )
end
if level > 0 then
curGroup:removeSelf()
end
else
curGroup:removeSelf()
curGroup = nil
return
end
end[/lua] [import]uid: 10389 topic_id: 22427 reply_id: 89505[/import] </…>

I also get this. I don’t understand why the values don’t initialise when I restar the game:

Runtime error
…/waulok/Dropbox/Corona Projects/Sneaker Run/game.lua:371: attempt to index upvalue ‘sneakerAnim’ (a nil value)
stack traceback:
[C]: ?
…/waulok/Dropbox/Corona Projects/Sneaker Run/game.lua:371: in function <… projects run>
?: in function <?:215> [import]uid: 10389 topic_id: 22427 reply_id: 89530[/import] </…>

At a higher level of the app design, consider doing this. Make your game.lua a module, and then in main.lua you can force a complete refresh of the game code by doing this before calling director:changeScene(“game”):

package.loaded["game"] = nil --force the module's code to be executed again via another require() require ("game") --load the module and execute its code [import]uid: 23636 topic_id: 22427 reply_id: 89533[/import]

I’m getting exactly the same problem, unfortunately the above code didn’t fix it for me.

What I noticed is that when the game ends and you go back to menu there are a lot of memory leaks (I’m using the memory tracking function from here: http://blog.anscamobile.com/2011/08/corona-sdk-memory-leak-prevention-101).
In fact looks like almost no memory is freed when you go back to menu.

So I’m guessing this might be a memory leaking problem, but it’s just a wild guess.
[import]uid: 145398 topic_id: 22427 reply_id: 110905[/import]

@lacho.tomov is all the memory still there or is part of it getting removed when you are going back? [import]uid: 126161 topic_id: 22427 reply_id: 110936[/import]

Hi,

Actually a small portion of the memory gets removed - like it’s 357 in game, and when you go back to menu it gets 347.

But I think this might be because I’m preloading some sprites in a global var on game start - that’s when mem usage quickly jumps to 357. And then when going back to menu looks like this sprites global var doesn’t get cleaned properly, even though I’m dispose()-ing the sprites. Anyways will play a bit with that.

Btw I found a post that may turn out to be very useful in this case - about getting rid of module(…, package.seeall) and using only local modules. And a rewritten version of Director class to utilize this:

http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
http://developer.anscamobile.com/forum/2011/09/07/modified-director-jonathan-beebes-blog-post

Didn’t fix the initial problem, but it’s still very good for fixing mem leaks. [import]uid: 145398 topic_id: 22427 reply_id: 111050[/import]

Ok, I finally managed to find the initial problem - where the second time game loads, it’s all messed up.

In my case it turned out to be an event listener on a global var that gets added on changeScene(“game”), but since it’s global it doesn’t get removed on changeScene(“menu”). (or not sure - do listeners on local vars get removed automatically?)

So the second time you changeScene(“game”) - this event listener gets added once again (as it was already there from previous loads). Or at least this is how it looks to me…

So the way to fix it was to remove that event listener when going out of the game - all those errors disappeared then.

And another tip that may be helpful - I actually migrated from the Director class to the official Storyboard API (http://blog.anscamobile.com/2011/11/introducing-the-storyboard-api/)

So now the event listeners are created in scene:enterScene() and removed in scene:exitScene()
This way it’s a bit easier to track them.

Hope this helps. [import]uid: 145398 topic_id: 22427 reply_id: 111078[/import]