1 Reply Latest reply on Jul 10, 2006 4:17 AM by blemmo

    Array

    angusyyl
      My Flash Actionscript

      var myText:String = "hello";
      var letterNum:Number = myText.length;
      var startX:Number = 0;
      var startY:Number = 0;
      for (j=0; j<myText.length; ++j) {
      var flyingLetterArray:Array = new Array();
      flyingLetterArray.push("flyingLetter"+j+"_mc");
      flyingLetter_mc.duplicateMovieClip("flyingLetter"+j+"_mc", i);
      flyingLetterArray[j].letter_mc.letter_txt.text = myText.substr(j, 1);
      flyingLetterArray[j]._x = startX+j*100;
      flyingLetterArray[j]._y = startY+j*100;
      trace(flyingLetterArray[j]);
      }

      When I did trace(flyingLetterArray[0]), why didn't the output window show
      flyingLetter0_mc
      flyingLetter0_mc
      flyingLetter0_mc
      flyingLetter0_mc
      flyingLetter0_mc
      ,
      instead it showed
      flyingLetter0_mc
      flyingLetter1_mc
      flyingLetter2_mc
      flyingLetter3_mc
      flyingLetter4_mc

      And when I did trace(flyingLetterArray[j]),the output window showed
      flyingLetter0_mc
      undefined
      undefined
      undefined
      undefined
      shouldn't it be like this?

      flyingLetter0_mc
      flyingLetter1_mc
      flyingLetter2_mc
      flyingLetter3_mc
      flyingLetter4_mc


      It may be a bit confusing, but hope you can get what I meant!
      Many thanks!!!
        • 1. Re: Array
          blemmo Level 1
          You recreate the array in every iteration, so the element you push() in is always at index 0, thus the output.
          Create the array before the for loop, and just push() the items in the loop.

          Btw, for code containing ] [ brackets, it's better to use the attach code option, because the forum uses those brackets as markup ( [ i ] is italic... ).

          hth,
          blemmo