淡入淡出图像

Fading image in and then out again

所以我正在尝试创建一个在图像中淡入淡出的闪屏 splash1.png,然后在几秒钟后再次淡出。所以我所做的是让屏幕开始时完全是黑色的,然后将颜色的 alpha 更改为 1,然后它应该逐渐变回黑色。但目前它似乎停滞在 fadein 阶段并且没有超越这一点。如果有人对我正在做的事情有任何修复或替代方案,将不胜感激。

function love.load()
    timer = 0
    alpha = 0
    fadein  = 300
    display = 500
    fadeout = 900
    splashScreen = love.graphics.newImage("images/Splash1.png")
end

function love.update(dt)
    timer=timer+dt
    if timer<fadein then alpha=timer/fadein  print("fadein")
    elseif timer<display then alpha=1  print("display")
    elseif timer<fadeout then alpha=1-((timer-display)/(fadeout-display))print("fadeout")

    else alpha=1 end

end

function love.draw()
    love.graphics.setColor(255, 255, 255, alpha*255)
    local sx = love.graphics.getWidth() / splashScreen:getWidth()
    local sy = love.graphics.getHeight() / splashScreen:getHeight()
    love.graphics.draw(splashScreen, 0, 0, 0, sx, sy) -- x: 0, y: 0, rot: 0, scale x and scale y
end

抱歉,我花了这么长时间才回答。以下代码应该可以实现您要查找的内容:

function love.load()
    timer = 0
    alpha = 0
    fadein  = 3
    display = 6
    fadeout = 9
    splashScreen = love.graphics.newImage("image.png")
end

function love.update(dt)
    timer = timer + dt
    if 0 < timer and timer < fadein then 
        alpha = timer / fadein  
    end
    if fadein < timer and timer < display then 
        alpha = 1  
    end
    if display < timer and timer < fadeout then 
        alpha = 1 - ((timer - display) / (fadeout - display))
    end
end

function love.draw()
    love.graphics.setColor(1, 1, 1, alpha)
    local sx = love.graphics.getWidth() / splashScreen:getWidth()
    local sy = love.graphics.getHeight() / splashScreen:getHeight()
    love.graphics.draw(splashScreen, 0, 0, 0, sx, sy)
end

与您的代码的主要区别是:

  • 我将 elseif 语句切换为 if 语句并给出了 timer 检查的上限和下限。您的版本的问题是第一个 if 语句每次都为真,因此该程序甚至不会查看其他语句;

  • 我更改了 fadeindisplayfadeout 的值。如果您使用 dt 作为动画计数器,请注意它的计数速度很慢,对于标准显示器,我给出的示例值应该给您一个很好的起点:

  • 我将您的 setColor 值更改为 [0, 1]。我很确定 255 比例可以工作,但我还没有尝试过。