Pause/Resume Button, How To Make It Disappear Then Reappear

hi guys, i’ve been learning corona for the past few weeks (not full time though as i have another business to do), but basically I’m now confused on how to make resume button appear after I pause a game and how do I make the pause button reappear when I resume the game?

I have learned from techority.com as well as searching on Google but i need more details…

here’s what i have basically

[code]local gameIsActive = true
local paused = false

local pauseBtn = display.newImage(“pause.jpg”)
pauseBtn.x = 50
pauseBtn.y = 100

–Pause function
local function pause ()
if paused == false then
physics.pause()
paused = true
gameIsActive = false
pauseBtn:removeSelf()
local resumeBtn = display.newImage(“play.jpg”)
resumeBtn.x = 50
resumeBtn.y = 100
end
end
pauseBtn:addEventListener(“tap”, pause)

local function resume ()
if paused == true then
physics.start()
paused = false
gameIsActive = true
resumeBtn:removeSelf()
local pauseBtn = display.newImage(“pause.jpg”)
pauseBtn.x = 50
pauseBtn.y = 100
end
end
resumeBtn:addEventListener(“tap”, resume)[/code]

*PS I added “if gameisactive=true” on every movement function

previously, i didnt separate the resume function from the pause function, i did it with “else if”. the code worked if I didnt add the resume button and destroy the pause button…

but I want to replace the pause button WITH the resume button when the game is paused and re-create the pause button and remove the resume button when the game is back to active, how to do that? [import]uid: 114765 topic_id: 20249 reply_id: 320249[/import]

My way with this is probably not the most elegant, but it’s simple.

I’d move the pause button off-screen while it’s not needed and replace it with resume button. And same vice versa.

Then you don’t need to remove anything, just change the x or y coordinate enough to take it far outside the screen when not needed. [import]uid: 10416 topic_id: 20249 reply_id: 79088[/import]

@Harm - if it works it works, no harm in it not being elegant :wink:

@Brad - You may also consider making a sprite sheet of the two buttons. The button would be a sprite that changed images (frames) depending on the game state.

Peach :slight_smile: [import]uid: 52491 topic_id: 20249 reply_id: 79095[/import]

[quote]I’d move the pause button off-screen while it’s not needed and replace it with resume button. And same vice versa.

Then you don’t need to remove anything, just change the x or y coordinate enough to take it far outside the screen when not needed.[/quote]

How to do this? Sorry but can you give me in form of example as I have zero experience with this?

Again, im not sure how to do this? [import]uid: 114765 topic_id: 20249 reply_id: 79100[/import]

My non-elegant and dirty way :wink:
I just define both buttons right at start and place one outside the screen. Then switch the places when button is pressed.

There is better ways to do this but I think this is easy to understand to get started :slight_smile:

[code]
local gameIsActive = true
local paused = false

local pauseBtn = display.newImage(“pause.jpg”)
pauseBtn.x = 50
pauseBtn.y = 100

local resumeBtn = display.newImage(“play.jpg”)
resumeBtn.x = 2050
resumeBtn.y = 100

–Pause function
local function pause ()
if paused == false then
physics.pause()
paused = true
gameIsActive = false
– Let’s change the resume button into screen
pauseBtn.x = 2050
resumeBtn.x = 50
end
end
pauseBtn:addEventListener(“tap”, pause)

local function resume ()
if paused == true then
physics.start()
paused = false
gameIsActive = true
– Let’s change the pause button back to screen
pauseBtn.x = 50
resumeBtn.x = 2050
end
end
resumeBtn:addEventListener(“tap”, resume)
[/code] [import]uid: 10416 topic_id: 20249 reply_id: 79107[/import]

@brad2000xi

This maybe a fix for ya, it right out of the api pages with
a little mod, but you could play with it…to be something
I think your looking for…

[code]
– ************************************************************
– plug in play, right out of the Corona SDK API pages
– with a little mod to work with our values.

– ************************************************************
– For my buttons I used a .png file or .jpg will work also.
– ************************************************************
– Have fun! (sharp1959) AKA (Larry)
– ************************************************************
local gameIsActive = true;
local paused = false;

local resumeBtn = display.newImage(“play.png”);
resumeBtn.x = 100;
resumeBtn.y = 100;
local pauseBtn = display.newImage(“pause.png”);
pauseBtn.x = 100;
pauseBtn.y = 100;

–Display when program starts to set our place holder for our buttons
–just a little trick, set to 0.01 alpha so we can’t see it, but the
– program does…:wink:

local pauseButton = display.newRect( 50, 50, 100, 50 );
pauseButton.x = 100;
pauseButton.y = 100;
pauseButton.alpha = 0.01;

function pauseUnPause( event )

if event.phase == “began” then

– This is just a place holder for event phase…
event.target.alpha = 0.01;

elseif event.phase == “ended” or event.phase == “cancelled” then

– comment these two print statements whent done with debug
– print our ‘paused’ var to output to see what is going on…
print("\nGame Paused", paused);
print(“Game Active”, gameIsActive);

if paused == true then
paused = false;
gameIsActive = true;
pauseBtn.alpha = 1;
resumeBtn.alpha = 0;
else
paused = true;
gameIsActive = false;
pauseBtn.alpha = 0;
resumeBtn.alpha = 1;
end

event.target.alpha = 0.01;

end

end

pauseButton:addEventListener(“touch”, pauseUnPause);

– end of code
– ************************************************** [import]uid: 107633 topic_id: 20249 reply_id: 79126[/import]

thanks guys, now it works! [import]uid: 114765 topic_id: 20249 reply_id: 79199[/import]