## [Solved]Fading to Black and Back

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

### [Solved]Fading to Black and Back

So HUMP's gamestate.lua works wonderfully for me. I would like to establish a fade to black transition for exiting a gamestate and a fade from black to clear when entering a gamestate. I found this snippet of code in another part of the forums. It fades between clear and black in one continuous cycle.

How can I pause the cycle between black, clear, and black again to stay at the clear?

Otherwise, if someone could explain the tween.lua library's fade to me, I would appreciate it. I originally chose the code below because I had difficulty implementing tween.lua's fade. Thanks!

Code: Select all

    function love.load()
timer = 0
alpha = 0

display = 7

image = love.graphics.newImage("background01.png")
end
function love.update(dt)
timer=timer+dt

elseif timer<display then alpha=1
else alpha=0 end

end
function love.draw()
love.graphics.setColor(255, 255, 255, alpha*255)
love.graphics.draw(image, 0, 0)
end
Attachments
Last edited by shatterblast on Sat Oct 18, 2014 1:07 am, edited 1 time in total.

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

### Re: Fading to Black and Back

You could stop the timer once the fade in is complete, and then count the timer back to zero as soon as you want to fade out.

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### Re: Fading to Black and Back

I personally would set a few variables for the fade: alpha, timer, maxtimerIn and maxtimerOut (just so you can adjust easily) and nextstate.
Then, whenever I needed a new gamestate, I'd name it on the "nextstate" variable. Then set the timer and the alpha to work together. When the timer reached 0, I'd change the gamestate to the "nextstate" value and reset the timer. Something more or less like this:

Code: Select all

function love.load()
alpha = 0
timer = 0
maxtimerIn = 1
maxtimerOut = 2 --just in case you need two different fading times
nextstate = false
end

function love.update(dt)
timer = timer + dt
if timer >= maxtimerIn then
timer = maxtimerOut
changeGameState(nextstate) --I don't know what function you call on HUMP, so this is placeholder
nextstate = false
end

alpha = timer/maxtimerIn*255
elseif timer > 0 then --fade out
timer = timer - dt
if timer <= 0 then
timer = 0
end

alpha = timer/maxtimerOut*255
end
end

function love.draw()

--Place this at the very end
love.graphics.setColor(0, 0, 0, alpha)
love.graphics.rectangle("fill", 0, 0, windowWidth, windowHeight)
end

function switchState(gamestate) --Call this when you want to switch gamestate and the fade to happen
nextstate = gamestate
end

Should be pretty easy to understand and reproduce, hopefully
HugoBDesigner - my Blog

shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

### Re: Fading to Black and Back

Thank you very much. I will look this over.

shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

### Re: Fading to Black and Back

Well, I found one solution. Press 2 to fade to black. Press 1 to return to the image from black. It will work even if the screen is black. Edit "fade_time" to change how long it takes to fade in either direction.

Code: Select all

function love.load()

timer = 0
alpha = 0

image = love.graphics.newImage("background01.png")

end

function love.update(dt)

timer=timer+dt
if timer > 4 then
end
end

timer=timer-dt
if timer < 0 then
timer = 0
end
end

end

end

function love.draw()
love.graphics.setColor(255, 255, 255, alpha*255)
love.graphics.draw(image, 0, 0)

love.graphics.print("Press 1 to fade from black to clear.  Press 2 to fade from clear to black.", 50, 50)
end

function love.keypressed(key)
if key == "1" then
elseif key == "2" then
end
end
Attachments

Chroteus
Citizen
Posts: 87
Joined: Wed Mar 20, 2013 7:30 pm

### Re: [Solved]Fading to Black and Back

Or, you can use Venus, which is exactly what you're looking for: Smooth transitions between gamestates (including fade)

shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

### Re: [Solved]Fading to Black and Back

Chroteus wrote:Or, you can use Venus, which is exactly what you're looking for: Smooth transitions between gamestates (including fade)
Maybe it's my lack of experience, but I had trouble starting graphics in the INIT portion of a gamestate in Venus. It worked fine when the graphics files were kept global, but that defeated the purpose of me using gamestates in the first place. With HUMP, putting the graphics files in ENTER seemed to work just fine.

I did like Venus when I tried it, but I couldn't get it to work like I wanted.

Chroteus
Citizen
Posts: 87
Joined: Wed Mar 20, 2013 7:30 pm

### Re: [Solved]Fading to Black and Back

Hmm, as long as you initialize everything in init, everything will load fine. The reason putting to enter doesn't work is because of the following order:

INIT -> Transition state -> ENTER & state you've switched to ("State" from now on)

Since in transition state, State's draw function is called, any image drawn in State must be loaded in INIT.
I believe I warned about loading everything in init (and resetting data, if needed, in enter) in documentation.

### Who is online

Users browsing this forum: No registered users and 28 guests