1 Reply Latest reply on Mar 24, 2011 8:49 AM by prip14

    trouble using setInterval method

    joeff2

      Hello all,

       

      I am trying to write a class that will take a string (provided by a different class) and display it gradually, character by character, as if it were being written by a typewriter.  To do this I am making use of the setInterval method in flash.utils, but I'm having difficulties... whenever I test the movie there is no text displayed, but flash isn't reporting any errors.  Here is the code:

       

      package  {
          import flash.display.MovieClip;
          import flash.text.TextField;
          import flash.utils.*;
         
          public class TypeWriter extends MovieClip {
              private var myTextField2:TextField = new TextField();

              private var myTextField3:TextField = new TextField();
              var i:int = 0;
              var intervalId:uint;
             
             
              public function TypeWriter() {

                  myTextField3.y = 50;
                  myTextField3.x = 0;
                  myTextField3.width = 5;
                  myTextField2.y = 50;
                  myTextField2.x = 0;

                  myTextField2.width = 5;
              }

       

           public function writeCharsSlowly(){
                  var textToWrite:String = arguments[0];
                  var displayedChar:String = textToWrite.substring(i,i+1);
                  myTextField2.text = displayedChar;
                  addChild(myTextField2);
                  myTextField2.x += 5;

                  myTextField3.text = "" + myTextField3.text + displayedChar;  //stores and concatenates the characters as they come
                  addChild(myTextField3);
                  myTextField3.width += 5;
                  i++;
                  if (i == textToWrite.length) {
                      clearInterval(intervalId);
                  }
              }

       

           public function playAtIntervals(theText:String) {   //calls function writeCharsSlowly at regular intervals of 1 second...
                   i = 0;
                  intervalId = setInterval(writeCharsSlowly, 1000, theText);
              }

       

      I call the function in the main class thusly:

       

      public function testTypeWriter(){
                  var typeW:TypeWriter = new TypeWriter();
                  typeW.playAtIntervals("Hello brave Adventurer");
              }

      but nothing is displayed... any ideas?

       

      Thanks!