i was wondering how can i make my game pause when the user clicks on the pause button? and how can i make it resume once the user clicks resume? [import]uid: 10827 topic_id: 4172 reply_id: 304172[/import]
Create a variable such as ‘gameIsActive’ and set it to true when the game starts.
For everything that you create (such as moving objects, etc) that you want to be paused, start each one of those code blocks with:
[blockcode]
if gameIsActive then
[/blockcode]
Then, on your touch event for the pause button, simply set gameIsActive to false and all of those things will stop! Also in your pause event, make sure to remove any event listeners and accelerometer events, etc.
In your resume codeblock (if gameIsActive then … else --> resume code), simply re-instate all of those event listeners and set gameIsActive back to true.
That’s how I do it and it works great. [import]uid: 7849 topic_id: 4172 reply_id: 12999[/import]
I’d just like to add on to Jon’s response, but:
“simply re-instate all of those event listeners and set gameIsActive back to true.”
This is the more difficult part, as for example in my game I start the event listeners only when they’re needed, so if I were to simply reinstate them all it wouldn’t work.
I have no idea if my approach is efficient or even good practice, but it’s the only way I could figure out how to do it. At the end of each function that handles movement I do this:
Runtime:addEventListener (“enterFrame”, nextfunction)
To call the relevant next function. And at the beginning of that function I remove the listener from the previous one. Then I just need a variable at the beginning of each function like, currentFunction = “thisfunctionsname” and in my pause code I know which listener I need to resume.
I’d love to know if there’s a better way to handle enterFrame listeners, but the way I’m doing it it’s working fine for me. [import]uid: 10835 topic_id: 4172 reply_id: 13005[/import]
Is there any way to pause transitions yet? I thought I saw something on the roadmap about it but wasn’t sure if it was added in this latest release of Corona or not. If not, does anyone know of any type of workaround to pause transitions?
I have used your advice Jon to pause all aspects of my game but I have not found out a way to pause a transition, any suggestions?
Thanks,
-Clark [import]uid: 5786 topic_id: 4172 reply_id: 13454[/import]
@bonesmalone133: This may not be a good solution for everyone, but it’s what I did in the recent Ghosts vs. Monsters sample app. During the game, there is a pause button. There is a transition going that bobs the character up and down (transition.to on their y value). When they click the pause button, I simply cancel the transition (transition.cancel). You can cancel them as long as you store the transition in a variable so you can identify it later in case you want to cancel it.
When they resume the game, I re-instate the transition (I had to re-copy/paste the code that started the transition in the resume function). Like I said, it’s pretty rough workaround, but in many cases, it’ll work. [import]uid: 7849 topic_id: 4172 reply_id: 13626[/import]
Ta [import]uid: 9371 topic_id: 4172 reply_id: 13635[/import]
For LINEAR transitions (no special easing stuff) on x, y, xScale, and yScale values, consider checking out my doTransition() function in my newly released BeebeGames Class:
http://jonbeebe.tumblr.com/post/2320935925/beebegames-class-for-corona-sdk
They can be paused/resumed with a simple switch (.isActive = true/false) [import]uid: 7849 topic_id: 4172 reply_id: 14086[/import]
I’m new to Corona but this issue is exactly the same in Flash games.
I too use the “gameIsActive” approach and instead of removing event listeners when the game is paused I simply have a check for that as the first thing in the listener function. Some may argue that this is inefficient, having a bunch of event listeners that don’t do anything, but you’d have all those event listeners going while the game is active so it’s not any worse to have them going while the game is paused.
For future projects though I’m probably going to actually call a “pause” method for all objects (obviously this means I have to write a “pause” method for them first.) Then the objects can be in charge of removing their own event listeners and transitions. [import]uid: 12108 topic_id: 4172 reply_id: 14166[/import]