local overlayCreated = false

function onCreatePost()
    -- Make a larger overlay to guarantee full coverage
    makeLuaSprite('blackOverlay', '', getProperty('camGame.scroll.x') - 200, getProperty('camGame.scroll.y') - 200)
    makeGraphic('blackOverlay', screenWidth + 400, screenHeight + 400, '000000') -- extra padding
    setObjectCamera('blackOverlay', 'game')
    setProperty('blackOverlay.alpha', 0)
    addLuaSprite('blackOverlay', true)
    overlayCreated = true
end

function onEvent(name, value1, value2)
    if name == 'BlackOverlay' and overlayCreated then
        if value1 == 'on' then
            doTweenAlpha('overlayFadeIn', 'blackOverlay', 1, 0.5, 'linear')
        elseif value1 == 'off' then
            doTweenAlpha('overlayFadeOut', 'blackOverlay', 0, 0.5, 'linear')
        end
    end
end

function onUpdatePost()
    if overlayCreated then
        -- Keep the oversized overlay centered on camera scroll
        setProperty('blackOverlay.x', getProperty('camGame.scroll.x') - 200)
        setProperty('blackOverlay.y', getProperty('camGame.scroll.y') - 200)
    end
end
