Color Fading
Posted: Sun Mar 09, 2014 4:15 pm
Hey Guys, it's been a long time since I last dwelled here on the forums, but now I've got a question for you
First of all, the code for my class:
It's just for creating a small text flying upwards, for points or pickups and stuff. Now my problem is that the fadeout of the color or in this example the alpha value is waaay too fast. The fading happens in this rows:
I don't quite know what is wrong here. In pseudocode, I take the starting color or alpha value and subtract the difference to the value I want to change it in multiplied by the time that has passed since the start.
Thanks in advance for every help and hint.
Greetings!
First of all, the code for my class:
Code: Select all
------------------
-- // FloatingText \\ --
------------------
local FloatingText = {}
FloatingText.__index = FloatingText
------------------------
-- // Dependencies \\ --
------------------------
------------------------
-- // Declarations \\ --
------------------------
function FloatingText.new(o)
local self = o or {}
self.name = self.name or "FloatingText"
self.x = self.x or 150
self.y = self.y or 50
self.fadeTime = 1
self.velY = self.velY or -120
self.text = self.text or "Chaos Factory"
self.textSize = self.textSize or 20
self.font = self.font or FONT.medium
self.startColor = self.startColor or {r = 255, g = 255, b = 255, a = 255}
self.endColor = self.endColor or {r = 255, g = 255, b = 255, a = 0}
self.visible = self.visible or true
--Do not change!
self.color = self.startColor
self.fadeTimer = 0
self.removed = false
setmetatable(self, FloatingText)
return self
end
function FloatingText:update(dt)
--Despawn timer
self.fadeTimer = self.fadeTimer + dt
--Despawn
if self.fadeTimer > self.fadeTime then self:destroy() end
--Movement
self.y = self.y + self.velY * dt
--Color fading
self.color.r = self.startColor.r - ( (self.startColor.r - self.endColor.r) * self.fadeTimer / self.fadeTime )
self.color.g = self.startColor.g - ( (self.startColor.g - self.endColor.g) * self.fadeTimer / self.fadeTime )
self.color.b = self.startColor.b - ( (self.startColor.b - self.endColor.b) * self.fadeTimer / self.fadeTime )
self.color.a = self.startColor.a - ( (self.startColor.a - self.endColor.a) * self.fadeTimer / self.fadeTime )
end
function FloatingText:destroy()
self.removed = true
end
function FloatingText:draw()
--Previous color settings (backup)
--local r, g, b, a = love.graphics.getColor()
love.graphics.setFont(self.font)
love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.color.a)
love.graphics.printf(self.text, self.x - self.font:getWidth(self.text) / 2, self.y - self.font:getHeight() / 2, self.font:getWidth(self.text), "center")
--love.graphics.setColor(r, g, b, a)
end
return FloatingText
Code: Select all
--Color fading
self.color.r = self.startColor.r - ( (self.startColor.r - self.endColor.r) * self.fadeTimer / self.fadeTime )
self.color.g = self.startColor.g - ( (self.startColor.g - self.endColor.g) * self.fadeTimer / self.fadeTime )
self.color.b = self.startColor.b - ( (self.startColor.b - self.endColor.b) * self.fadeTimer / self.fadeTime )
self.color.a = self.startColor.a - ( (self.startColor.a - self.endColor.a) * self.fadeTimer / self.fadeTime )
Thanks in advance for every help and hint.
Greetings!