Proper way to exit game
Posted: Wed Jan 06, 2016 2:40 pm
When I press the home button on my phone, the background music of my game keeps on playing. What is the proper way to exit the game whenever the user leaves it?
Code: Select all
function love.focus(focus)
if not focus then
exit()
end
end
Interesting.slime wrote:I don't know about Android, but on iOS Apple will probably reject your app from the app store if you programmatically quit it (as that looks like a crash to the user, and apps are never supposed to quit themselves in the iOS app lifecycle).
That is part of my exit() function. It looked a little like this:GiveMeAnthony wrote:Haven't tested stuff on mobile yet; however, I would use love.event.quit() to cause the program to abort and use the love.quit callback for cleaning up resources. Try to use love.event.quit() in the love.focus() callback and see what it does on your friend's Huawei.
Code: Select all
function exit()
sfx.stop("music")
-- Some code for stopping all sound effects
love.event.quit()
end
Alright, so maybe we ought to try something like this...Kasperelo wrote:That is part of my exit() function. It looked a little like this:GiveMeAnthony wrote:Haven't tested stuff on mobile yet; however, I would use love.event.quit() to cause the program to abort and use the love.quit callback for cleaning up resources. Try to use love.event.quit() in the love.focus() callback and see what it does on your friend's Huawei.Code: Select all
function exit() sfx.stop("music") -- Some code for stopping all sound effects love.event.quit() end
Code: Select all
local exiting = false -- we can set this to true from a user dialog
local focus_switch = false -- we'll set this when the app goes out of focus
function love.focus(in_focus)
if not in_focus then
focus_switch = true
love.event.quit()
end
end
function love.quit()
if exiting then
-- code to execute for exiting the program
-- from a user dialog. Perhaps something to
-- to ask if they'd really like to exit or not.
return false
elseif focus_switch then
love.audio.stop()
-- optionally, make multiple calls to love.audio.stop(source)
-- where 'source' is a specific audio source. You can do this
-- to only stop certain audio if you desire.
--------------------------------------------------------------
-- If you must, call sfx.stop("music") and other code instead
-- for stopping all other audio effects.
return false
end
return true
end
I don't really understand your example, sorry. What is the point of exiting and focus_switch exactly? On my friends Huawei, the problem seems to be that the game is still in focus after he has pressed the home button.GiveMeAnthony wrote:Alright, so maybe we ought to try something like this...Kasperelo wrote:That is part of my exit() function. It looked a little like this:GiveMeAnthony wrote:Haven't tested stuff on mobile yet; however, I would use love.event.quit() to cause the program to abort and use the love.quit callback for cleaning up resources. Try to use love.event.quit() in the love.focus() callback and see what it does on your friend's Huawei.Code: Select all
function exit() sfx.stop("music") -- Some code for stopping all sound effects love.event.quit() end
Code: Select all
local exiting = false -- we can set this to true from a user dialog local focus_switch = false -- we'll set this when the app goes out of focus function love.focus(in_focus) if not in_focus then focus_switch = true love.event.quit() end end function love.quit() if exiting then -- code to execute for exiting the program -- from a user dialog. Perhaps something to -- to ask if they'd really like to exit or not. return false elseif focus_switch then love.audio.stop() -- optionally, make multiple calls to love.audio.stop(source) -- where 'source' is a specific audio source. You can do this -- to only stop certain audio if you desire. -------------------------------------------------------------- -- If you must, call sfx.stop("music") and other code instead -- for stopping all other audio effects. return false end return true end