7 Replies Latest reply on Feb 6, 2008 12:20 PM by Rothrock

    Trouble getting movieclips to play at random

    shallowdeep Level 1
      I desperately need help!

      What I want to do:

      I want a different movie clip to play each time the swf file is loaded. And after the movieclip has finished playing, I would like another random movieclip to start.

      What I have:

      I have four layers, a actionscript layer. And three other layers each with one movieclip on. All these layers are on the first frame.

      I have placed this actionscript into the actionscript layer:


      However this isn't working, and is driving me mad. If anyone could help that would be great.
        • 1. Re: Trouble getting movieclips to play at random
          shallowdeep Level 1
          Hi, sorry to bump my own thread but I'm pulling my hair out with this. As it's not working I have attached my latest attempt.

          Any help I'd be so grateful.

          I have four layers all on the first frame, one actionscript layer the other three contain a movieclip each. Could this be the problem?

          Should each movie clip have its own frame?
          • 2. Re: Trouble getting movieclips to play at random
            Rothrock Level 5
            You don't say exactly what isn't working about your example. From how it looks it seems like it should, so exactly what is the problem?

            No, each clip doesn't need to have its own frame. Most often my files only have one frame on the main timeline and then everything else is controlled from there.

            Generally I would have a stop() on the first frame of each of those clips. Then I wouldn't have to worry about stopping some and playing one. If they all are stopped by default you only have to tell the one that you want to play.

            Below is the code I use to shuffle an array. Just put this code on a frame somewhere before you need to have your array randomized. (I usually just put it on Frame 1.)

            Then after you have created your clips array you can shuffle it with.

            clips.shuffle();

            The array will now be a random order. So you can just pull out the elements one at a time to play.

            I'm not sure if that helps because I'm not really sure what is wrong. Post back with more details.
            • 3. Trouble getting movieclips to play at random
              shallowdeep Level 1
              Hi,

              Thank you for replying.

              I will further explain my woes.

              As I have said I have four layers each on the first frame, three have a movieclip each on. The fourth is a actionscript layer. All my actionscript is on this layer, I have a stop(); at the end of each of my movieclips.

              As I have said I want a different movieclip to appear every time the SWF is loaded, and after one movieclip has played I would like another random movieclip to be played, etc.. I want it to be and ongoing process.

              The problem is that when I currently load my SWF it's just the movieclip on the top layer which is played each time.

              I have tried the actionscript you have suggested, thank you by the way. I placed it in the actionscript layer, however unfortunately this didn't work.

              Any further help you or someone else could provide would be greatly appreciated.

              Thank you

              • 4. Re: Trouble getting movieclips to play at random
                Rothrock Level 5
                So what happens if you have only this code on your first frame?
                • 5. Re: Trouble getting movieclips to play at random
                  shallowdeep Level 1
                  Hi, thanks for responding.

                  I place the code you gave me on the first frame of the actionscript layer.

                  The output was shown as:

                  The clips: Paul_mc,Gary_mc,Ama_mc
                  The clips: Paul_mc,Ama_mc,Gary_mc

                  However when the SWF was loaded it still showed the movieclip on the top layer which is Paul_mc.

                  Thank you for your help so far.
                  • 6. Re: Trouble getting movieclips to play at random
                    shallowdeep Level 1
                    I have included a ZIP file of my FLA, rather than me further trying to explain my problem.

                    I would be very grateful if you could have a look.

                    ZIP file

                    Thank you
                    • 7. Re: Trouble getting movieclips to play at random
                      Rothrock Level 5
                      Okay, so the shuffling is working. We haven't done anything about visibility or playing. Next you should make all of the clips invisible and stop them.

                      var home:MovieClip=this;
                      function hideClips(){
                      for(var i=0;i<clips.length;i++){
                      home[clips ]._visible=false;
                      home[clips
                      ].gotoAndStop(1);
                      }
                      }

                      Finally we can do something about playing the clips.

                      var count:Number=0;
                      function playClip(){
                      home[clips[count]]._visible=true;
                      home[clips[count]].play();
                      count++
                      if(count>=clips.length){
                      count=0;
                      clips.shuffle();
                      }
                      }

                      playClips();

                      Of course your going to need something that checks to see when the clip is done playing. The easiest perhaps would be to put a

                      _parent.hideClips();
                      _parent.playClips();

                      on the last keyframe of each of your clips.