7 Replies Latest reply on Jan 6, 2008 9:00 PM by pow_haaweeese

    Loading Multiple Movie Clips in random order

      I have a flash file that loads random movie clips. The code below is on frame 1. I have a gotoAndPlay at the end of the timeline to start it over so it will cycle through all of the clips instead of playing one. I don't think this way is really the best way to do it...but it's what I have so far. Ideally what I need to do is have it cycle through the movie clips in random order, preferably not starting over until it has gotten through them all, but at least not playing the same one twice in a row. Any suggestions are greatly appreciated.

        • 1. Re: Loading Multiple Movie Clips in random order
          FlashForumName Level 1
          If you want to stick with this method, you can use it but I would extend it a bit. I would keep an original array with all the movies. Then, at the start of the random selection, I would copy the original array into a new array. Next, I would select a random index in the 2nd array, take that value and copy it into a 3rd array and then remove it from the 2nd array.

          This way, you keep randomly selecting an index in the 2nd array and passing it to the 3rd array. Each selection removes that item from the 2nd array so no duplicates are selected. Once the 2nd array is empty, the 3rd array is full of items in a random order and ready to go.

          Might not be the most efficient way of handling it but fits along the method of yours I believe. It shouldn't take too long since your array is not to big.
          • 2. Re: Loading Multiple Movie Clips in random order
            plaidpnts Level 1
            I don't need to stick with this method if there is a better way. I'm just not super familiar with ActionScript yet.

            How do I add and remove content from the array?
            • 3. Re: Loading Multiple Movie Clips in random order
              FlashForumName Level 1
              Look up the array class in your help documents for some syntax but you can just use pop() to remove things off the end of your array.
              • 4. Re: Loading Multiple Movie Clips in random order
                clbeech Level 3
                use splice instead of pop:
                • 6. Re: Loading Multiple Movie Clips in random order
                  plaidpnts Level 1
                  Everyone's suggestions were very helpful. Thank you! This is the code we ended up using. This code was on frame 1 and we forced it to loop by putting a gotoAndPlay(1) at the end of the main timeline. I'm not sure why we needed that but it didn't work without it.

                  • 7. Re: Loading Multiple Movie Clips in random order
                    pow_haaweeese Level 1
                    hi dzedward,

                    question... I've been searching thru the posts...and I think this code you wrote below could do what I need with some small modifications. could you advise?

                    What I'm looking to do is to upon a button click (btn_01) send the play head to a frame label and play a mc.
                    but I'd like to send that play head to another "random" label on the same timeline...I'd maybe have 3-4 different ones...but doing so after pressing the same button....

                    could the "Array.prototype.shuffle = function" also work in sending the playhead...so if I replaced the swf names with my label names...

                    maybe it's changing surgeons in the middle of an operation, and I'm best detailing my problem in full...

                    basically, just wanting to play a different movie clip (random) every time one (the same) button is pressed.

                    if you've any thoughts....
                    Thank you,


                    your code:

                    Array.prototype.shuffle = function() {
                    for (var ivar = this.length-1; ivar>=0; ivar--) {
                    var p = random(ivar+1);
                    var t = this[ivar];
                    this[ivar] = this[p];
                    this[p] = t;

                    //would this (below) also send to frame labels...as well as swfs?

                    var Array_ar:Array = new Array("rc.swf", "sealy.swf", "labcorp.swf", "baldor.swf", "parker.swf", "sae.swf", "philips.swf", "electrolux.swf", "cox.swf", "gojo.swf", "dolby.swf", "3m.swf", "loreal.swf", "icg.swf", "carlon.swf", "ici.swf", "redcross.swf", "rainsoft.swf", "lt.swf", "gentek.swf", "lesco.swf", "br.swf", "tremco.swf", "ra.swf", "novartis.swf", "ss.swf", "jabil.swf", "tcp.swf");
                    index = 0;
                    function load_random() {
                    loadMovie(Array_ar[index++], movieTarget);
                    if (index == Array_ar.length) {