title: Broken htmlText in Actionscript 3.0 - funky display from incremented display using string.substring
body: <p>This is an issue with textField.htmlText in actionScript 3.0 and the class I'm making for it.</p>
  
  <p>In an attempt not to post my whole class, I've managed to boil down the problem to what seems like a silly and almost impossible-to-overcome issue. It seems that TextFields (specifically, ones created with ActionScript) have an unbearably hard time with a Type-Writing effect created by</p>
  
  <pre><code>myTextField.htmlText =  myString.substr(0, length);
  </code></pre>
  
  <p>where length is an ever-increasing increment. Let's get a more specific example, however. To start, we have some random string on frame 1 of our .fla file:</p>
  
  <pre><code>var Text0:String = 'It wasn\'t like we didn\'t have love. John and I shared much of our love, but for some reason something went terribly wrong one day...\nWe talked less, we saw each other less, we hardly even got to know each other anymore. The love was dried up, and if I stayed I would have withered, and choked on words I could never share...';
  </code></pre>
  
  <p>Using my class, it takes this string and for X keywords applies Y HTML tags around those words:</p>
  
  <pre><code>var textBuildah = new textBuilder(); //Class name, for this example, is textBuilder
  Text0 = textBuildah.htmlModify(Text0, ['love', 'red'], ['we', '~#FCFE12']);
  </code></pre>
  
  <p>This takes Text0 (we're still in the Actions of Frame 1 here) and does this to it:</p>
  
  <pre><code>trace(Text0) // Outputs: It wasn't like &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; didn't have &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt;. John and I shared much of our &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt;, but for some reason something &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt;nt terribly wrong one day... We talked less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; saw each other less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; hardly even got to know each other anymore. The &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt; was dried up, and if I stayed I would have withered, and choked on words I could never share...
  </code></pre>
  
  <p>I'm assuming all those HTML tags are correct. Then, we do this:</p>
  
  <pre><code>textBox.htmlText = Text0 //textBox is a text field that was put on the stage and named "textBox" using the properties window
  </code></pre>
  
  <p>And it displays exactly as I want it (bottom right of this picture) [ i49.tinypic.com/2upts1v.jpg ]. Add http:// to see it.</p>
  
  <p>Then, I erase the line of code above, and try this:</p>
  
  <pre><code>var Length:Number = 0;
  textBox.addEventListener(Event.ENTER_FRAME, writeText);
  
  function writeText(e:Event):void {
      if (Length &lt; Text0.length) {
          Length ++;
          e.currentTarget.htmlText = Text0.substring(0, Length);
          trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
      } else {
          e.currentTarget.htmlText = Text0;
          e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
      }
  }
  </code></pre>
  
  <p>And, it works like just doing textBox.htmlText = Text0;</p>
  
  <p>Then, we try something new. We create a textField using ActionScript, and apply the same Event Listener to it:</p>
  
  <pre><code>var LengthTwo:Number = 0;
  var myTextField = new TextField();
  myTextField.x = 100
  myTextField.y = 100;
  myTextField.width = 200;
  myTextField.height = 150;
  myTextField.wordWrap = true;
  
  addChild(myTextField);
  
  mytextField.addEventListener(Event.ENTER_FRAME, writeText);
  
  function writeTextTwo(e:Event):void {
  if (LengthTwo &lt; Text0.length) {
      LengthTwo++;
      e.currentTarget.htmlText = Text0.substring(0, Length);
      trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
  } else {
      e.currentTarget.htmlText = Text0;
      e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
  }
  }
  </code></pre>
  
  <p>It produces something like you see in the center of this picture. [ i49.tinypic.com/2upts1v.jpg ]. Add http:// to see it. Also, while it types, it changes colors frequently and disorientatingly. I did a few traces, and I realized that the htmlText of the myTextField has extra tags at the beginning of it. Here's a snapshot of one of the traces:</p>
  
  <pre><code>&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="Times Roman" SIZE="12" COLOR="#FF0000" LETTERSPACING="0" KERNING="0"&gt;It wasn&amp;apos;t like &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; didn&amp;apos;t have love. John and I shared much of our love, but for some reason something &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt;nt terribly wrong one day...&lt;/FONT&gt;&lt;/P&gt;&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="Times Roman" SIZE="12" COLOR="#FF0000" LETTERSPACING="0" KERNING="0"&gt;We talked less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; saw each other less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; hardly even got to know each other anymore. The love was dried up, and if I stayed I would have withered, and choked on words I could never share..&lt;/FONT&gt;&lt;/P&gt;
  instance5
  
  &lt;TEXTFORMAT LEADING="2"&gt;&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0"&gt;It wasn&amp;apos;t like &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; didn&amp;apos;t have &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt;. John and I shared much of our &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt;, but for some reason something &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt;nt terribly wrong one day...&lt;/FONT&gt;&lt;/P&gt;&lt;/TEXTFORMAT&gt;&lt;TEXTFORMAT LEADING="2"&gt;&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0"&gt;We talked less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; saw each other less, &lt;FONT COLOR="#FCFE12"&gt;we&lt;/FONT&gt; hardly even got to know each other anymore. The &lt;FONT COLOR="#FF0000"&gt;love&lt;/FONT&gt; was dried up, and if I stayed I would have withered, and choked on words I could never share..&lt;/FONT&gt;&lt;/P&gt;&lt;/TEXTFORMAT&gt;
  textBox
  </code></pre>
  
  <p>I've realized that throughout this process, a Text Field put on the stage and set to Render HTML Tags will keep it's beginning tags</p>
  
  <pre><code>&lt;TEXTFORMAT LEADING="2"&gt;&lt;P ALIGN="LEFT"&gt;&lt;FONT FACE="Calibri" SIZE="10" COLOR="#FFFFFF" LETTERSPACING="0" KERNING="0"&gt;
  </code></pre>
  
  <p>There and simply make changes to the specified pieces inside. However, for a Text Box added VIA Actionscript 3.0, it changes any of the above starter-tags as the writeText function goes on and the subString starts to include more and more HTML tags.</p>
  
  <p>Does anyone have any idea what I'm missing? Is there a property I need to define to get rid of this weird effect?</p>
  
  <p>Also, if the explanation was too long, <a href="http://www.kirupa.com/forum/attachment.php?attachmentid=51671&amp;d=1267493249" rel="nofollow noreferrer">this is a .fla that produces exactly the same problem I've been having</a>. The middle textfield is the problem. The textField in the bottom-right corner is not a problem.</p>
  
  <p>If you can't handle .fla that's made for CS3/4, have this:</p>
  
  <pre><code>var Text0:String = 'It wasn\'t like &lt;font color = "#FCFE12"&gt;we&lt;/font&gt; didn\'t have &lt;font color = "#FF0000"&gt;love&lt;/font&gt;. John and I shared much of our &lt;font color = "#FF0000"&gt;love&lt;/font&gt;, but for some reason something &lt;font color = "#FCFE12"&gt;we&lt;/font&gt;nt terribly wrong one day...\nWe talked less, &lt;font color = "#FCFE12"&gt;we&lt;/font&gt; saw each other less, &lt;font color = "#FCFE12"&gt;we&lt;/font&gt; hardly even got to know each other anymore. The &lt;font color = "#FF0000"&gt;love&lt;/font&gt; was dried up, and if I stayed I would have withered, and choked on words I could never share...';
  var myTextField:TextField = new TextField();
  var Length:Number = 0;
  var LengthTwo:Number = 0;
  myTextField.x = 75;
  myTextField.y = 100;
  myTextField.width = 400;
  myTextField.height = 150;
  myTextField.wordWrap = true;
  myTextField.textColor = 0xFFFFFF;
  addChild(myTextField);
  
  textBox.addEventListener(Event.ENTER_FRAME, writeText);
  myTextField.addEventListener(Event.ENTER_FRAME, writeTextTwo);
  
  function writeText(e:Event):void {
      if (Length &lt; Text0.length) {
          Length ++;
          e.currentTarget.htmlText = Text0.substring(0, Length);
          trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
      } else {
          e.currentTarget.htmlText = Text0;
          e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
      }
  }
  
  function writeTextTwo(e:Event):void {
  if (LengthTwo &lt; Text0.length) {
      LengthTwo++;
      e.currentTarget.htmlText = Text0.substring(0, Length);
      trace(e.currentTarget.htmlText+"\n"+e.currentTarget.name+"\n");
  } else {
      e.currentTarget.htmlText = Text0;
      e.currentTarget.removeEventListener(Event.ENTER_FRAME, writeText);
  }
  }
  </code></pre>
  
  <p>So far, I have tried setting the TextFormat for myTextField right underneath the textField definition. Then I tried setting a specific TextFormat outside of the field. I've tried making the defaultTextFormat a new format, I've tried resetting the style sheet within the writeText function... It's been a little bit of a logistical nightmare for me. I have no idea how to stop the weird color changes in the .fla...</p>
  
  <p>The problem doesn't only happen with color, however. It happens with bold, italics, etc. that you put into the function and do that Type-Writing effect upon. However, if you just set htmlText = x, then it looks fine.</p>
  
  <p>Any ideas?</p>
  
  <p>EDIT: There seems to be a slight problem with implementing the same things using Bold, Italics, Underline, etc. If anyone has any idea on how to do that with the above text, or to create a similar effect using htmltext with bold, italics, etc - answer here!</p>