Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Textfade Engine

edited July 2012 in Code Sharing Posts: 5

Here is a good engine to make text fade away (and move). I wrote this while working on my tower defense game (progress is coming along quite well!). It is easily customizable including color, font size, movement direction and speed, and fading speed,etc...

It can also handle multiple texts at the same time (since all of the data is stored in tables). Please note that I could have made this more efficient by storing it as vec4 values instead of vec2 and vec3 values. It would have reduced the number of tables required.

Please note: If you don't want the text to fade but simply disappear then make the 3rd and 4th argument the same. 1 unit of time is simply the speed in which the draw event is executed (60 times per second?).

--Call this in setup()

Textfade:init()

--Call something in this format to create animated text: Textfade:create(string,font size,time until it starts to fade,time until it finishes fading,x orgin,y orgin,horizontal speed,vertical speed,r,g,b)

Textfade:create("20$",22,5,50,WIDTH/2,HEIGHT/2,0,2,246,43,35) --An example.

--Call this in the draw event

Textfade:draw()

--And here is the code for the Textfade class:

Textfade = class()
function Textfade:init()
    text_fade={}
    text_fade_o={}
    text_fade2={}
    text_fade3={}
    text_fade_pos={}
    text_fade_move={}
end

function Textfade:create(string,size,time1,time2,startx,starty,movex,movey,col1,col2,col3) --time till start to fade, time till done
    table.insert(text_fade,vec3(size,time1,time2))
    table.insert(text_fade_o,255) --current opacity
    table.insert(text_fade2,vec3(col1,col2,col3)) --first 3 parts of color
    table.insert(text_fade3,string)
    table.insert(text_fade_pos,vec2(startx,starty))
    table.insert(text_fade_move,vec2(movex,movey))
end

function Textfade:draw()
    offset=0
    for aa=1,#text_fade do
        a= aa-offset
        text_fade[a].y = text_fade[a].y - 1
        text_fade[a].z = text_fade[a].z - 1
        if text_fade[a].y < 1 then text_fade_o[a] = text_fade_o[a] - 255/(text_fade[a].z-text_fade[a].y) end
        -----
        font("MyriadPro-Bold")
        fontSize(text_fade[a].x)
        textMode(CENTER)
        fill(text_fade2[a].x, text_fade2[a].y, text_fade2[a].z, text_fade_o[a])
        text(text_fade3[a],text_fade_pos[a].x,text_fade_pos[a].y)
        -----
        text_fade_pos[a].x = text_fade_pos[a].x + text_fade_move[a].x
        text_fade_pos[a].y = text_fade_pos[a].y + text_fade_move[a].y
        -----
        if text_fade[a].z < 0 then
                table.remove(text_fade,a)
                table.remove(text_fade_o,a)
                table.remove(text_fade2,a)
                table.remove(text_fade3,a)
                table.remove(text_fade_pos,a)
                table.remove(text_fade_move,a)
                offset = offset + 1
        end
    end
end

Comments

  • Posts: 563

    Very nice - thanks for sharing.

  • Thanks! Please note that if you want to clear all of the texts you simply have to call this command:

    Textfade:init()

  • @skullagepk, this discussion is a year old, please look at the date when you post on discussions

  • IgnatzIgnatz Mod
    Posts: 5,396

    @skullagepk - just because you "want" something that isn't in the code, doesn't mean the code has problems. It means you have a problem, that you need to try to solve before demanding someone else "fix" it.

  • Posts: 1,976

    @skullagepk @Ignatz Demanding someone who hasn't been on in over a year to "fix" it...

This discussion has been closed.