actionScript AS2 (typewriter) text effect

I needed a typewriter text effect with a twist for an mp3 player I was making, looked around and could not find one that suits my needs so I wrote this class, should do the job.

[as]

/*

@author = Wassim Sidani
@www.sidani.info

usage example:

var tE:TxtEff=new TxtEff(textFieldInstance, textToWrite, delay);

textFieldInstance= a reference to a text field.

textToWrite= the actual text that will be typed in the text field.

delay = dealy in milliseconds before writing next character;

*/
class TxtEff extends TextField
{
function TxtEff (me, txt, delay)
{
//clearInterval(myInt);
//clearTimeout(myOtherIn);
myClass=this;
inite (me, txt, delay);
}
function inite (me, txt, delay)
{

var j : Number = 0;
chars = [‘$’, ‘!’, ‘”‘, ‘#’, ‘$’, ‘%’, ‘&’, “‘”, ‘(‘, ‘)’, ‘*’, ‘+’, ‘,’, ‘-‘, ‘.’, ‘/’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘:’, ‘;’, ‘<', '=', '>‘, ‘?’, ‘@’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’, ‘[‘, ‘\\’, ‘]’, ‘^’, ‘_’, ‘`’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘{‘, ‘|’, ‘}’, ‘~’, ‘ ‘];
var initialText : String = txt;
for (k = 0; k < txt.length; k ++) { me.replaceText (k, k + 1, '-'); } var myInt : Number = setInterval (go, delay, me); function go (me) { //trace('goo is called and me.text ='+me.text); if (j < initialText.length) { //trace('j is ' + j+" and chars.length is "+chars.length); for (var i = 0; i < chars.length; i ++) { if (initialText.charAt (j) == chars [i]) { me.replaceText (j, j + 1, chars [i]); j ++; break; } } } else { clearInterval (myInt); trace ("done j=" + j); //myOtherIn=setTimeout(myClass.inite,3000,me,txt,delay); } } } } [/as]

Leave a Reply