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!