I have the following code:
package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
public class AnimateText {
private var _text:String;
private var _holder:Sprite;
private var _textfield:TextField;
private var _fmt:TextFormat;
private var _timer:Timer;
private var _cont:uint = 1;
public function AnimateText() {
_text = "Hello world";
_holder = new Sprite();
TopLevel.stage.addChild(_holder);
_fmt = new TextFormat();
_fmt.font = "Arial";
_fmt.size = 18;
_textfield = new TextField();
_textfield.autoSize = TextFieldAutoSize.LEFT;
_textfield.setTextFormat(_fmt);
_holder.addChild(_textfield);
_timer = new Timer(100, _text.length);
_timer.addEventListener(TimerEvent.TIMER, onTimerTick);
_timer.start();
}
private function onTimerTick(evt:TimerEvent):void {
_textfield.text = _text.substr(0, _cont++);
}
}
}
When publishing, the textfield does not have any textformat. However, if I change the following:
private function onTimerTick(evt:TimerEvent):void {
_textfield.text = _text.substr(0, _cont++);
_textfield.setTextFormat(_fmt);
}
It works fine. Why do I have to apply the TextFormat in every tick of the Timer? It does the same with an ENTER_FRAME event, and I don’t understand it.