A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help Please: XML random image gallery - non-repeating

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1

    Help Please: XML random image gallery - non-repeating

    Hi there,

    I'm using Flash CS3 and would like to create an image gallery that randomly loads images listed in an XML file without repeating any of the images until all have been displayed once.

    So far, I've managed to combine a couple of tutorials to create an image gallery that randomly loads images from an XML file, but images often repeat before all images have been displayed once (there are over 2 dozen images). Here's the code:

    Code:
    delay = 3000;
    
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		image = [];
    		description = [];
    		total = xmlNode.childNodes.length;
    		for (i=0; i<total; i++) {
    			image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    			description[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    		}
    		firstImage();
    	} else {
    		content = "file not loaded!";
    	}
    }
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("images.xml");
    p = 0;
    this.onEnterFrame = function() {
    	filesize = picture.getBytesTotal();
    	loaded = picture.getBytesLoaded();
    	preloader._visible = true;
    	if (loaded != filesize) {
    		preloader.preload_bar._xscale = 100*loaded/filesize;
    	} else {
    		preloader._visible = false;
    		if (picture._alpha<100) {
    			picture._alpha += 10;
    		}
    	}
    };
    function nextImage() {
    var ranNum = Math.floor(Math.random()*image.length);
    trace(image[ranNum]);
    	if (p<(total-1)) {
    		p++;
    		if (loaded == filesize) {
    			picture._alpha = 0;
    			picture.loadMovie(image[ranNum],1);
    			slideshow();
    		}
    	}
    }
    function prevImage() {
    var ranNum = Math.floor(Math.random()*image.length);
    trace(image[ranNum]);
    	if (p>0) {
    		p--;
    		picture._alpha = 0;
    		picture.loadMovie(image[ranNum], 1);
    	}
    }
    function firstImage() {
    var ranNum = Math.floor(Math.random()*image.length);
    trace(image[ranNum]);
    	if (loaded == filesize) {
    		picture._alpha = 0;
    		picture.loadMovie(image[ranNum], 1);
    		slideshow();
    	}
    }
    function slideshow() {
    	myInterval = setInterval(pause_slideshow, delay);
    	function pause_slideshow() {
    		clearInterval(myInterval);
    		if (p == (total-1)) {
    			p = 0;
    			firstImage();
    		} else {
    			nextImage();
    		}
    	}
    }
    I've spent days sifting through help forums and tutorials trying to learn how to do this and haven't been successful. Thought I would ask for a hand. Any help is very much appreciated!

    Thank you, kindly!


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    22
    This is my image gallery developed within visual basic .net framework, which maninly contains four image editing functions: image processing application, image converting controls, image annotation add-on and image drawing functionality. Try the free evaluation verison.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    If you want to be sure that all images play at least once before looping again:
    1) you could push all of the URL's from your XML sheet into an array, so that each image is accounted for
    2) then shuffle the array so that they are in a random order
    Code:
    function arrayShuffle(array_arr:Array):Array{
          for(var i:Number = 0; i < array_arr.length; i++){
             var randomNum_num = Math.floor(Math.random() * array_arr.length)
             var arrayIndex = array_arr[i];
             array_arr[i] = array_arr[randomNum_num];
             array_arr[randomNum_num] = arrayIndex;
          }
          return array_arr;
    }
    - See more at: http://learntodevelop.com/2010/09/21/as2-shuffle-array/#sthash.o4z79QAa.dpuf
    3) then call nextImage() using the shuffled array rather than a randomly generated number (which may dupicate before it is time)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center