had to code this for rpg style text. thx love2d forums for answering my question on how to drip text. Try it
-- typed
displayMode(OVERLAY)
function setup()
x = 256
y = 256
rate = 10 -- type 10 characters per second
example = [[TYPEWRITER
x: ]]..x..[[
y: ]]..y..[[
rate: ]]..rate..[[
]]
end
function draw()
textMode(CORNER)
background(0)
fill(255)
fontSize(40)
textWrapWidth(768)
textMode(CORNER)
text('*', x - 80, y)
typewriter(example, rate, x, y, SOUND_HIT)
end
--[[
--Made for Codea, initially for Love2d
--THANKS azhukar for giving me the initial code that i based this on
--CODEA VERSION (love version coming soon)
]]
--[[
Usage.
You can and should call typewriter from the draw function.
What it does is slowly types out text to the screen using text()
--The first parameter is the string to type
--the second parameter controls the speed of typing
--the third and fourth parameters set the drawing location
--the fifth parameter is a string to play a sound when typing a character
]]
local __drip = {} --cache the dripped text
local function dripText(text,charactersPerSecond,startTime,beep,label)
--In Love2d we use love.timer.getTime() instead of ElapsedTime
local c = charactersPerSecond --the rate of speaking
local d = ElapsedTime - startTime --time since the first call to typewriter
local n = math.floor (d * c) --the number of characters typed
local s = __drip[label][1] --the string that is typed
if n > #s then
sound(beep) --play a sound
return text:sub(1, n)
end
return nil
end
--the typewriter function can write text like a typewriter onto your screen
--it's meant to be placed into the draw function
--it takes the following parameters
--The first parameter is the string to type
--the second parameter controls the speed of typing, think of it as characters per second
--the third and fourth parameters set the drawing location
--the fifth parameter is a string for the sound when typing a character
--=========================
--set fill, wrapwidth, etc before you call typewriter
--note that since typewriter uses textAlign(LEFT)
--you should use: textMode( CENTER )
--=========================
--if using LOVE2D here's a quick glossary of codea text() functions
--fill(r,g,b,a) sets the text color;
--wrapWidth(int) causes text to wrap onto the next line;
--textAlign(LEFT|CENTER|RIGHT) sets the alignment of text
--and is generally used with textWrapWidth;
------------------
--textMode( CORNER|CENTER ) sets the origin of text(string,x,y)
----corner: x,y specifies the lower left corner
----center: x,y specifies the center
--sound(string) plays a sound (i.e. sound('Documents:Beep') )
--=========================
function typewriter(s, r, x, y, b)
local StartTime
local label = table.concat({s,r}, ', ')
if not __drip[label] then
StartTime = ElapsedTime
--in love2d use StartTime = love.timer.getTime()
__drip[label] = {[1]='', [2]=StartTime}
elseif s == __drip[label][1] then
textAlign(LEFT)
text(s,x,y)
--in love2d replace text(s,x,y) with love.graphics.print(s,x,y)
--[[not sure what replaces textAlign]]
else
local start = __drip[label][2]
__drip[label][1] = dripText(s, r, start, b, label) or __drip[label][1]
local str = __drip[label][1]
textAlign(LEFT)
text(str,x,y)
end
end