Weird error with the enterFrame event

I don't think this is a director error, more me having a brain fart with adding and removing event handlers. I'm using Director 1.4 btw.

So in my game, main.lua calls menu.lua which when you tap the play button it loads game.lua.

All this is working fine.

In my game, I have an event listener on the enterFrame event:

Runtime:addEventListener("enterFrame", gameLoop)

Now gameLoop's job is to move the enemies around, respawn them, etc. But more importantly, there is code to call the kill player function. This starts off a chain reaction of timers that play some sounds, animations, etc. During this chain reaction, I detect if the player has been hit, reduce the number of lives, and test to see if I'm out of lives.

If I'm out of lives, I need to remove the enterFrame listener and then have director switch to gameover.lua

1
2
3
4
5
6
7
Runtime error
        ...racle/Lua/MyApp/MyApp/director.lua:1092: attempt to call method 'insert' (a nil value)
stack traceback:
        [C]: in function 'insert'
        ...racle/Lua/MyApp/MyApp/director.lua:1092: in function 'changeScene'
        ...miracle/Lua/MyApp/MyApp/game.lua:356: in function <...miracle/Lua/MyApp/MyApp/game.lua:353>
        ?: in function <?:215>

Hey, Rob, I'm just brainstorming (and this may not work with your game project at all), but based on what you noted above, how about using performWithDelay?

1
2
3
4
5
6
7
8
9
10
    gameLoop = function()
        if gameOver then
            Runtime:removeEventListener("enterFrame", gameLoop)
            -- delay by 1 or 10 or 100 milliseconds -- whatever that works
            timer.performWithDelay(1, changeMyScene)
            return true 
        end
    end
    -- EDIT:  if the above doesn't work, you may want to try the timer with changeMyScene
    -- inserted above the Runtime:removeEventListener("enterFrame", gameLoop)
views:1495 update:2011/11/8 8:33:25
corona forums © 2003-2011