Proper way to exit game

A project to port LÖVE to Android handhelds
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Proper way to exit game

Post by Kasperelo » 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?

User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Proper way to exit game

Post by Beelz » Wed Jan 06, 2016 5:07 pm

I don't really dabble in the mobile sector, but I think you're having focus issues.... Think of apps like pandora and youtube. When you return to the home screen pandora will keep playing, however youtube stops when it loses focus. I don't know the specifics, but that's how it seems to me.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website

User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Proper way to exit game

Post by Kasperelo » Wed Jan 06, 2016 6:06 pm

Yeah, my current code looks like this:

Code: Select all

function love.focus(focus)
    if not focus then
        exit()
    end
end
where exit is the code for stopping all sounds and exiting the game. However, that only seems to work on some phones, like my Xperia Z1, but not my friend's Huawei.

GiveMeAnthony
Prole
Posts: 7
Joined: Fri Jan 08, 2016 3:51 am

Re: Proper way to exit game

Post by GiveMeAnthony » Sat Jan 09, 2016 1:24 am

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.

User avatar
slime
Solid Snayke
Posts: 2763
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Proper way to exit game

Post by slime » Sat Jan 09, 2016 1:34 am

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).

User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Proper way to exit game

Post by undef » Sat Jan 09, 2016 6:44 am

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).
Interesting.
What do you do then on iOS? Just remove an option to exit the game?
twitter | steam | indieDB

Check out quadrant on Steam!

User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Proper way to exit game

Post by Kasperelo » Sat Jan 09, 2016 7:01 pm

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.
That is part of my exit() function. It looked a little like this:

Code: Select all

function exit()
    sfx.stop("music")
    -- Some code for stopping all sound effects
    love.event.quit()
end

User avatar
Nixola
Inner party member
Posts: 1924
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Proper way to exit game

Post by Nixola » Sat Jan 09, 2016 8:20 pm

I think love.audio.stop stops all playing sources and is automatically called, so you shouldn't need to do that.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

GiveMeAnthony
Prole
Posts: 7
Joined: Fri Jan 08, 2016 3:51 am

Re: Proper way to exit game

Post by GiveMeAnthony » Sat Jan 09, 2016 9:20 pm

Kasperelo wrote:
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.
That is part of my exit() function. It looked a little like this:

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...

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

User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Proper way to exit game

Post by Kasperelo » Sun Jan 10, 2016 11:16 am

GiveMeAnthony wrote:
Kasperelo wrote:
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.
That is part of my exit() function. It looked a little like this:

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...

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.

Locked

Who is online

Users browsing this forum: No registered users and 2 guests