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

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) &lt; _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>

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?

[lua] 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)[/lua]

And your changeMyScene function would include all additional cleaning up you may want to do before you actually change the scene to gameover.lua. You may also need to use another performWithDelay for actually changing the scene. (I often find that performWithDelay can solve problems that I cannot otherwise solve.)

Please note, in my game, I am able to remove runtime event listener from within the function. In other words, I have something like the following, and it does not cause the error you’ve described above:

[lua]local function myFunction()
– do this and that, and then…
if (myCondition == true) then
Runtime:removeEventListener(“enterFrame”, myFunction)
end
end
Runtime:addEventListener(“enterFrame”, myFunction);[/lua]

By the way, for many of my functions, I don’t include return true. I do include it in some functions when I feel they need it (for whatever reason), but otherwise, I don’t judiciously include it. I wonder if I should always include it (and why might it be advisable?)

Anyhow, I hope performWithDelay will solve the issue you are dealing with. If not, I hope you’ll find other ways to make it work.

Naomi
[import]uid: 67217 topic_id: 17404 reply_id: 65877[/import]

You need to return true on touch and tap events for certain. If you don’t or if you return false then those events propagate to the next display object that’s also being tapped. This is something known as event bubbling. So if your actor that you tapped is in front of a tree which is in front of your background and you tap on the actor and return false (which may be the default if nothing is returned), then the tree gets the event passed to it. If it doesn’t handle it or returns false, it then goes to the background to be handled.
I don’t know how timers, transitions and other events handle the need to know if the event was handled or not. For instance, transition.to calls an onComplete function. If that function returns false, does the transition.to actually complete and clean up itself? Or timer.performWithDelay, does that timer clean itself up when its done if the called function doesn’t return correctly? I simply don’t know Lua and Corona SDK’s internals enough to know, so I as a safety net for my ignorance always try to “return true” if its called from an event.

Thanks for the suggestion. I’m going to try that. I might even put the removeEventListener in the cleanup code that will be called from the timer. [import]uid: 19626 topic_id: 17404 reply_id: 65960[/import]

Hey, Rob, thanks for explaining why we’d want to add return true. I do add it in the specific cases you noted above.

About timers and transitions, I don’t really know either. I vaguely remember reading somewhere on the forum about how, upon completion of timer/transition, timer/transition will become nil – but I could’ve misread/misunderstood. Timers and transitions are necessity for me, but they are real pain in the neck to deal with.

I hope using timer will solve the issue for you anyhow.

Naomi

[import]uid: 67217 topic_id: 17404 reply_id: 66008[/import]

Naomi, you rock my world. Bug squashed.

Now I have to figure out what qualifies as “end of level”…

[import]uid: 19626 topic_id: 17404 reply_id: 66118[/import]

Hey, Rob, it’s so great to hear it. Squashing bugs are so satisfying!

Naomi [import]uid: 67217 topic_id: 17404 reply_id: 66121[/import]