To pause my app I add and remove enterFrame listeners. However, I also add and remove them in other parts of the app.
The question is how do I check to see if the runtime listener is already present?
Thanks,
Gullie
To pause my app I add and remove enterFrame listeners. However, I also add and remove them in other parts of the app.
The question is how do I check to see if the runtime listener is already present?
Thanks,
Gullie
I just pop a print into each (and every) enterFrame listener for testing (comment it out once done of course). After you initially set things up, they’re usually fine left alone. But if you have a lot of things you add later that add/remove them temporarily (like going to sub screens, etc), you might want to add the print back in to give things a quick check…
Obviously with this technique, it’s pretty easy to tell if an enterFrame doesn’t get removed… It’s a little tougher to tell if it one double added though (which is totally possible, and then instead of 60/sec, it fires off 120/sec).
If it’s *just* for pausing, you should probably use a global var like “isPaused” or such…
Finally, you can probably try to remove the listener first… An error return value (or hard error) would mean it wasn’t installed, of course. (I think it throws a hard error, so you’d need a pcall to do it this way).
Thanks for the reply.
I guess I’ll do a isPaused var to check before adding the listener again / potentially twice.
Gullie
This is what I would recommend:
Create a module similar to the following:
local M = { isPaused = false, } // Function to set the app as paused. function M:setPaused() self.isPaused = true end // Function to set the app as un-paused function M:setUnPaused() self.isPaused = false end // Function to get whether or not the app is paused function M:getPaused() return self.isPaused end return M
When your user presses the pause button…
– At the top of your code somewhere local pauseManager = require( “pauseManager” ) – or whatever you call the module – Set the game as paused pauseManager:setPaused()
In your runtime listener, wrap your logic around an if block:
local function runtimeListener( event ) – If the game isn’t paused if pauseManager:getPaused() == false then – Your code here end return true end – Add the event listener, note you now no longer have to keep re-adding it Runtime:addEventListener( “enterFrame”, runtimeListener )
This way you don’t have to worry about knowing if the runtime listener is already added because you no longer have to keep re-adding it. You simply don’t execute the logic in question if the game is paused.
Hope that helps.
I just pop a print into each (and every) enterFrame listener for testing (comment it out once done of course). After you initially set things up, they’re usually fine left alone. But if you have a lot of things you add later that add/remove them temporarily (like going to sub screens, etc), you might want to add the print back in to give things a quick check…
Obviously with this technique, it’s pretty easy to tell if an enterFrame doesn’t get removed… It’s a little tougher to tell if it one double added though (which is totally possible, and then instead of 60/sec, it fires off 120/sec).
If it’s *just* for pausing, you should probably use a global var like “isPaused” or such…
Finally, you can probably try to remove the listener first… An error return value (or hard error) would mean it wasn’t installed, of course. (I think it throws a hard error, so you’d need a pcall to do it this way).
Thanks for the reply.
I guess I’ll do a isPaused var to check before adding the listener again / potentially twice.
Gullie
This is what I would recommend:
Create a module similar to the following:
local M = { isPaused = false, } // Function to set the app as paused. function M:setPaused() self.isPaused = true end // Function to set the app as un-paused function M:setUnPaused() self.isPaused = false end // Function to get whether or not the app is paused function M:getPaused() return self.isPaused end return M
When your user presses the pause button…
– At the top of your code somewhere local pauseManager = require( “pauseManager” ) – or whatever you call the module – Set the game as paused pauseManager:setPaused()
In your runtime listener, wrap your logic around an if block:
local function runtimeListener( event ) – If the game isn’t paused if pauseManager:getPaused() == false then – Your code here end return true end – Add the event listener, note you now no longer have to keep re-adding it Runtime:addEventListener( “enterFrame”, runtimeListener )
This way you don’t have to worry about knowing if the runtime listener is already added because you no longer have to keep re-adding it. You simply don’t execute the logic in question if the game is paused.
Hope that helps.