6 Replies Latest reply on Jul 5, 2010 3:20 PM by kglad

    random images in flash

    Aron61523

      does anyone know how to make a random image viewer in flash from scratch
      as in click a button then it will take you to a picture then click it again and it will take you to a different one
      so the images dont come up twice, or does anyone know any tutorials with the same sorta thing

        • 1. Re: random images in flash
          kglad Adobe Community Professional & MVP

          will the images be loaded or in your swf?

          • 2. Re: random images in flash
            Aron61523 Level 1

            well i was thinking of keeping it in the swf , depends on which is easier. do you know how to do it? i need a simple tutorial or quick notes on how to do this... thanks in advance

            • 3. Re: random images in flash
              kglad Adobe Community Professional & MVP

              create an array with the frame labels (or numbers) of frames that contain your images, eg frameA.  you can then use:

               

              var frameA = [define your array];

              frameA = shuffle(frameA);

              var index:Number=0;

              // call playF() when you want to display that next image

               

               

              ///////////// nothing below needs to be changed //////////////////

              function playF(){

              gotoAndStop(frameA[index]);

              index = (index+1)%frameA.length;

              }


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

              • 4. Re: random images in flash
                Aron61523 Level 1

                sorry . i am abit dyslexic

                could you please explain more about it

                • 5. Re: random images in flash
                  Kwangle Level 1

                  This might help - I had to go through this quite carefully before I understood it - the ab

                  stract variable names make it much more difficult to understand. here is my version with more obvious var names:

                   

                  //creates original array with numbers you want to randomise
                  var orgArr:Array = [1,2,3,4,5];
                  trace("original numbers = "+orgArr);


                  //The function that shuffles the numbers. the argument passed (in parenthesis) is the array of ascending numbers (orgArr) it is named 'inArr' for use by the function, but it is exactly the same array
                  function shuffle(inArr:Array) {

                  //creates numeric variable that is the length of the original array
                      var arrLen:Number = inArr.length-1;

                  //a loop that runs through each element in the array starting at the highest position down to zero
                      for (var i:Number = arrLen; i>=0; i--) {
                          //generates random integer based on length of array
                          var rndNum:Number = Math.floor(Math.random()*(i+1));
                          //gets data from array corresponding to loop number (i)
                          var arrData = inArr[i];
                          //swaps array slot i with data from random position in the same array
                          inArr[i] = inArr[rndNum];
                          //swaps random array slot with data from array slot i
                          inArr[rndNum] = arrData;
                      }
                  }
                  shuffle(orgArr);
                  trace("shuffled numbers = "+orgArr);

                   

                  The function cycles through the original array and swaps each array element with another element randomly chosen from the same array. If you run this code the output window will show the original numbers and then the array after it has been shuffled. The numbers could be frame numbers of your pics or filenames for externally loaded content.

                   

                  Thanks for that code Kglad - a neat solution and much simpler that the code I wrote to do the same thing.

                  • 6. Re: random images in flash
                    kglad Adobe Community Professional & MVP

                    you're welcome.

                     

                    (but the shuffle algorithm is well-known and evolved from mathematicians fisher-yates to durstenfeld.)