6 Replies Latest reply on Oct 13, 2009 4:34 AM by Ned Murphy

    Random image loader help, Please

    richiebeno Level 1

      Hi all,
      I am looking into using a random image loader, to load external image files using the following code:

       

      pic_arr = ["dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5"];

      onLoad = function() {

              ranNum = Math.floor(Math.random()*pic_arr.length);

              randomLoaderMc.loadMovie(pic_arr[ranNum]+".jpg");

      };

       

      Does anyone know how I can do this so It will never loads the same image twice in a row?

       

      Many thanks in advance
      Rich

        • 1. Re: Random image loader help, Please
          Ned Murphy Adobe Community Professional & MVP

          In what way do you mean to not load the same image twice in a row?  That code appears to only load one image, so when would the next occurence of a loading take place?

          1 person found this helpful
          • 2. Re: Random image loader help, Please
            richiebeno Level 1

            The image loads on first frame (randomly) then the timeline plays and loops with the movieClip to then action the same code.

             

            Please see FLA attached, movieClip in question is "dynamicImageLoader"

             

            thanks

            Rich

            • 3. Re: Random image loader help, Please
              richiebeno Level 1

              The code use original wasn't work, so now using this:

               

              pic_arr = ["dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6"];

              ranNum = Math.floor(Math.random()*pic_arr.length);

              myClip = this.createEmptyMovieClip("randomLoaderMc", 100);

              myClip._x = 0;

              myClip._y = 0;

              myClip.loadMovie(pic_arr[ranNum]+".jpg")

               

              But still cant figure out how to make to not load the same image twice in a row!

               

              Thanks in advance for any help.

              rich

              • 4. Re: Random image loader help, Please
                Ned Murphy Adobe Community Professional & MVP

                You should just shuffle the array elements, then you can loop thru them with a counter.  If you search this forum for the term "shuffle" you should be able to find the shuffle function.

                 

                Edit: My own search for the function turned up nothing as code segments are apparently being removed from older forum postings, so here it is...

                 

                function shuffle(a){
                var len = a.length-1;
                for (i=len; i>=0; i--) {
                  var p = Math.floor(Math.random()*(i+1));
                  var t = a[i];
                  a[i] = a[p];
                  a[p] = t;
                }
                return a;
                }

                 

                You would just need to pass your array into the function to have it returned in a random order...

                 

                pic_arr = shuiffle(pic_arr);

                1 person found this helpful
                • 5. Re: Random image loader help, Please
                  richiebeno Level 1

                  I am not an experienced with actionScript, can't get this to work... not sure how to add the code you suggested!

                   

                  this is what i tried:

                   

                   

                  function shuffle(a){

                  var len = a.length-1;

                  for (i=len; i>=0; i--) {

                    var p = Math.floor(Math.random()*(i+1));

                    var t = a[i];

                    a[i] = a[p];

                    a[p] = t;

                  }

                  return a;

                  }

                   

                  pic_arr = shuiffle("dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6");

                   

                  myClip = this.createEmptyMovieClip("randomLoaderMc", 100);

                  myClip._x = 0;

                  myClip._y = 0;

                  myClip.loadMovie(pic_arr[shuffle]+".jpg")

                  • 6. Re: Random image loader help, Please
                    Ned Murphy Adobe Community Professional & MVP

                    // first create the array

                    var pic_arr = new Array("dhi/1", "dhi/2", "dhi/3", "dhi/4", "dhi/5", "dhi/6");

                     

                    function shuffle(a){

                    var len = a.length-1;

                    for (i=len; i>=0; i--) {

                      var p = Math.floor(Math.random()*(i+1));

                      var t = a[i];

                      a[i] = a[p];

                      a[p] = t;

                    }

                    return a;

                    }

                     

                     

                    // then randomize what's in it

                    picArray = shuffle(picArray);