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
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><br> ?: in function <?:215><br><br>
Line 355 is this line of code:<br> Runtime:removeEventListener("enterFrame", gameLoop)<br>
and currently is inside my gameLoop function:<br> gameLoop = function()<br> if gameOver then<br> Runtime:removeEventListener("enterFrame", gameLoop)<br> director:changeScene("gameover", "crossFade", 500)<br> return true <br> end<br> -- move enemies<br> for i=1,#enemies do<br> if rand(1000) < _G.gameSettings.currentLevel * 6 and enemies[i].direction ~= 0 then<br> enemies[i].direction = 0<br> killPlayer(i)<br> end<br> end<br> return true<br> end<br>
But… I’ve tried to put the removeEventListener inside the clean() function. I’ve tried to put it in the chain reaction from the killPlayer() function (I trimmed a bunch of irrelevent code from the example above, leaving the killPlayer function for you to see)
So I’m getting the feeling that I cannot remove the enterFrame listener from within the listener itself. But if I return from all of the calls, there really isn’t any code left to switch scenes.
Any idea where I’m screwing up?
Thanks
Rob
[import]uid: 19626 topic_id: 17404 reply_id: 317404[/import] </…miracle>