Fading error

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
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Fading error

Post 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
What's the object-oriented way to get wealthy? Inheritance.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Fading error

Post 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)
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Fading error

Post 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 *
What's the object-oriented way to get wealthy? Inheritance.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: Fading error

Post by DaedalusYoung »

Thank you and you're very welcome, I'm glad you got it working :)
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Another question

Post 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
+--------+
|    (-: |
|        |
+--------+
What's the object-oriented way to get wealthy? Inheritance.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Fading error

Post 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.
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Fading error

Post 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.
What's the object-oriented way to get wealthy? Inheritance.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Fading error

Post 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.
User avatar
TheOddByte
Prole
Posts: 35
Joined: Sun Dec 15, 2013 7:15 pm

Re: Fading error

Post 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:
What's the object-oriented way to get wealthy? Inheritance.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests