In a previous post I asked how to render text letter by letter. Sumo provided a very nice solution with his Typewriter class, which I’m showing here: (Sumo, I replaced the setInterval call with a timer, and added some formatting)
package {
// @usage: Typewriter(s:String, v:Number, t:TextField)
// @param: s: String variable for the complete text
// @param: v: speed in milliseconds, how often the interval is called
// @param: t: textfield for the string (s)
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.*;
public class Typewriter extends MovieClip {
private var sInt:Number;
private var count:int;
private var str:String;
private var speed:Number;
private var _textField:TextField;
private var format:TextFormat;
private var timer:Timer;
public function Typewriter (s:String, v:Number, t:TextField):void {
str = s;
_textField = t;
timer = new Timer(v);
timer.addEventListener(TimerEvent.TIMER, writeIt);
timer.start();
count = 0;
_textField.text = " ";
}
public function writeIt(e:TimerEvent):void {
format = new TextFormat();
format.font = "FFF Alpine";
format.size = 8;
format.bold = false;
format.color = 0x890025;
_textField.width = 200;
_textField.height = 500;
_textField.wordWrap = true;
_textField.text = str.substring(0, count);
_textField.setTextFormat(_format);
count++;
if (count > str.length) {
clearInterval(sInt);
}
}
}
}
Here’s my next problem. After defining my text string t in my .FLA file and passing it to Typewriter thus:
var type = new Typewriter (t.text, 3, t);
… I now want to be able to access t to change formatting (font, bold, etc.). But I cannot get this to work:
t.replaceText(15, 15, "[inserted text]");
What do I need to change to gain access to the text rendered in the Typewriter class?
Thanks!