How do I wait for an EventListener initiated routine to end?

I’ve got one of these going:

Runtime:addEventListener(“enterFrame”, dostuff);

and later on I’ll kill it with one of these:

Runtime:removeEventListener(“enterFrame”, dostuff);

The problem is that even after I kill it the program might still be in the middle of the “dostuff” routine. Therefore, I’d like to kill the listener and then WAIT for the “dostuff” routine to finish running (if applicable) before continuing. We’re talking about just an unnoticable (to the player) instant here, but if I want to do something like nil a variable that is used in “dostuff” for garbage collection I can’t do it immediately after killing the listener if we’re still in the middle of “dostuff” at the time. So I need to kill the listener, wait for any currently running “dostuff” routine to end, then do my thing.

So how do I wait for “dostuff” to finish?

Thanks much!
[import]uid: 5540 topic_id: 24053 reply_id: 324053[/import]

This one is really tough. After I learned how to not use the enterFrame at all, I never looked back. But I understand there are a lot of ways to work, so…

Your talking about state management here. What you would do is have the enterFrame check the state of your prog (ie. if(gameIsRunning)then … or if(gameIsDone)then… Then you could set your state somewhere else in the code.

I’ll keep an eye on this post and see if you figure out something that works for you…and try to help you get’r done. :slight_smile:

And as always, there are a lot of ways to “skin a cat”… I need to come up with a new saying…that one really is bad isn’t it…:slight_smile: [import]uid: 21331 topic_id: 24053 reply_id: 96986[/import]

Ah, I’m new to Corona and didn’t realize enterFrame isn’t the way to go… everything I’ve come across so far gave me the impression it was. Thankfully, even though the issue can throw errors in the simulator’s output, they don’t actually stop or in any way seem to affect my game (including on the actual device).

Any suggestions on sample code that will show me how to do what enterFrame does in another way? The core of my game is done so I’m not going to change it now, but there is always the next game!

Thanks very much for the information!
[import]uid: 5540 topic_id: 24053 reply_id: 96997[/import]

Remember, there are soooo many ways to do things. I’m sure many still do take care of business in their enterFrame… but I found the best performance when I let Corona have as many cycles as possible…

That being said, leaving errors in the simulator output is probably not a good idea, things have a tendency of functioning poorly after even a seemingly, unrelated error fires somewhere else in your code.

For what you are doing, you may want to try my suggestion of state management. I did this for quite awhile at first. When you want to remove your enterFrame Listener, simply change the state variable and your enterFrame Listener will know its time to clean up and remove itself.

For now, since it appears you are pretty well finished, I want to help you get rid of errors, and strengthen your core logic…squish them bugs! :slight_smile:

Not using enterFrame is just the technique of timers and events, turning your whole game into an event driven system. Things that we normally took care of in our “Windows Main” function in old days/other languages, now can happen within timers. I’ve recently started a blog for this very thing, so I will try to put a solid example of this together in the next few days… [import]uid: 21331 topic_id: 24053 reply_id: 96999[/import]

One method is to put all the data you’re working with in one table (or collection of them) and have a flag (just another value) to indicate that the table is still in use (or “dirty”)

Once the function (enterFrame or otherwise) exits the rest of your code will know that it is not being used any more. This is the basics behind garbage collection. [import]uid: 8271 topic_id: 24053 reply_id: 97028[/import]