Oct 132011
 

A tutorial teaching you to add a simple pause/unpause button to your iPhone or Android app.

The code below is plug-and-play and features a simple bouncing ball and white rectangle representing a pause button.

The ball bounces because of physics, so that is the only thing we are changing in the pause/unpause functions currently, however you could easily add timer.pause (if you are a subscriber, you need daily builds for that ;)) or the like.

I hope it helps someone!

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
27
28
29
30
31
32
33
34
35
36
37
38
39
--Hide status bar
display.setStatusBar(display.HiddenStatusBar)

--Physics
require ( "physics" )
physics.start()
physics.setGravity( 0, 7.5 )

--Paused variable
local paused = false

--Background image
local bg = display.newRect( 0, 0, 320, 480 )
bg:setFillColor(120,120,250)

--Floor
local floor = display.newRect( 0, 460, 320, 40 )
floor:setFillColor(200,150,100)
physics.addBody(floor, "static", {bounce=0.6})

--Ball
local ball = display.newCircle( 160, 200, 25 )
ball:setFillColor(220,0,0)
physics.addBody(ball, "dynamic", {density=0.1, bounce=0.8})

--Pause button
local pauseBtn = display.newRect( 40, 40, 60, 30 )

--Pause function
local function pause ()
if paused == false then
physics.pause()
paused = true
elseif paused == true then
physics.start()
paused = false
end
end
pauseBtn:addEventListener("tap", pause)

Enjoy!

Peach Pellen :)

PS – I apologize that the indents do not appear to be showing up on the post, they’re there, I swear!

Like this post? Subscribe to the monthly newsletter!

 

  9 Responses to “A Simple Pause Button”

  1. No worries – glad it’s all sorted now.

    As to the email thing, hey, at least you comment subscribe – I never remember to and then end up keeping the page minimized for days at a time. My dock is terribly full ;)

  2. Thanks Peach,
    I didn’t see your post because I typed in the wrong email. I was actually able to figure it out and is working great!

  3. You’d remove/pause whatever was moving the object, like gravity or a Runtime listener.

    For the code I can’t test that. See how you have images? I don’t have those images. (For example.)

    If you post on the forum you be able to get a hand, although providing your project file would help – and I can try to check it out there as well.

    Peach :)

  4. Oh and can you pause the accelerometer as well?

  5. Complements on another FANTASTIC tutorial. There have been many times when I want to hit my head on a wall because i’ve been trying to code something to work all day and I come over to your site and I figure it out in two minutes. One thing that I’m still having trouble with is I start the game with a start button that the user presses, it displays the pause button (and the pause button works), I hide the pause button, then show a resume button. The problem is I don’t know how to get the resume button to resume the game and it isn’t cause I didn’t add “physics.start()”
    My code is really confusing and I apologize but I figured you’d want to see the code, I’ve been trying different things all day so some stuff might be straggling.
    Is there a simpler solution?

    local PauseResumeLevel = function()

    pause = display.newImage(“Pause Button.png”)
    pause.x = 29
    pause.y = 19

    local Resume = display.newImage(“Resume Button.png”)
    Resume.x = 160
    Resume.y = 240

    function resumeGame(e)
    if(e.phase == “began”) then
    physics.start()
    deletePause()
    end
    end
    –START REARANGING THE INFO

    function pauseGame(e)
    if(e.phase == “began”) then
    physics.pause()
    deletePause()
    local Resume
    end
    end

    deletePause = function()
    pause:removeSelf()
    pause = nil
    end

    deleteResume = function()
    Resume:removeSelf()
    Resume = nil
    end

    pause:addEventListener(“touch”, pauseGame)
    Resume:addEventListener(“touch”, ResumeGame)

    end

    Thanks,
    Danny

  6. Hi Max, I don’t use Zwoptex but you could try sprite:pause() as that is what you’d normally use. (I have no idea if this is different with Zwoptex or not, sorry!)

    As to pausing timers where you increase difficulty like in the Dash template (thanks for the kind words on that by the way) you would still pause and resume, you’d just do it will all the timers. You’d check first to see if they existed though, so you weren’t trying to pause them if they were nil.

    Like; if timer1 then timer.pause(timer1)

    That kind of thing.

    Peach :)

  7. Thanks again for an awesome tutorial. I have two question though… 1) is there a way to pause a sprite sheet animation (I’m using zwoptex for my sprite sheets, etc.) 2) I know pausing and resuming timers works well, but how about pausing timers where you add increasing difficulty like in your awesome Dash template… which by the way everyone reading should be buying right now!

  8. Thank you, I now have yet another thing to wish we had in Australia. It’s like poptarts ^-^;

  9. As always, very cool.

    You deserve a case of zomgtwinkies.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.