Pause Button Problem

I have spawning object falling down. When i click the pause button it pauses, but when i resume it, all of my spawning objects fall in a big group together. Please help me.

These are my spawning codes
[lua]–Pause button
local pauseBtn = display.newRect( 40, 40, 60, 30 )
localGroup:insert(pauseBtn)

–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)
[import]uid: 132369 topic_id: 24481 reply_id: 324481[/import]

I don’t understand what your question is, with the code you posted, yes pausing it will pause the physics, then resuming will resume physics.

Where are your “spawned” objects that are falling, what is the example code? [import]uid: 129334 topic_id: 24481 reply_id: 99131[/import]

Thanks for replying, these are my spawning codes
[lua]local function spawnOrb()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
orb = display.newImage( “orb.png” )
orb.x = randomPos
orb.y = -100
orb.myName = “orb”
physics.addBody(orb , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( orb ) --PEACH: Adds new orbs to localGroup for use with Director
end

end
orbTimer = timer.performWithDelay( 1500, spawnOrb, 0 )
–PEACH: Name timer so can cancel later, replace 500 with 0 for unlimited repeats

–PEACH: Spawn bombs
local function spawnBomb()
if( spawnTrue == true )then
local randomPos = math.random ( 0, 480 )
bomb = display.newImage( “bomb.png” )
bomb.x = randomPos
bomb.y = -100
bomb.myName = “bomb”
physics.addBody(bomb , “dynamic”, {isSensor = true, radius = 15})
localGroup:insert( bomb ) --PEACH: Adds new bombs to localGroup for use with Director
end

end
bombTimer = timer.performWithDelay( 2700, spawnBomb, 0 ) [import]uid: 132369 topic_id: 24481 reply_id: 99132[/import]

Hi mazensalih1, what is happening is that you are not “pausing” the spawning functions for your orbs and bombs. So basically, your pausing the physics, but your still spawning the objects in mid-air! When you unpause the physics, they all fall down at once.

You need to pause the timers like so:
[lua] --Pause button
local pauseBtn = display.newRect( 40, 40, 60, 30 )
localGroup:insert(pauseBtn)

–Pause function
local function pause ()
if paused == false then
physics.pause()
timer.cancel(orbTimer)
timer.cancel(bombTimer)
paused = true
elseif paused == true then
physics.start()
orbTimer = timer.performWithDelay( 1500, spawnOrb, 0 )
bombTimer = timer.performWithDelay( 2700, spawnBomb, 0 )
paused = false
end
end
pauseBtn:addEventListener(“tap”, pause)[/lua]

I believe there’s a “cancel all timers” function so that you don’t have to do it to each and everyone. (See the link below, and scroll down to the Timers section):
http://blog.anscamobile.com/2011/08/corona-sdk-memory-leak-prevention-101/ [import]uid: 129334 topic_id: 24481 reply_id: 99135[/import]

I knew it had something with that so i tried all kinds of codes and they didn’t work. So thank you really much. I will test them out. And one quick question. How do i make buttons come up when i click the pause button like resume or main menu ?
[import]uid: 132369 topic_id: 24481 reply_id: 99136[/import]

For making a “main menu” button, there’s many different ways (some better than others). I personally have a “main menu” button already created, and either have it “slide” into view when the pause is pressed, then “slide” back out. I used tranisitions for this way.

Another way is to “hide” the button, but when you press pause it will appear.

For example, have your button set to [lua]pauseBtn.alpha = 0[/lua], then when paused, set alpha = 1. When unpaused, set back to alpha = 0 to hide it.

Sample:
[lua]–Pause button
local pauseBtn = display.newRect( 40, 40, 60, 30 )
localGroup:insert(pauseBtn)
pauseBtn.alpha = 0

–Pause function
local function pause ()
if paused == false then
physics.pause()
timer.cancel(orbTimer)
timer.cancel(bombTimer)
pauseBtn.alpha = 1
paused = true
elseif paused == true then
physics.start()
orbTimer = timer.performWithDelay( 1500, spawnOrb, 0 )
bombTimer = timer.performWithDelay( 2700, spawnBomb, 0 )
pauseBtn.alpha = 0
paused = false
end
end
pauseBtn:addEventListener(“tap”, pause)[/lua]

Keep in mind this is sample code, if you put this in your game, it’s going to hide your pause button! [import]uid: 129334 topic_id: 24481 reply_id: 99139[/import]

Yes i understand now. Thanks Mark! [import]uid: 132369 topic_id: 24481 reply_id: 99140[/import]

What do i need to add to this to make it work ? [import]uid: 132369 topic_id: 24481 reply_id: 99149[/import]

This works (I believe) but change the button to be your main menu button. And change the x,y coordinates so it’s not on top of your pause button.

Basically it’s just hiding and unhiding the button, with alpha = 0 or alpha = 1 [import]uid: 129334 topic_id: 24481 reply_id: 99151[/import]

Thank you. I got it! [import]uid: 132369 topic_id: 24481 reply_id: 99153[/import]

[lua]-- simple rectangle
local testRect = display.newRect(0,0,50,50)
testRect:setFillColor(150,0,0);
testRect.alpha = 0
localGroup:insert(testRect)

–Pause function
local function pause ()
if paused == false then
physics.pause()
timer.cancel(orbTimer)
timer.cancel(bombTimer)
testRect.alpha = 1
paused = true
elseif paused == true then
physics.start()
orbTimer = timer.performWithDelay( 1500, spawnOrb, 0 )
bombTimer = timer.performWithDelay( 2700, spawnBomb, 0 )
testRect.alpha = 0
paused = false
end
end
testRect:addEventListener(“tap”, pause)[/lua]

Try this, you will need to adjust this “NEW” button which is just a rectangle. But in theory, when you press your “pause” button, this will make the “rectangle” appear. When you “unpause” the"rectangle" will disappear.

If anybody wants to chime in and correct me, please do, because I too am new! After you get this working, think about actually making buttons, that has a default and over state, to simulate you pressing it (2 different images). [import]uid: 129334 topic_id: 24481 reply_id: 99154[/import]

Okay i have this so far for my pause button
[lua]local pauseBtn = display.newRect( 40, 40, 60, 30 )
localGroup:insert(pauseBtn)
back.alpha = 0

–Pause function
local function pause ()
if paused == false then
physics.pause()
timer.cancel(orbTimer)
timer.cancel(bombTimer)
back.alpha = 1
paused = true
elseif paused == true then
physics.start()
orbTimer = timer.performWithDelay( 1500, spawnOrb, 0 )
bombTimer = timer.performWithDelay( 2700, spawnBomb, 0 )
back.alpha = 0
paused = false
end
end
pauseBtn:addEventListener(“tap”, pause)


–There is a problem with the codes below

back = display.newRect( 40, 40, 60, 70 )
localGroup:insert(back)

local function pressBack (event)
if event.phase == “ended” then
director:changeScene (“menu”)
end
end

backbutton:addEventListener (“touch”, pressBack) [import]uid: 132369 topic_id: 24481 reply_id: 99156[/import]

It seems you are having issues with something different now, scene changing, is that correct?

For me, I based my first game (which I’m still developing), around this GAME TEMPLATE:
http://developer.anscamobile.com/code/game-template

This has scene changes for you to check out, then you can build off of this.

I’m sure there’s other templates out there, but currently this is my choice. [import]uid: 129334 topic_id: 24481 reply_id: 99157[/import]