-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 pauseButton:addEventListener("tap", pauseGame) 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!

use a simple "isPaused = true" variable to toggle if you're paused or not.

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.) :P

1
2
3
4
5
6
7
8
local function pauseGame(event)
        isPaused = !isPaused
        if (isPaused) then
                -- Remove Listeners/Timers
        else
                -- Add Listeners/Timers
        end
end

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

1
2
3
4
5
6
7
   if isPaused then
     isPaused = false
     -- do stuff
   else
     isPaused = true
     -- do stuff
   end

How do I remove these things?

1
2
3
4
        leftButton:addEventListener("touch", touchLeft)
        rightButton:addEventListener("touch", touchRight)
        Runtime:addEventListener("enterFrame", movePiggyBank)
        Runtime:addEventListener("touch", stopPiggyBank)

Anyone know?

To remove do this;

leftButton:removeEventListener("touch", touchLeft)

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 :)

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.

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. :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        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)

Plug and play code would help. Any errors?

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

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.

I finally got it! Thanks!

views:1491 update:2011/12/1 20:56:45
corona forums © 2003-2011