/** * TypeWriterTextField.as - AS3 * author: Thomas Praxl - macrominds * date: 2009-06-10 * version: 0.1 * ################################# * Note: This is just a quick hack for a limited use-case. * * TypeWriterTextField is meant to be an easy way to produce * typewriter effects on dynamic text-fields. * Just create a dynamic textfield, press F8 (Convert to Symbol), * enable "Export for ActionScript", * as the baseclass, choose TypeWriterTextField (which has to be * in your class-path). * That will "magically" ;) add a Type Writer Effect to it. * * If you want to add behaviour, there are a few hooks, you can use. * Just inherit TypeWriterTextField and override any of the * following functions: * * getTimerDelayMS * to adjust the speed of the typewriter. The value returned * denotes the pause / delay between charachter-typings * in milliseconds * * onTypeEvent * called whenever a character has been typed. * * onFinishedTyping * called when the whole text has been typed. * * */ package { import flash.text.TextField; import flash.display.MovieClip; import flash.utils.Timer; import flash.events.TimerEvent; class TypeWriterTextField extends MovieClip{ var textField:TextField =null; //won't work with html-text var text:String =null; var linkUrl:String = null; var linkTarget:String = null; var currentText:String = null; var timer:Timer; public function TypeWriterTextField(){ super(); for(var i:int =0;i]*href="(.*?)"/i; var targetPattern:RegExp = /]*target="(.*?)"/i; var hrefResult:Object=hrefPattern.exec(htmlText); if(hrefResult!=null){ linkUrl=hrefResult[1]; var targetResult:Object=targetPattern.exec(htmlText); if(targetResult!=null){ linkTarget=targetResult[1]; } } } protected function getTimerDelayMS():Number{ return 50; } protected function onTypeEvent(typedCharacter:String){ } protected function onFinishedTyping(){ } private function addLink(){ if(linkUrl!=null){ var result:String = ""); result+=""; textField.text=""; textField.htmlText=result; } } private function timerHandler(event:TimerEvent){ var startIdx:int = currentText.length; var endIdx:int = currentText.length+1; if(endIdx>text.length){ timer.stop(); addLink(); onFinishedTyping(); return; } var char:String = text.charAt(startIdx); this.currentText+=char; textField.text = this.currentText; onTypeEvent(char); } } }