Archive for February, 2008

23
Feb

actionScript AS2 (typewriter) text effect

Posted By wassim in Flash, Flash Tutorials

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.

Actionscript:
  1. /*
  2. @author = Wassim Sidani
  3. @www.sidani.info
  4. usage example:
  5. var tE:TxtEff=new TxtEff(textFieldInstance, textToWrite, delay);
  6. textFieldInstance= a reference to a text field.
  7. textToWrite= the actual text that will be typed in the text field.
  8. delay = dealy in milliseconds before writing next character;
  9. */
  10. class TxtEff extends TextField
  11. {
  12.     function TxtEff (me, txt, delay)
  13.     {
  14.         //clearInterval(myInt);
  15.         //clearTimeout(myOtherIn);
  16.         myClass=this;
  17.         inite (me, txt, delay);
  18.     }
  19.     function inite (me, txt, delay)
  20.     {
  21.        
  22.         var j : Number = 0;
  23.         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', '{', '|', '}', '~', ' '];
  24.         var initialText : String = txt;
  25.         for (k = 0; k <txt.length; k ++)
  26.         {
  27.             me.replaceText (k, k + 1, '-');
  28.         }
  29.         var myInt : Number = setInterval (go, delay, me);
  30.         function go (me)
  31.         {
  32.             //trace('goo is called and me.text ='+me.text);
  33.             if (j <initialText.length)
  34.             {
  35.                 //trace('j is ' + j+" and chars.length is "+chars.length);
  36.                 for (var i = 0; i <chars.length; i ++)
  37.                 {
  38.                     if (initialText.charAt (j) == chars [i])
  39.                     {
  40.                         me.replaceText (j, j + 1, chars [i]);
  41.                         j ++;
  42.                         break;
  43.                     }
  44.                 }
  45.             } else
  46.             {
  47.                 clearInterval (myInt);
  48.                 trace ("done j=" + j);
  49.                 //myOtherIn=setTimeout(myClass.inite,3000,me,txt,delay);
  50.             }
  51.         }
  52.     }
  53.    
  54. }