[Newb Question] Fading images in/out?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
VladiMatt
Prole
Posts: 4
Joined: Sun Feb 17, 2013 9:19 pm

[Newb Question] Fading images in/out?

Post by VladiMatt »

Hi there, I'm trying to write a couple functions that lets me fade images in and out, but I can't get it to work.

Code: Select all

function fadeGraphicIn(graphic, fadetime, _x, _y, scale)
	local fade = fadetime
	local alphachange = 255 / fadetime * 0.1
	local alpha = 0
	while fade > 0 do
		love.timer.sleep( 0.1 )
		fade = fadetime - 0.1
		love.graphics.setColor(255, 255, 255, alpha )
		love.graphics.draw(graphic, _x, _y)
		alpha = alpha + alphachange
	end
end
This function is called by love.draw() as the game starts to fade in a .PNG image that says "Made with LOVE", but the game completely locks up. If anyone knows what I'm doing wrong, I'd really appreciate any help they could offer. Once I've got this function working I'll be able to make the fadeout function easily.

Thanks in advance!
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: [Newb Question] Fading images in/out?

Post by adnzzzzZ »

The game locks up because you're telling it to sleep for 0.1s every frame (multiple times, since it's also inside a while loop). love.draw is called every frame, so your function is getting called every frame and your function, on top of sleeping everything, is drawing the image multiple times per frame and doing the whole fading logic in one frame, not along the fadetime. You're also forgetting to use setColorMode so that the current color affects your drawn images.

To get fading (out) working properly I'd do something like this:

Code: Select all

function love.load()
  fade_time = 5
  fade_timer = 5
  love.graphics.setColorMode('modulate')
  image = love.graphics.newImage()
end

function love.update(dt)
  if fade_timer > 0 then fade_timer = fade_timer - dt end
end

function love.draw()
  love.graphics.setColor(255, 255, 255, fade_timer*(255/fade_time))
  love.graphics.draw(image, )
end
VladiMatt
Prole
Posts: 4
Joined: Sun Feb 17, 2013 9:19 pm

Re: [Newb Question] Fading images in/out?

Post by VladiMatt »

While your example didn't work the way I needed it to, I fixed what you pointed out (I've gotta say I'm a bit embarrassed I didn't realize it from the start, haha!) and got it working (for the most part)! Thanks a billion! :)
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: [Newb Question] Fading images in/out?

Post by Zeliarden »

The game locks up because you're telling it to sleep for 0.1s every frame (multiple times, since it's also inside a while loop).
The game locks up because of an infite while loop

Code: Select all

      fade = fadetime - 0.1 
should be

Code: Select all

      fade = fade - 0.1 
for the while loop to go through
You're also forgetting to use setColorMode so that the current color affects your drawn images.
setColorMode is set to 'modulate' by default so its not needed to be set
DarthGrover
Prole
Posts: 16
Joined: Wed Feb 05, 2014 11:31 pm
Location: Ohio USA

Re: [Newb Question] Fading images in/out?

Post by DarthGrover »

how can fading be done in 0.9.0? since it seems setColorMode is gone.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [Newb Question] Fading images in/out?

Post by Ranguna259 »

Use love.graphics.setColor() before the image and decrease the alpha overtime
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests