Proper way to exit game

A project to port LÖVE to Android handhelds
GiveMeAnthony
Prole
Posts: 7
Joined: Fri Jan 08, 2016 3:51 am

Re: Proper way to exit game

Post by GiveMeAnthony »

Kasperelo wrote:
GiveMeAnthony wrote:
Kasperelo wrote: 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.
You may have multiple ways to exit your game and so the love.quit() callback should handle those. I'm just giving an example. The exiting and focus_switch variables are both boolean values which you can switch to control how the callback works. If you only want one call, then you can reduce it to the following

Code: Select all

function love.focus(in_focus)
    if not in_focus then
        love.event.quit()
    end
end

function love.quit()
    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
So, if the window is not in focus, love.event.quit() is called. This triggers the love.quit() callback which stops all audio with love.audio.stop(). If you don't actually want to quit, then you might simply try this

Code: Select all

function love.focus(in_focus)
    if not in_focus then
        love.audio.stop()
    end
end
Locked

Who is online

Users browsing this forum: No registered users and 1 guest