dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: loop xml gallery

  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    40

    loop xml gallery

    Small problem. I am using this flash/xml slideshow found on Kirupa: http://www.kirupa.com/developer/mx20...togallery3.htm

    And I just need to know how to make it loop continiously, for example you click, next, next, next, next and when it hits 7/7 I want the next button to go to 1/7.

    I will be adding more than 7 images, and will want to add more in the future. So I pretty much need a bit of code extended to it that says "if there's no next image in line, return to image 1".

  2. #2
    Senior Member
    Join Date
    May 2008
    Posts
    332
    Here’s something from a AS2 slide show that you might be able to adapt:

    When you are creating the arrays, also include a variable called currentIndex to track which element of the array is being displayed:
    Code:
    var currentIndex:Number = 0;
    Then tie your next and previous buttons to that currentIndex number:
    Code:
    function nextPicture() {
    	if (urls[currentIndex + 1]) {
    		currentIndex++;
    	}
    	else
    	{
    		currentIndex = 0;
    	}
    	hie_ss_mc.holder_mc.loadMovie(urls[currentIndex]);
    	hie_ss_mc.caption_txt.text = captions[currentIndex];
    }
    
    function prevPicture() {
    	if (currentIndex == 0) {
    		currentIndex = urls.length - 1;
    	}
    	else
    	{
    		currentIndex--;
    	}
    	hie_ss_mc.holder_mc.loadMovie(urls[currentIndex]);
    	hie_ss_mc.caption_txt.text = captions[currentIndex];
    }
    
    hie_ss_mc.next_btn.onPress = nextPicture;
    hie_ss_mc.prev_btn.onPress = prevPicture;
    Next button increments the currentIndex by 1 for as many times as there are elements in the array, else it sets the currentIndex to 0, the first image.
    The previous button checks to see if the index is already 0, the first image. If it is 0, then it uses the length of the urls array and subtracts 1 (because the first element in url array is 0). So 7 pictures, the last element is [6]. else it just increments by -1.
    Hopefully you can get the gist of it and gleen enough code to make it work.
    Best wishes,
    Eye for Viedo
    www.cidigitalmedia.com

  3. #3
    Member
    Join Date
    Aug 2008
    Posts
    40
    That's a little beyond my current knowledge of Actionscript. Here's the current code I'm using, how would I tie it in here?

    Code:
    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");
    /////////////////////////////////////
    listen = new Object();
    listen.onKeyDown = function() {
    	if (Key.getCode() == Key.LEFT) {
    		prevImage();
    	} else if (Key.getCode() == Key.RIGHT) {
    		nextImage();
    	}
    };
    Key.addListener(listen);
    previous_btn.onRelease = function() {
    	prevImage();
    };
    next_btn.onRelease = function() {
    	nextImage();
    };
    /////////////////////////////////////
    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() {
    	if (p<(total-1)) {
    		p++;
    		if (loaded == filesize) {
    			picture._alpha = 0;
    			picture.loadMovie(image[p], 1);
    			desc_txt.text = description[p];
    			picture_num();
    		}
    	}
    }
    function prevImage() {
    	if (p>0) {
    		p--;
    		picture._alpha = 0;
    		picture.loadMovie(image[p], 1);
    		desc_txt.text = description[p];
    		picture_num();
    	}
    }
    function firstImage() {
    	if (loaded == filesize) {
    		picture._alpha = 0;
    		picture.loadMovie(image[0], 1);
    		desc_txt.text = description[0];
    		picture_num();
    	}
    }
    function picture_num() {
    	current_pos = p+1;
    	pos_txt.text = current_pos+" / "+total;
    }

  4. #4
    Member
    Join Date
    Aug 2008
    Posts
    40
    Works great, now just one more thing, I have XML text comments loading into a small text box, but when the comments are larger than the textbox the text bleeds outside of the textbox. I made the textbox larger but it seems to have no effect, how do I make the comment wrap around inside the textbox?

  5. #5
    Senior Member
    Join Date
    May 2008
    Posts
    332
    Well you see how the next and previous buttons work? if p is less than the total number of images in the array, (-1 because arrays start with 0) then p++ or increment p by +1.
    So if p is [4] then Next button changes that to [5] and then does loadmovie[5].
    The problem I see is that there is no "else" statement. In other words what does it do if p is not <(total-1), p is not less than the total number in the array? What does it do when p=7 (meaning there are 8 images in the array)? It needs an else statement to run when it gets to the last picture in the array.
    So here is a revised next button that you can play with:
    Code:
    function nextImage() {
    	if (p<(total-1)) {
    		p++;
    		if (loaded == filesize) {
    			picture._alpha = 0;
    			picture.loadMovie(image[p], 1);
    			desc_txt.text = description[p];
    			picture_num();
    		}
    	}
    	
    /* this section is added
    if p can be incremented do the stuff above, if not set p to 0 and do the stuff below */
    
    	else
    	{
    		p = 0;
    		picture._alpha = 0;
    		picture.loadMovie(image[p], 1);
    		desc_txt.text = description[p];
    		picture_num();
    	}
    /* if that doesn't work, remove this stuff from inside the parenthesis and put here	
    	picture._alpha = 0;
    	picture.loadMovie(image[p], 1);
    	desc_txt.text = description[p];
    	picture_num(); */
    	
    ////end added section ////
    }
    I just include a next button to see if you can get that working before tackling the prev button. It will be very similar giving it an else statement.
    EfV

  6. #6
    Member
    Join Date
    Aug 2008
    Posts
    40
    Thanks Eye, but I have got it working. I just need to resolved the text issue. I need to get the text (that is already loaded via XML) to wrap inside the textbox instead of bleeding outside it. It stays on 1 line and doesn't create a new line when the text exceeds the length of the textbox.

  7. #7
    Senior Member
    Join Date
    May 2008
    Posts
    332
    In the properties panel where you set the font, etc, be sure to select "Multiline". If the dynamic text box is not large enough you may still need to add a scrollbar. Simplest there is to just a UIScrollBar component.
    EfV

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