# Typing text animation (dialogue) libGDX or in general

Can anyone think of an efficient way to animate the typing dialogue text as found in pokemon? I have a vague idea of using string.length, and having a new letter drawn every .2 seconds or so, but rather than using a timer event, i was wondering if there is a better way to do this. for example an animation, but i can't find anything like that in libGDX. Any ideas?

If you're accumulating the amount of time that has passed since the dialog box appeared in a variable, then you can divide that by the total length of time you want it to take for all the text to appear. That will give you the percentage of the string should have appeared by now. Multiply that with the string length and round down to find out actually how many characters of the string need to be drawn that frame.

int charCountThisFrame = (int)((elapsedTime / dialogDuration) * text.length);


Alternatively, if you want to use "text speed" instead of a duration (like you see in the option menu of Pokemon and other RPGs), don't use elapsed time. Instead, multiply the speed with the amount of time that has passed since the last game update (sometimes called the time step or delta time) and add that to a variable every update.

float stringCompleteness = 0;
// Then, do this every update
stringCompleteness += textSpeed * deltaTime;
// and this when you draw
int charCountThisFrame = (int)stringCompleteness;
// Don't go over the length of the string!
if (charCountThisFrame > text.length)
{
charCountThisFrame = text.length;
}


Text speed refers to how many characters should appear per unit of time.

• Using delta time! That's a concept I'm familiar with. Thank you, this should work perfectly :) – zyzz Dec 24 '14 at 4:58