Text which appears gradually
Posted: Thu Dec 15, 2016 8:46 pm
I can't figure out how to make a text which appears gradually.
Do you have any idea about how to that? o.O
Do you have any idea about how to that? o.O
Code: Select all
string = "This lame example is twice as big."
for i = 1, #string do
local c = string:sub(i, i)
love.graphics.print(c, i * i, 90)
-- pause for half a sec
end
Code: Select all
local str = "This lame example is twice as big."
local time_per_letter = 0.5
local time_passed = 0
local current_letter = 0
function love.update(dt)
time_passed = time_passed + dt
if time_passed >= time_per_letter then
time_passed = 0
current_letter = current_letter + 1
end
end
function love.draw()
local chars = str:sub(1, current_letter)
love.graphics.print(chars, 50, 50)
end
Code: Select all
print: (lines, x, y, width, height, align, hover) =>
if align == "right"
x -= width - @limit
height = 7
chars = @chars
for text in *lines
if chars
if chars < 1
text = ""
else
start = text\sub 1, (utf8.offset(text, math.floor chars + 1) or math.floor chars + 1) - 1
chars -= utf8.len text
text = start\gsub "%%", ""
lg.setColor 255, 255, 255, 255
lg.printf text, x, y, width
y += 7
update: (dt) =>
@chars = math.min @maxchars, @chars + dt * @speed if @chars and @maxchars
This doesn't work for a few reasons, let's me explain so you can learn something from your own code too:xNick1 wrote:My idea doesn't seem to work:
Code: Select all
local text = "This lame example is twice as big."
for i = 1, #text do
local c = text:sub(i, i)
love.graphics.print(c, i * i, 90)
-- pause for half a sec
end
Code: Select all
local text = "This lame example is twice as big."
for i = 1, #text do
local c = text:sub(1, i)
love.graphics.print(c, 10, 90)
-- pause for half a sec
end
Code: Select all
local text = "This lame example is twice as big."
local timer = 0
local i = 0
love.update = function (dt)
timer = timer + dt
print(start) --You'll get an increasing number here, the number of seconds since the first frame
end
love.draw = function ()
local c = text:sub(1, i)
love.graphics.print(c, 10, 90)
end
Code: Select all
local text = "This lame example is twice as big."
local timer = 0
local i = 0
love.update = function (dt)
timer = timer + dt
if timer > 0.5 then --Bigger than half a second
i = i + 1
timer = 0
end
end
love.draw = function ()
local c = text:sub(1, i)
love.graphics.print(c, 10, 90)
end
Code: Select all
local text = "This lame example is twice as big."
local timer = 0
local i = 0
love.update = function (dt)
timer = timer + dt
while timer > 0.5 then
i = i + 1
timer = timer - 0.5
end
end
love.draw = function ()
local c = text:sub(1, i)
love.graphics.print(c, 10, 90)
end
Thank you so much.Sir_Silver wrote:I wrote this example up really hastily, so forgive me if it's buggy - it's definitely not good, - but I think it's accomplishing what you want, which I think it having text appear gradually from left to right. Use what I wrote, if it is what you want, to try to understand how it works and not just to copy it please.Code: Select all
local str = "This lame example is twice as big." local time_per_letter = 0.5 local time_passed = 0 local current_letter = 0 function love.update(dt) time_passed = time_passed + dt if time_passed >= time_per_letter then time_passed = 0 current_letter = current_letter + 1 end end function love.draw() local chars = str:sub(1, current_letter) love.graphics.print(chars, 50, 50) end
Also, I don't recommended naming your strings "string" unless you don't care about using any functions from the string library.
Code: Select all
--text one letter at a time, multicolored
--colors
local red = {1,0,0,1}
local green = {0,1,0,1}
local blue = {0,0,1,1}
local white = {1,1,1,1}
local black = {0,0,0,1}
local yellow = {1,1,0,1}
--
--texts
local textA = "This text is"
local textB = " red "
local textC = "and then white."
local a = "" --substitute with textA
local b = "" --substitute with textB
local c = "" --substitute with textC
--
--variables
local textLength = 32 --number of letters in the string
local timer = 0 --starting time
local delay = 0.05 --text delay, higher number = slower text
local i = 0 --current letter
--
function love.update (dt)
if i < textLength then
timer = timer + dt --deltatime
while timer>0 do
i = i+1
timer = timer-delay
end
end
end
function love.draw (dt)
if i<13 then
a = textA:sub(1,i) --current textA
end
if i>12 then
b = textB:sub(1,i-12) --current textB
end
if i>17 then
c = textC:sub(1,i-17) --current textC
end
--print multicolor text
love.graphics.print ({white,a,red,b,white,c},10,0)
--print information
love.graphics.print ('i='..i..'\nstring length = '..textLength,10,40)
end
An obvious answer is to have the texts in a single array with their colours. You'd have an index to the array and a pointer to the current character in the current index; when you complete one text, you advance the current index. Meanwhile you build another table with the contents that will go into love.graphics.printf (or print).zingo wrote: ↑Fri Jul 07, 2023 4:41 am I know this thread is kind of old, but I didn't want to start a new one being as this is more or less what I was already looking for, except one additional feature: I want to have different parts of the text being printed one letter at a time, in different colors. I was able to achieve this using multiple texts, and some hard-coded timing, but was hoping someone could help me achieve the same result in a more optimized way (if possible, for example, just having one text, etc.)
Code: Select all
--text one letter at a time, multicolored
--colors
local red = {1,0,0,1}
local green = {0,1,0,1}
local blue = {0,0,1,1}
local white = {1,1,1,1}
local black = {0,0,0,1}
local yellow = {1,1,0,1}
--
--text
local text = {'This text is',' red ','and then white.',"","",""}
--
--variables
local textLength = 32 --number of letters in the string
local timer = 0 --starting time
local delay = 0.05 --text delay, higher number = slower text
local i = 0 --current letter (iteration)
--
function love.update (dt)
if i < textLength then
timer = timer + dt --deltatime
while timer>0 do
i = i+1
timer = timer-delay
end
end
end
function love.draw (dt)
if i<13 then
text[4] = text[1]:sub(1,i) --current text[4]
end
if i>12 then
text[5] = text[2]:sub(1,i-12) --current text[5]
end
if i>17 then
text[6] = text[3]:sub(1,i-17) --current text[6]
end
--print multicolor text
love.graphics.print ({white,text[4],red,text[5],white,text[6]},10,0)
--print information
love.graphics.print ('current letter = '..i..'\nstring length = '..textLength,10,40)
end