Posted By

Bonky on 10/05/09


Tagged

buttonhtmldynamictext


Versions (?)

overlay button on dynamic text


 / Published in: ActionScript 3
 

URL: http://www.kirupa.com/forum/archive/index.php/t-236621.html

  1. package {
  2. import flash.net.URLLoader
  3. import flash.net.URLRequest
  4. import flash.xml.XML;
  5. import flash.display.*;
  6. import flash.events.*;
  7. import flash.text.*;
  8. import flash.geom.*;
  9.  
  10. /*
  11. requires xml document named "test.xml" that looks like:
  12. <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
  13. <!-- rollover dynamic text in flash 9 -->
  14. <document>
  15. <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>
  16. </document>
  17.  
  18. */
  19. public class DynTextLink extends Sprite {
  20.  
  21. private var _fmt:TextFormat;
  22. private var _xml:XML;
  23. private var _tf:TextField;
  24. private var _s:String;
  25. private var _linkText:String;
  26. private var _output:TextField;
  27. private var _loader:URLLoader;
  28. public function DynTextLink() {
  29. _loader = new URLLoader();
  30. _fmt = new TextFormat("Tahoma", 11, 0x000000);
  31.  
  32. loadXML();
  33. }
  34.  
  35. private function loadXML():void {
  36. _loader.addEventListener(Event.COMPLETE, onXMLloaded);
  37. _loader.load(new URLRequest("test.xml"));
  38. }
  39. private function onXMLloaded(e:Event):void {
  40. _xml = new XML(_loader.data);
  41. _xml.ignoreWhitespace = true;
  42. _s = _xml.string[0].toString();
  43. _linkText = _xml.string[0].@linkText.toString();
  44. createTextFields();
  45. }
  46. private function createTextFields():void {
  47. _tf = new TextField();
  48. _tf.width = 500;
  49. _tf.height = 100;
  50. _tf.selectable = false;
  51. _tf.x = 50;
  52. _tf.y = 50;
  53. _tf.htmlText = _s;
  54. _tf.setTextFormat(_fmt);
  55.  
  56. _output = new TextField();
  57. _output.width = 500;
  58. _output.height = 100;
  59. _output.selectable = false;
  60. _output.x = 50;
  61. _output.y = 150;
  62.  
  63. addChild(_tf);
  64. addChild(_output);
  65.  
  66. createButton();
  67. }
  68. private function createButton() {
  69. var index:Number = _tf.text.indexOf(_linkText);
  70. var r:Rectangle = _tf.getCharBoundaries(index);
  71. var r2:Rectangle = _tf.getCharBoundaries(index + _linkText.length);
  72. var textButton:Sprite = drawSprite(r2.x - r.x, r.height);
  73. textButton.x = r.x + _tf.x;
  74. textButton.y = r.y + _tf.y;
  75. textButton.addEventListener(MouseEvent.ROLL_OVER, onTextButtonRollOver);
  76. textButton.buttonMode = true;
  77. }
  78. private function onTextButtonRollOver(e:MouseEvent):void {
  79. _output.text = "You just triggered a roll over event with dynamic text!";
  80. _output.setTextFormat(_fmt);
  81. }
  82. function drawSprite(w:Number, h:Number):Sprite {
  83. var s:Sprite = new Sprite();
  84. s.graphics.beginFill(0x000000, 0);
  85. s.graphics.drawRect(0, 0, w, h);
  86. addChild(s);
  87. return s;
  88. }
  89. }
  90. }

Report this snippet  

You need to login to post a comment.