# Need Random Functionality for an existing XML slideshow

**URL:** <https://forum.kirupa.com/t/need-random-functionality-for-an-existing-xml-slideshow/263540>\
**Category:** flash\
**Created:** [June 17, 2008, 12:20am UTC](https://forum.kirupa.com/t/need-random-functionality-for-an-existing-xml-slideshow/263540 "2008-06-17T00:20:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![artriste](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@artriste](https://forum.kirupa.com/u/artriste)\
**Post date:** [June 17, 2008, 12:20am UTC](https://forum.kirupa.com/t/need-random-functionality-for-an-existing-xml-slideshow/263540/1 "2008-06-17T00:20:31Z")

</div>

Hello,

I was using Emre Biberoglu’s excellent slideshow as a base for a slideshow. Worked absolutely great, until my client asked for the slideshow to display the images in random order.

The untouched source is here:  
[http://www.biberoglu.com/flash/xmlslide/xmlslide.zip](http://www.biberoglu.com/flash/xmlslide/xmlslide.zip)

I tried modifying the values that define the current image to something like: (Math.floor(Math.random() \* nodeCount); to generate a random number as it is a common technique which worked well for me in the past.

The code is pretty clean, but for some reason, every attempt I made, the image number displayed doesn’t match the image shown.

So… just a random function that works, if anybody wants to give it a try.

Cheers

---

<div class="post-metadata">

**Author:** ![artriste](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@artriste](https://forum.kirupa.com/u/artriste)\
**Post date:** [June 18, 2008, 5:10pm UTC](https://forum.kirupa.com/t/need-random-functionality-for-an-existing-xml-slideshow/263540/2 "2008-06-18T17:10:42Z")

</div>

Here is the code. Seems to me very similar to most Slideshow/Galleries that I’ve seen. It has all the functions you would need like next button, fade in, auto or manual… hence the lengthy code. But all I need really is to change some Var to a Random number, so my images load randomly… shouldn’t be too hard. I usually do it without too much trouble.  
All i should have to do is to place [COLOR=“DarkOrange”]Math.floor(Math.random()\*(nodeCount.length));[/COLOR] somewhere… but where?

[AS]  
/////////////////////Settings

startState = “auto”;  
// use “manual” for manual start  
// use “auto” for auto start  
var numFadeRate = 5;  
// Fade in and out rate. You can change it if you want a faster or slower fade in and out.  
// Slide Function (Fade out and unload pictures from the image\_mc movie clip)  
var xmlSource = “images.xml”;

//////////////////////// Handler Script

switch (startState) {  
case “auto” :  
// setTimerCtrl();  
// Check the seleced timer control  
\_root.man\_mc.\_visible = false;  
// Hide manual controls  
\_root.timer\_mc.\_visible = true;  
auto.selected = true;  
// setTimerCtrlAuto();  
break;  
case “manual” :  
clearInterval(intervalID);  
\_root.man\_mc.\_visible = true;  
// Show manual controls  
\_root.timer\_mc.\_visible = false;  
// Hide auto controls  
manual.selected = true;  
break;  
default :  
trace(“no case tested true”);  
}  
stop();  
listenerObject = new Object();  
listenerObject.click = function(eventObject) {  
selectState();  
};  
radioButtonGroup.addEventListener(“click”, listenerObject);

//////////////////////// Auto / Manual switch

// \_root.man\_mc.\_visible = false;  
// Show manual controls  
function setTimerCtrlAuto() {  
// if (\_root.image\_mc.getBytesLoaded() == \_root.image\_mc.getBytesTotal()) {  
// intervalID = setInterval(autoSlideNew, 6000);  
// Set an interval for auto slide  
// }  
}  
function autoSlideNew() {  
if (\_root.image\_mc.getBytesLoaded() == \_root.image\_mc.getBytesTotal()) {  
if (currentRecord == nodeCount) {  
loadNumber = 1;  
} else {  
loadNumber = currentRecord+1;  
}  
imgSlide();  
}  
trace(\_global.myTrigger);  
}  
function setTimerCtrl() {  
if (\_root.image\_mc.getBytesLoaded() == \_root.image\_mc.getBytesTotal()) {  
autoTimer = Number(((\_root.timer\_mc.setTimer.getSelectedItem().label)\*1000)+2000);  
// Check the selected timer  
clearInterval(intervalID);  
// Clear the interval for auto slide  
intervalID = setInterval(autoSlide, autoTimer);  
// Set an interval for auto slide  
}  
}  
function selectState() {  
if (auto.selected == true) {  
// Check if the auto radio button is selected  
// setTimerCtrl();  
// Check the seleced timer control  
\_root.man\_mc.\_visible = false;  
// Hide manual controls  
\_root.timer\_mc.\_visible = true;  
// trace(\_root.timer\_mc.setTimer.getSelectedItem().label);  
// Show auto controls  
// Hide the arrows  
// if (\_root.image\_mc.getBytesLoaded() == \_root.image\_mc.getBytesTotal()) {  
autoTimer = Number(((\_root.timer\_mc.setTimer.getSelectedItem().label)\*1000)+2000);  
// Check the selected timer  
clearInterval(intervalID);  
// Clear the interval for auto slide  
intervalID = setInterval(autoSlide, autoTimer);  
// Set an interval for auto slide  
// }  
} else if (manual.selected == true) {  
clearInterval(intervalID);  
// Check if the manual radio button is selected  
\_root.man\_mc.\_visible = true;  
// Show manual controls  
\_root.timer\_mc.\_visible = false;  
// Hide auto controls  
// Clear the interval created from the auto state.  
}  
}

//////////////////////// SLIDE FUNCTIONS

image\_mc.\_alpha = 0;  
// Hide the image movie clip  
// Declare variables

function imgSlide() {  
onEnterFrame = function () {  
image\_mc.\_alpha -= numFadeRate;if (image\_mc.\_alpha\<0) {delete onEnterFrame;image\_mc.unloadMovie();  
showRecord(loadNumber);}  
};  
}  
// Fade in function  
function show() {  
onEnterFrame = function () { if (\_global.myTrigger == “laoded”) {image\_mc.\_alpha += numFadeRate;if (image\_mc.\_alpha\>100) {delete onEnterFrame;}} else if (\_global.myTrigger == “laoding”) {image\_mc.\_alpha = 0;}};  
}  
// Load slide function. Dynamicly load pictures from “img” folder.  
function autoSlide() {  
if (currentRecord == nodeCount) {  
loadNumber = 1;  
} else {  
loadNumber = currentRecord+1;  
}  
imgSlide();  
}  
// Jump to image function  
function goToImage() {  
if (loadNumber != \_root.man\_mc.dynGo.getSelectedItem().label) {  
loadNumber = \_root.man\_mc.dynGo.getSelectedItem().label;  
imgSlide();  
}  
}

/////////////////////////// XML FUNCTIONS

currentRecord = 0;  
// Variable used to keep track of the current image  
// function for onLoad method of xmlObject  
function dataLoaded(complete) {  
if (complete) {  
\_root.man\_mc.dynGo.removeAll();  
nodeCount = this.firstChild.childNodes.length;  
// Check the image count from the xml file  
showRecord(1);  
\_root.man\_mc.dynGo.rowCount = nodeCount;  
// set the rowcount of jump menu  
for (i=1; i\<=nodeCount; i++) {  
// Add numbers to jump menu  
\_root.man\_mc.dynGo.addItem(i);  
}  
\_root.man\_mc.dynGo.rowCount = 20;  
}  
}  
// XML declaration and loading  
xmlObject = new XML();  
xmlObject.ignoreWhite = true;  
xmlObject.onLoad = dataLoaded;  
xmlObject.load(xmlSource);

//////////////////////////////////////////////// NEXT PREVIOUS BUTTONS FUNCTIONS

// function to be run by “next” button  
function showNext() {  
if (currentRecord == nodeCount) {  
loadNumber = 1;  
} else {  
loadNumber = currentRecord+1;  
}  
imgSlide();  
}  
// function to be run by “previous” button  
function showPrevious() {  
if (currentRecord == 1) {  
loadNumber = nodeCount;  
} else {  
loadNumber = currentRecord-1;  
}  
imgSlide();  
}

// function to set variable names/display fields  
function showRecord(recordNumber) {  
var nodeObject = xmlObject.firstChild.childNodes[recordNumber-1];  
titled = nodeObject.childNodes[0].childNodes[0].nodeValue;  
// description.html = true;  
description = nodeObject.childNodes[1].childNodes[0].nodeValue;  
media = nodeObject.childNodes[2].childNodes[0].nodeValue;  
// trace(media);  
\_global.myTrigger = “”;  
slideFilename = “img/”+media+"";  
image\_mc.loadMovie(slideFilename);  
onEnterFrame = function () { bytesLoaded = \_root.image\_mc.getBytesLoaded();trace(\_global.myTrigger);if (\_global.myTrigger == “laoded”) {selectState();delete onEnterFrame;show();} else if (\_global.myTrigger == “laoding”) {image\_mc.\_alpha = 0;clearInterval(intervalID);}};  
currentRecord = recordNumber;  
\_root.man\_mc.dynGo.setSelectedIndex(currentRecord-1);  
}  
[/AS]

---

<div class="post-metadata">

**Author:** ![artriste](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@artriste](https://forum.kirupa.com/u/artriste)\
**Post date:** [June 19, 2008, 1:57pm UTC](https://forum.kirupa.com/t/need-random-functionality-for-an-existing-xml-slideshow/263540/3 "2008-06-19T13:57:29Z")

</div>

One last time …

I’m sure somebody can solve this in a few minutes…  
It really is about 3 lines to be replaced.
