Page 1 of 1

Fading error

Posted: Thu May 08, 2014 9:02 pm
by TheOddByte
Hello, I'm not so familiar with Löve and I'm not sure what I'm doing wrong here..
I'll explain what's wrong, when I try fading an image it fades as it should, but for example when it's completly faded out it reappears for about a millisecond or something and then disappears, it's the same when fading it in but it disappears instead and then reappears..
Here's the code I'm using

Code: Select all

function love.load()
    --# Fading variables
    fade       = false;
    fade_mode  = nil;
    fade_time  = 5
    fade_timer = 5
	
    --# Load test image
    image = love.graphics.newImage("logo.png")
end

function love.update(dt)

  --# Handle fading
	if fade then
	
	    --# Fade out
	    if fade_mode == "out" then
			if fade_timer > 0 then	
				fade_timer = fade_timer - dt
			else
			    if fade_timer < 0 then
				    fade_timer = 0
				end
			    fade = false;
			end
		else
		    --# Fade in
			if fade_timer < fade_time then
				fade_timer = fade_timer + dt
			else
			    if fade_timer > fade_time then
				    fade_timer = fade_time
				end
				fade = false;
			end
		end
		
	end
	
end



function fadeOut( rate )
    fade       = true;
    fade_mode  = "out";
    fade_timer = rate;
    fade_time  = rate;
end

function fadeIn( rate )
    fade       = true;
    fade_mode  = "in";
	fade_time  = rate;
end


function love.draw()
  love.graphics.setColor(255, 255, 255, fade_timer*(255/fade_time))
  love.graphics.draw(image, 100, 100)
end

function love.keypressed(key, unicode)
    if key == "left" then
		if fade_mode == "out" then
			fadeIn(2)
		else
			fadeOut(2)
		end
	end
end
Note: My indenting of the code may get screwed up when pasting it here :P

Re: Fading error

Posted: Thu May 08, 2014 9:29 pm
by DaedalusYoung
Try clamping the alpha value before calling setColor, fade_timer*(255/fade_time) might create odd rounding results.

Code: Select all

local alpha = math.min(255, math.max(0, fade_timer*(255/fade_time)))
love.graphics.setColor(255, 255, 255, alpha)

Re: Fading error

Posted: Thu May 08, 2014 9:33 pm
by TheOddByte
DaedalusYoung wrote: -Snip-
Thanks! It worked as a charm! :D
I'm very grateful, Here's a virtual cookie for you :nyu:
* hands over cookie *

Re: Fading error

Posted: Fri May 09, 2014 2:28 pm
by DaedalusYoung
Thank you and you're very welcome, I'm glad you got it working :)

Another question

Posted: Fri May 09, 2014 7:36 pm
by TheOddByte
Even though this problem have been solved I have another question, But I don't want to start another topic for it since it's probably something simple. I'm wondering how I can invert a picture like this

Code: Select all

--# This is a "picture"
+--------+
| :-)    |
|        |
+--------+

--# That I want to become like this
+--------+
|    (-: |
|        |
+--------+

Re: Fading error

Posted: Fri May 09, 2014 8:37 pm
by micha
The love.graphics.draw function takes a whole lot of input parameters:

Code: Select all

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
sx and sy are scale factors in x- and y-direction. To flip an image left to right, set sx to -1:

Code: Select all

love.graphics.draw( image, x, y, 0, -1, 1)
(you don't need to hand over all of the parameters. If you omit some, they use some default values.

Re: Fading error

Posted: Fri May 09, 2014 8:58 pm
by TheOddByte
Oh thanks! :nyu:
Where exactly is stuff like this documented? I viewed the wiki and didn't find out that you could do -1.

Re: Fading error

Posted: Fri May 09, 2014 9:56 pm
by micha
I am not aware of one specific location, where stuff like this is documented. The wiki is very good in answer the question "What can a specific function do". However, to answer the question "What function do I need to achieve a certain thing", the wiki cannot help you directly. The usual approach here is to
  • Learn more and more over time and get a feeling for this
  • Ask people from the community, like you just did
  • Browse the wiki and read stuff, even if you are not looking for something specific. That way you more and more get an overview of what is possible. This is not the first thing you would do, when you learn LÖVE, because at that time you have no clue, what you really need.
  • In the beginning of course, it is most convenient to read tutorials and try to learn from code others write.

Re: Fading error

Posted: Sat May 10, 2014 9:41 am
by TheOddByte
micha wrote:I am not aware of one specific location, where stuff like this is documented. The wiki is very good in answer the question "What can a specific function do". However, to answer the question "What function do I need to achieve a certain thing", the wiki cannot help you directly. The usual approach here is to
  • Learn more and more over time and get a feeling for this
  • Ask people from the community, like you just did
  • Browse the wiki and read stuff, even if you are not looking for something specific. That way you more and more get an overview of what is possible. This is not the first thing you would do, when you learn LÖVE, because at that time you have no clue, what you really need.
  • In the beginning of course, it is most convenient to read tutorials and try to learn from code others write.
Well I will mess around with LÖVE until I'm used to it, I feel like I've browsed the wiki one million times, but I might as well do it one million times more xP
Thanks for the help everyone! :nyu: