Restart & Exit buttons?

Hi,

I’m very newbie on Corona and beause of that i’m working with some sample projects, one of those project are from Carlos Yanez tutplus tutorials.

Here’s the link http://code.tutsplus.com/tutorials/corona-sdk-create-a-helicopter-obstacles-game–mobile-21216

The only problem with this sample is that he don’t have a restart or exit buttons once you finish the game. So i’ve tryed to implement those but is not working because i think how the code have been writed for the demo.

The demo is not based on any director or storyboard api, so I try using the director ui with multiple configurations but nothing is working.

Also I try to set up the storyboards but also no working becase i’m no able to trigger a function like (os.exit()) on a button (if i put right on the alert function automatically exit the game without prompt)

And for the restart button, well… I have tried also to call all the functions from the beggining but always get errors and i don’t know how to initialize all to restart over.

Here is the game code:

-- Helicopter Game -- Developed by Carlos Yanez -- Hide Status Bar display.setStatusBar(display.HiddenStatusBar) -- Physics local physics = require('physics') physics.start() --physics.setDrawMode('hybrid') -- Graphics -- [Background] local gameBg = display.newImage('gameBg.png') -- [Title View] local titleBg local playBtn local creditsBtn local titleView -- [Credits] local creditsView -- TextFields local scoreTF -- Instructins Message local ins -- Helicopter local helicopter -- Blocks local blocks = {} -- Alert local alertView -- Sounds local bgMusic = audio.loadStream('POL-rocket-station-short.wav') local explo = audio.loadSound('explo.wav') -- Variables local timerSrc local yPos = {90, 140, 180} local speed = 5 local speedTimer local up = false local impulse = -60 -- Functions local Main = {} local startButtonListeners = {} local showCredits = {} local hideCredits = {} local showGameView = {} local gameListeners = {} local createBlock = {} local movePlayer = {} local increaseSpeed = {} local update = {} local alert = {} -- Main Function function Main() titleBg = display.newImage('titleBg.png') playBtn = display.newImage('playBtn.png', 220, 178) creditsBtn = display.newImage('creditsBtn.png', 204, 240) titleView = display.newGroup(titleBg, playBtn, creditsBtn) startButtonListeners('add') end function startButtonListeners(action) if(action == 'add') then playBtn:addEventListener('tap', showGameView) creditsBtn:addEventListener('tap', showCredits) else playBtn:removeEventListener('tap', showGameView) creditsBtn:removeEventListener('tap', showCredits) end end function showCredits:tap(e) playBtn.isVisible = false creditsBtn.isVisible = false creditsView = display.newImage('credits.png', -110, display.contentHeight-80) transition.to(creditsView, {time = 300, x = 55, onComplete = function() creditsView:addEventListener('tap', hideCredits) end}) end function hideCredits:tap(e) playBtn.isVisible = true creditsBtn.isVisible = true transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end}) end function showGameView:tap(e) transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end}) -- [Add GFX] -- Instructions Message ins = display.newImage('ins.png', 180, 270) transition.from(ins, {time = 200, alpha = 0.1, onComplete = function() timer.performWithDelay(2000, function() transition.to(ins, {time = 200, alpha = 0.1, onComplete = function() display.remove(ins) ins = nil end}) end) end}) -- TextFields scoreTF = display.newText('0', 450, 5, 'Marker Felt', 14) scoreTF:setTextColor(255, 255, 255) -- Helicopter helicopter = display.newImage('helicopter.png', 23, 152) -- Walls local top = display.newRect(0, 60, 480, 1) top:setFillColor(34, 34, 34) local bottom = display.newRect(0, 260, 480, 1) bottom:setFillColor(34, 34, 34) -- Add physics physics.addBody(helicopter) physics.addBody(top, 'static') physics.addBody(bottom, 'static') blocks = display.newGroup() gameListeners('add') audio.play(bgMusic, {loops = -1, channel = 1}) end function gameListeners(action) if(action == 'add') then gameBg:addEventListener('touch', movePlayer) Runtime:addEventListener('enterFrame', update) timerSrc = timer.performWithDelay(1300, createBlock, 0) speedTimer = timer.performWithDelay(5000, increaseSpeed, 5) helicopter:addEventListener('collision', onCollision) else gameBg:addEventListener('touch', movePlayer) Runtime:removeEventListener('enterFrame', update) timer.cancel(timerSrc) timerSrc = nil timer.cancel(speedTimer) speedTimer = nil helicopter:removeEventListener('collision', onCollision) end end function createBlock() local b local rnd = math.floor(math.random() \* 4) + 1 b = display.newImage('block.png', display.contentWidth, yPos[math.floor(math.random() \* 3)+1]) b.name = 'block' -- Block physics physics.addBody(b, 'kinematic') b.isSensor = true blocks:insert(b) end function movePlayer(e) if(e.phase == 'began') then up = true end if(e.phase == 'ended') then up = false impulse = -60 end end function increaseSpeed() speed = speed + 2 -- Icon local icon = display.newImage('speed.png', 204, 124) transition.from(icon, {time = 200, alpha = 0.1, onComplete = function() timer.performWithDelay(500, function() transition.to(icon, {time = 200, alpha = 0.1, onComplete = function() display.remove(icon) icon = nil end}) end) end}) end function update(e) -- Move helicopter up if(up) then impulse = impulse - 3 helicopter:setLinearVelocity(0, impulse) end -- Move Blocks if(blocks ~= nil)then for i = 1, blocks.numChildren do blocks[i].x = blocks[i].x - speed end end -- Score scoreTF.text = tostring(tonumber(scoreTF.text) + 1) end function onCollision(e) audio.play(explo) display.remove(helicopter) alert() end function alert() audio.stop(1) audio.dispose() bgMusic = nil gameListeners('rmv') alertView = display.newImage('alert.png', 170, 136) transition.from(alertView, {time = 300, xScale = 0.5, yScale = 0.5}) -- Wait 100 ms to stop physics timer.performWithDelay(1000, function() physics.stop() end, 1) end Main()

So if you see the last function before the main call. The alert function do some stuff like stop the listeners and show a game over message, then waits 100 ms to stop the physics function (i also try to put the os.exit, it works but you have to start all and launch the game again from the mobile menu.

Thanks for the replys.

Regards.

Just a few things for you to ponder before you go too far in a direction.

1.  If your goal is to have this on the Apple iTunes store at some point, you cannot have your app programaticlally exit.  They will reject your app because of it.  In the iOS world, people see apps that exit as crashes and will lead to bad ratings and reviews.  The Android world is a little more forgiving and will tolerate exiting apps a bit more.  There is an API call native.requestExit() that you should use to actually exit your app and avoid os.exit().  See:  http://docs.coronalabs.com/api/library/native/requestExit.html.

  1. I would recommend spending some time and learning how to use Storyboard if you want to have the ability to restart the game.  We have a tutorial on that:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/  though you probably need to have gone through a few other storyboard based tutorials before attacking that one directly.  

  2. Director is a wonderful module. I’ve used it on several apps, and many people love it for a scene manager, however it is a 3rd party solution and your support has to come from that 3rd party.  It has not been updated yet to work with our Graphics 2.0 engine and you may run into some difficulties with it that you will have to fix.  I think most people have worked around them in some form or fashion.  But if you start with Storyboard and then move to our new scene manager Composer when it becomes available to Starter and Basic subscribers in the next build, you will help future proof your app.

Rob

Thanks Rob, i’m gonna start over and from the beginning to use and learn storyboards.

Regards

Rob - what would you recommend for an app with an initial disclaimer message to ask the user to Accept (continues to the app) or Decline/Exit (in which case the app terminates)?  

Is this problematic on IOS such that it may be better to just have an Accept button with no Decline Button.  So they can either Accept or hit the home button to go do something else then?

You should probably provide an accept and decline button.  It’s not intuitive that hitting the home button is the way out of the app.  I suspect Apple might be okay with the app exiting if you decline the terms of service. 

Just a few things for you to ponder before you go too far in a direction.

1.  If your goal is to have this on the Apple iTunes store at some point, you cannot have your app programaticlally exit.  They will reject your app because of it.  In the iOS world, people see apps that exit as crashes and will lead to bad ratings and reviews.  The Android world is a little more forgiving and will tolerate exiting apps a bit more.  There is an API call native.requestExit() that you should use to actually exit your app and avoid os.exit().  See:  http://docs.coronalabs.com/api/library/native/requestExit.html.

  1. I would recommend spending some time and learning how to use Storyboard if you want to have the ability to restart the game.  We have a tutorial on that:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/  though you probably need to have gone through a few other storyboard based tutorials before attacking that one directly.  

  2. Director is a wonderful module. I’ve used it on several apps, and many people love it for a scene manager, however it is a 3rd party solution and your support has to come from that 3rd party.  It has not been updated yet to work with our Graphics 2.0 engine and you may run into some difficulties with it that you will have to fix.  I think most people have worked around them in some form or fashion.  But if you start with Storyboard and then move to our new scene manager Composer when it becomes available to Starter and Basic subscribers in the next build, you will help future proof your app.

Rob

Thanks Rob, i’m gonna start over and from the beginning to use and learn storyboards.

Regards

Rob - what would you recommend for an app with an initial disclaimer message to ask the user to Accept (continues to the app) or Decline/Exit (in which case the app terminates)?  

Is this problematic on IOS such that it may be better to just have an Accept button with no Decline Button.  So they can either Accept or hit the home button to go do something else then?

You should probably provide an accept and decline button.  It’s not intuitive that hitting the home button is the way out of the app.  I suspect Apple might be okay with the app exiting if you decline the terms of service.