3 Replies Latest reply on Dec 12, 2009 12:08 PM by Ned Murphy

    Images loading from an xml - can I randomize the array in actionscript?

    UT12Hive

      I have a flash file that loads images from an xml file. Is there a way to make the images load randomly? I would like a different image to start every time it loads. Unfortunately I do not know actionscript so any help would be greatly appreciated.

       

      here is where the xml is pulled in...

       

      var myXML2:XML = new XML();
      myXML2.ignoreWhite = true;
      myXML2.load("gallery.xml");
      var thumbPath:Array = Array();
      var picPath:Array = Array();
      var link:Array = Array();
      var desc:Array = Array();
      _root.gapH = 17;
      // horizontal space
      myXML2.onLoad = function(success) {
      displayPage();
      };

       

      here is where the first image is started....

       

      mc_thumbnail.img._visible = false;
      //load the first big image  
      showLargeImage(picPath[0], link[0], desc[0]);
      _root.currentImage = 0;
      _root.currentThumb = 0;
      showClickedThumbnail();
      if (_root.slideShow.toUpperCase() == "ON") {
        playSlide();
      }

       

      }

       

      I've tried a few things to both places by viewing similar examples, but I am just not getting anything to work. Can someone help with the code? what changes do I need to make where? Please...any help would be greatly appreciated.

        • 1. Re: Images loading from an xml - can I randomize the array in actionscript?
          Ned Murphy Adobe Community Professional & MVP

          Here's one approach I think will work.  First create a new function called shuffle().  This will be used to randomize the elements of an array.  The elements of that array will be the indexes relative to the arrays that hold the image information.  So the code will end up sequencing the images ordered in the random order of those iindexes... (yeah, it's a potential brain twister, but hopefully you'll catch on).  I am showing added/revised code in red. and the "...." is just to save space and not show the entire code for the existing displayPage() function.  I don't think any more of that function needs to be revised than what I show, but I could be wrong

           

          // this new function is to randomize the indexes for the images

          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;
          }

           

           

          function displayPage() {
          ....
          ....

           

          _root.numberOfThumbs = _root.numberOfItems;

          // declare an array for the indexes

          var randomIndexes:Array = new Array();

           

          // load it with values 0 thru numberOfItems
          for (var j = 0; j<_root.numberOfItems; j++){

                 randomIndexes.push(j);
          }

           

          // randimize the index array
          randomIndexes = shuffle(randomIndexes);

          for (var i = 0; i<_root.numberOfItems; i++) {
            thumbPath[i] = xml.childNodes[randomIndexes[i]].childNodes[0].childNodes[0].nodeValue;
            picPath[i] = xml.childNodes[randomIndexes[i]].childNodes[1].childNodes[0].nodeValue;
            link[i] = xml.childNodes[randomIndexes[i]].childNodes[2].childNodes[0].nodeValue;
            desc[i] = xml.childNodes[randomIndexes[i]].childNodes[3].childNodes[0].nodeValue;
           
            ....
            ....
          }

          • 2. Re: Images loading from an xml - can I randomize the array in actionscript?
            UT12Hive Level 1

            That worked!!! Thank you sooo much!!! I greatly appreciate the help...and such a fast response too!! Thanks!