2 Replies Latest reply on Sep 1, 2009 7:54 AM by Rothrock

    attachMovieClip issue, playing them after attaching

    reinhat

      Hi,

      I have an issue with the following script. It nicely generates the 10 movieclip stars but it will not play them. What is wrong?
      Also how can I make the for loop execute with timing. Let's say I want to generate the ten stars in half second intervals. how can I add setInterval to this?

       

      _root.createEmptyMovieClip("starRow4Holder",this.getNextHighestDepth());
      starRow4Holder._x = 315;
      starRow4Holder._y = 347;


      var i:Number;

      for (i=0; i<10; i++) {
              starRow4Holder.attachMovie("StarWhiteAnim","row4Star"+i,i,{_x:(i*20)});
              starRow4Holder.row4Star[i].play();
      }

       

      Thanks in advance,

      Attila

        • 1. Re: attachMovieClip issue, playing them after attaching
          Ned Murphy Adobe Community Professional & MVP

          Without the setInterval inclusion, try:

           

          _root.createEmptyMovieClip("starRow4Holder",this.getNextHighestDepth());
          starRow4Holder._x = 315;
          starRow4Holder._y = 347;


          var i:Number;

          for (i=0; i<10; i++) {
                  starRow4Holder.attachMovie("StarWhiteAnim","row4Star"+i,i,{_x:(i*20)});
                  starRow4Holder["row4Star"+i].play();
          }

           

          To implement a setInterval version of this you do not want to use the for loop, mainly because it will execute without any delay.... you cannot force it to slow down.  Instead, just use the setInterval method along with a counter variable to keep track of how many times in has executed so that you can use clearInterval() when sufficient objects have been added.  If you look up setInterval in the help documents, using it is explained with examples.  The function that your setInterval uses would contain the ocde that is inside your current for loop, along with a line to increment the counter (the counter plays the role of the "i" used in the for loop version)

          1 person found this helpful
          • 2. Re: attachMovieClip issue, playing them after attaching
            Rothrock Level 5

            Do you mean you want to add 10 stars every half second or one star every half second repeatedly until you have 10 stars?

             

            In any event here is a neat trick.

             

            attachMovie returns a reference to the movie you have just created. And I don't know about you, but if I have to type something like starRow4Holder["row4Star"+i] more than once I've likely to make a mistake!

             

            var i:Number;

            for (i=0; i<10; i++) {
                    var curStar:MovieClip=starRow4Holder.attachMovie("StarWhiteAnim","row4Star"+i,i,{_x:(i*20)});
                    curStar.play();
            }

             

            You can then use that returned reference to refer to the newly attached clip. Easy! However the clips should already play, unless you have put a stop on their first keyframe?

             

            As Ned said, there is no way to slow down a for loop, it will always execute before the screen is redrawn, so you will need some kind of counter.

             

            And just as a precaution I always recommend that you clear your intervals before you set them.

             

            clearInterval(starIntervalID)

            var starCount:Number=0;

            var numStars:Number=10;

            var starIntervalID:Number=setInterval(makeStar,500);

             

            function makeStar(){

             

            // whatever kind of star making you want to do

             

            if(starCount<numStars){

            starCount++;

            } else {

            clearInterval(starIntervalID);

            }

            }