-SOLVED- Pause Button

I’m going to make a pause button, but I don’t know what the method should look like. I know it will have [lua]pauseButton:addEventListener(“tap”, pauseGame)[/lua] to call the method, but I don’t know how I can detect whether It’s the first time I pressed the button to open the screen or close it. Basically, all the button does is remove listeners and timers. That pretty much pauses my game.

Thanks for the help! [import]uid: 103624 topic_id: 18359 reply_id: 318359[/import]

use a simple “isPaused = true” variable to toggle if you’re paused or not. [import]uid: 19626 topic_id: 18359 reply_id: 70378[/import]

I know a little Java, so I think this is what your’e saying. But what is the proper opposite sign? In Java, ! means opposite (I think.) :stuck_out_tongue:

[lua]local function pauseGame(event)
isPaused = !isPaused
if (isPaused) then
– Remove Listeners/Timers
else
– Add Listeners/Timers
end
end[/lua] [import]uid: 103624 topic_id: 18359 reply_id: 70394[/import]

Lua doesn’t have those type of operators per se.

 if isPaused then  
 isPaused = false  
 -- do stuff  
 else  
 isPaused = true  
 -- do stuff  
 end  

Though You probably could do

isPaused = not isPaused

[import]uid: 19626 topic_id: 18359 reply_id: 70395[/import]

How do I remove these things?
[lua] leftButton:addEventListener(“touch”, touchLeft)
rightButton:addEventListener(“touch”, touchRight)
Runtime:addEventListener(“enterFrame”, movePiggyBank)
Runtime:addEventListener(“touch”, stopPiggyBank)[/lua]

So this isn’t working like I had hoped. It only works if you pressed it for the first time.

[lua] local function pauseGame()
local isPaused
isPaused = not isPaused
if (isPaused) then
pauseButton:prepare(“pausep”)
pauseButton:play(“pausep”)
else
pauseButton:prepare(“pausen”)
pauseButton:play(“pausen”)
end
end[/lua]

Basically, I’m trying to flip the boolean every time it’s pressed. [import]uid: 103624 topic_id: 18359 reply_id: 70397[/import]

Anyone know? [import]uid: 103624 topic_id: 18359 reply_id: 70465[/import]

To remove do this;

[lua]leftButton:removeEventListener(“touch”, touchLeft)[/lua]

For each event.

For your pause button, is it sprite with only two frames? If so it makes more sense to set the current frame than do prepare/play each time.

This little article on flags has an example of toggling a value on and off; http://techority.com/2011/10/30/flags-what-and-why/

Peach :slight_smile:

PS - Are you using “tap” or “touch” in the event listener for the pause? Don’t use touch unless you’re specifying an event phase also, else it will fire twice, once on press and once on release. [import]uid: 52491 topic_id: 18359 reply_id: 70517[/import]

I’m using the same sprite sheet for the pause button and the left and right buttons. Also, I’m using “tap”.

The pause button isn’t working like it should. For some reason, the listeners aren’t removed and the pig somehow speeds up. :confused:
[lua] local isPaused = false
local function pauseGame()
isPaused = not isPaused
if (isPaused) then
pauseButton:prepare(“pausep”)
pauseButton:play(“pausep”)
piggyBank:addEventListener(“collision”, piggyBank)
leftButton:addEventListener(“touch”, touchLeft)
rightButton:addEventListener(“touch”, touchRight)
Runtime:addEventListener(“enterFrame”, movePiggyBank)
Runtime:addEventListener(“touch”, stopPiggyBank)
else
pauseButton:prepare(“pausen”)
pauseButton:play(“pausen”)
piggyBank:removeEventListener(“collision”, piggyBank)
leftButton:removeEventListener(“touch”, touchLeft)
rightButton:removeEventListener(“touch”, touchRight)
Runtime:removeEventListener(“enterFrame”, movePiggyBank)
Runtime:removeEventListener(“touch”, stopPiggyBank)
end
end
leftButton:addEventListener(“touch”, touchLeft)
rightButton:addEventListener(“touch”, touchRight)
Runtime:addEventListener(“enterFrame”, movePiggyBank)
Runtime:addEventListener(“touch”, stopPiggyBank)
pauseButton:addEventListener(“tap”, pauseGame)[/lua]

Edit: And how do you remove a timer like this?
[lua]timer.performWithDelay(math.random(750, 1500), createProjectiles, 0)[/lua] [import]uid: 103624 topic_id: 18359 reply_id: 70525[/import]

Plug and play code would help. Any errors?

For the timer give it a name;
[lua]myTimer = timer.performWithDelay(math.random(750, 1500), createProjectiles, 0)[/lua]

Then cancel it by doing timer.cancel(myTimer)

When you’re a subscriber you’ll be able to pause and resume timers, which is much easier for pausing/unpausing.

Peach. [import]uid: 52491 topic_id: 18359 reply_id: 70534[/import]

I finally got it! Thanks! [import]uid: 103624 topic_id: 18359 reply_id: 70589[/import]