PDA

View Full Version : "Typing" XML text



tpann
December 7th, 2007, 04:23 PM
More on the project I've posted about recently...

I've developed a class, TypewriterXML, that loads text from an XML file and IS SUPPOSED TO render the text typewriter-like. Here's TyperwriterXML:


package {
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.*;

public class TypewriterXML extends Sprite {
private var xmlFile:String;
private var xmlData:XML;
private var urlLoader:URLLoader;
private var _speed:Number;
private var _tX:uint;
private var _tY:uint;
private var _tWidth:uint;
private var _tHeight:uint;
private var _primaryFormat:String;
private var _formatMatrix:Array;
private var _textField:TextField;
private var _typeText:String;
private var typeTimer:Timer;
private var count:int;
private var str:String;

private var testTextField:TextField;
private var testString:String;

private var defaultFormat:TextFormat;

public function TypewriterXML(arg_filename:String,
arg_speed:uint,
arg_x:int,
arg_y:int,
arg_width:int,
arg_height:int,
arg_primaryFormat:String,
arg_formatMatrix:Array) {
xmlFile = arg_filename;
_speed = arg_speed;
_tX = arg_x;
_tY = arg_y;
_tWidth = arg_width;
_tHeight = arg_height;
_primaryFormat = arg_primaryFormat;
_formatMatrix = arg_formatMatrix;
var urlRequest:URLRequest = new URLRequest(xmlFile);
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, xmlCompleteListener);
urlLoader.load(urlRequest);
}
private function xmlCompleteListener(e:Event) {
xmlData = new XML(urlLoader.data);
str = xmlData.PAR1.toString();
typewriter(_speed, _tX, _tY, _tWidth, _tHeight,
_primaryFormat, _formatMatrix);
}
private function typewriter(_speed, _tX, _tY, _tWidth, _tHeight,
_primaryFormat, _formatMatrix):void {
_textField = new TextField();
typeTimer = new Timer(_speed);
typeTimer.addEventListener(TimerEvent.TIMER, type);
typeTimer.start();
count = 0;
_textField.text = "";
_textField.x = _tX;
_textField.y = _tY;
_textField.wordWrap = false;
}
private function type(e:TimerEvent) {
_textField.text = str.substring(0, count);
trace("_textField.text = " + _textField.text);
addChild(_textField);
count++;
if (count > str.length) {
typeTimer.stop();
}
}
}
}
Problem: Although the trace in the type() function shows that the text is rendering letter by letter (meaning the class works), I cannot make the text render on the stage. Note that I have addChild(_textField); in the type() function -- I thought this should do it. But it doesn't.

Probably a simple solution? Help please.

Felixz
December 8th, 2007, 10:59 AM
have you added TypewriterXML instance to stage?
and move that addChild to typewriter function because it is enough to add it ONCE

Felixz
December 8th, 2007, 11:53 AM
Ok I made it more optimized and universal:
import flash.events.TimerEvent;
import flash.events.Event;
import flash.utils.Timer;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.text.TextFormat;
class Typewriter extends TextField {
private var _timer:Timer=new Timer(100);
private var _text:String;
public function Typewriter($text:String="",textFormat:TextFormat=null,$speed:uint=100,$width :Number=NaN,$height:Number=NaN) {
autoSize=TextFieldAutoSize.LEFT;
this.speed=$speed;
this.text=$text;
if ($width) {
width=$width;
if ($height){
height=$height;
autoSize=TextFieldAutoSize.NONE;
} else wordWrap=true;
}
if (textFormat) this.defaultTextFormat=textFormat;
this._timer.addEventListener(TimerEvent.TIMER, _type);
if (this._text) addEventListener(Event.ADDED_TO_STAGE,start);
}
override public function set text(string:String):void {
if (this._timer.running) super.text=string else {
this._text=string;
_timer.repeatCount=string.length;
}
}
public function set speed(number:uint):void {
this._timer.delay=number;
}
public function get speed():uint {
return this._timer.delay;
}
public function start(event:Event=null):void {
this._timer.start();
}
public function stop():void {
this._timer.stop();
}
public function reset():void {
this.text="";
this._timer.reset();
}
private function _type(event:TimerEvent) {
text=this._text.substring(0,event.target.currentCo unt);
trace(text)
}
}If you want to add autoDownload just extend it, and if you have some more ideas I'm ready to implement them.