/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package { import flash.net.URLLoader import flash.net.URLRequest import flash.xml.XML; import flash.display.*; import flash.events.*; import flash.text.*; import flash.geom.*; /* requires xml document named "test.xml" that looks like: <?xml version='1.0' encoding='UTF-8' standalone='yes'?> <!-- rollover dynamic text in flash 9 --> <document> <string linkText = "these words"><![CDATA[Flash 9 is freakin' hot! Just roll over <b><u>these words</u></b> to see another reason why...]]></string> </document> */ public class DynTextLink extends Sprite { private var _fmt:TextFormat; private var _xml:XML; private var _tf:TextField; private var _s:String; private var _linkText:String; private var _output:TextField; private var _loader:URLLoader; public function DynTextLink() { _loader = new URLLoader(); _fmt = new TextFormat("Tahoma", 11, 0x000000); loadXML(); } private function loadXML():void { _loader.addEventListener(Event.COMPLETE, onXMLloaded); _loader.load(new URLRequest("test.xml")); } private function onXMLloaded(e:Event):void { _xml = new XML(_loader.data); _xml.ignoreWhitespace = true; _s = _xml.string[0].toString(); _linkText = _xml.string[0].@linkText.toString(); createTextFields(); } private function createTextFields():void { _tf = new TextField(); _tf.width = 500; _tf.height = 100; _tf.selectable = false; _tf.x = 50; _tf.y = 50; _tf.htmlText = _s; _tf.setTextFormat(_fmt); _output = new TextField(); _output.width = 500; _output.height = 100; _output.selectable = false; _output.x = 50; _output.y = 150; addChild(_tf); addChild(_output); createButton(); } private function createButton() { var index:Number = _tf.text.indexOf(_linkText); var r:Rectangle = _tf.getCharBoundaries(index); var r2:Rectangle = _tf.getCharBoundaries(index + _linkText.length); var textButton:Sprite = drawSprite(r2.x - r.x, r.height); textButton.x = r.x + _tf.x; textButton.y = r.y + _tf.y; textButton.addEventListener(MouseEvent.ROLL_OVER, onTextButtonRollOver); textButton.buttonMode = true; } private function onTextButtonRollOver(e:MouseEvent):void { _output.text = "You just triggered a roll over event with dynamic text!"; _output.setTextFormat(_fmt); } function drawSprite(w:Number, h:Number):Sprite { var s:Sprite = new Sprite(); s.graphics.beginFill(0x000000, 0); s.graphics.drawRect(0, 0, w, h); addChild(s); return s; } } }
URL: http://www.kirupa.com/forum/archive/index.php/t-236621.html