Typewriter Text Effect with AS3

May 26th, 2010 in Flash | 20 Comments

Quick & easy code : see how to create a typewriter text effect with Actionscript 3. The text to be displayed will be loaded from a txt file.

View DemoDownload Source

1. Create a new flash file (Actionscript 3.0) and save it as typewriter.fla.

2. Rename “layer 1″ to “actions”. Open the actions panel.
Declare the following variables:

var myText:String;
var counter:int = 0;

3. Create a TextFormat object in order to format our text.

var format : TextFormat = new TextFormat();
format.size = 16;
format.font = "Verdana";
format.bold = true;
format.color = 0xFFFFFF;

4. Create a TextField, set its properties as follow and add it to the display list.

var textField : TextField = new TextField();
textField.width = 600;
textField.height = 350;
textField.selectable = false;
textField.wordWrap = true;
textField.defaultTextFormat = format;
textField.x = textField.y =10;
addChild(textField);

5. Next create an initText function that set the “myText” variable to the string passed as argument. Also add an ENTER_FRAME event listener.

function initText(string:String):void{
	myText = string;
	addEventListener(Event.ENTER_FRAME, writeText);
}

6. The writeText function checks whether the counter variable is inferior or equal to the length of “myText”.
While it’s true, we use the substr method that returns a substring consisting of the characters that start at index 0 and with a length specified by counter. When it becomes false, we remove the ENTER_FRAME listener.

function writeText(e:Event):void{
	if (counter <= myText.length){
   	     textField.text = myText.substr(0,counter);
   	     counter++;
	}
	else{
		removeEventListener(Event.ENTER_FRAME,writeText);
	}
}

7. The text to be display is stored inside a text file. So create your text file, type your own text and save it as text.txt inside the same directory of your fla file. Then load the file. When the load is complete, it simply calls the initText function.

var textLoader:URLLoader = new URLLoader(new URLRequest("text.txt"));
textLoader.addEventListener(Event.COMPLETE, function(e:Event){initText(e.target.data);});

8. Here’s the final code, test your movie to see in action.

var myText:String;
var counter:int = 0; 

var format : TextFormat = new TextFormat();
format.size = 16;
format.font = "Verdana";
format.bold = true;
format.color = 0xFFFFFF; 

var textField : TextField = new TextField();
textField.width = 600;
textField.height = 350;
textField.selectable = false;
textField.wordWrap = true;
textField.defaultTextFormat = format;
textField.x = textField.y =10;
addChild(textField);

var textLoader:URLLoader = new URLLoader(new URLRequest("text.txt"));
textLoader.addEventListener(Event.COMPLETE, function(e:Event){initText(e.target.data);});

function initText(string:String):void{
	myText = string;
	addEventListener(Event.ENTER_FRAME, writeText);
}

function writeText(e:Event):void{
	if (counter <= myText.length){
   	     textField.text = myText.substr(0,counter);
   	     counter++;
	}
	else{
		removeEventListener(Event.ENTER_FRAME,writeText);
	}
}
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(20 Comments)

+ Add a Comment
  1. Thankfully
    May 27th, 2010

    I have a dream that one day on the blue waves of Internet, many thousands of people will be able to concieve and write so fine programs in AS3 thanks to your generosity and imagination and give thanks to you and send you all that you want.
    Thankfully.

  2. Nishu
    May 29th, 2010

    thanks for the tute…

  3. Zbigniew
    May 30th, 2010

    How to slow down the typewriting? Thanks for tut!

  4. chary
    Jun 25th, 2010

    hi – really need help to do this effect using actionscript 2. hope you can help me.

  5. Acharigaru
    Jul 14th, 2010

    Great Job. You ROCK dude.
    How can I make the test being typed faster ? Pls help.

  6. Chris Clemente
    Jul 30th, 2010

    Thank you so much for your tutorials. They are of a tremendous help :)

  7. DK
    Aug 16th, 2010

    Cool code but i have UTF Character,how to show this .swf
    Your action script don’t support UTF Characters.

    Tnx any way cool code.

  8. knalle
    Aug 28th, 2010

    If you want it to type slower you could just lower the frame rate – since a new character is added at every frame (ENTER_FRAME).

    BUT that wouldn’t really be the proper way of controlling it. Instead try using the Timer class

    var myTimer:Timer = new Timer(200); // milliseconds as argument
    myTimer.addEventListener(TimerEvent.TIMER, writeText);
    myTimer.start();

    (remember to change writeText function parameter to TimerEvent argument type – and remove the ENTER_FRAME eventlisteners)

  9. Serdar
    Oct 9th, 2010

    Thank you so much for your fantastic tutorials…
    Regards,

  10. Luis
    Oct 25th, 2010

    Hi there i used this tutorial code to make a text to write itself as it showed here but i hit a lil problem after, how can i edit the animation it self in order to montage it with other animated elements in a picture. its because im making a kind of board that has a few animated items and i would love to use this animation on it. could you please help :) ? this tutorial was very usefull to me

  11. John
    Nov 4th, 2010

    Is there a simple way to use load multiple .txt files and have previous .txt text fade off?

  12. Kelly
    Nov 13th, 2010

    This is great, saved me a lot of time….
    If i want the text to fade at the end and then start typing new text how does that work?
    Thanks so much this helps a lot…
    Kelly

  13. nanana
    Nov 26th, 2010

    how to add typewrite sound effects, like ‘dash’ in each character?

  14. neelima
    Feb 21st, 2011

    thanks,great tut

  15. khen solomon lethil
    Mar 10th, 2011

    have you forgot to test with chrome and IE?

  16. Abelardo
    Mar 29th, 2011

    man you job is very good, but my english is very bad to explain, jajaja, thanks.

  17. Flasheye
    Jun 8th, 2011

    Hi,
    i changed something, maybe someone need this:

    // you need 2 TextFields: textField_txt & textField2_txt

    function TextAnimation(string:String, platz:TextField, speed:Number):void{
    var counter:int = 0;
    var TATimer:Timer = new Timer(speed);
    TATimer.addEventListener(TimerEvent.TIMER, writeText);
    TATimer.start();

    function writeText(e:Event):void{
    if (counter <= string.length){
    platz.text = string.substr(0,counter);
    counter ++;
    }
    else{
    TATimer.removeEventListener(TimerEvent.TIMER,writeText);
    }
    }
    }

    TextAnimation("THIS is a TEST", textField_txt, 20);
    TextAnimation("THIS is a second TEST", textField2_txt, 100);

  18. bob
    Aug 21st, 2011

    thanx for share, thanx for your time, thanx so much..

    best regards,

  19. ruth
    Aug 25th, 2011

    This code is a great help. I was wondering how you align the text but.

    I’ve been able to move the text in with

    textField.x = textField.y =450;

    but if i add to the x it doesn’t work, i.e

    textField.x=100 = textField.y =450;

  20. ruth
    Aug 25th, 2011

    just solved it, my mistake, all that was needed was a ;

    again great code!

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website