PDA

View Full Version : Help needed with a random image script



Skellz
September 19th, 2006, 05:23 AM
Hello people, i was wondering if someone could possibly lend me a hand. I have this great idea which i have begun implimenting on my website but i have currently hit a brick wall. The idea is to have three images on the home page. Every time the page is reloaded the images change to a new random image (from a defined array of images). But...and this is the problem...there are THREE of these images. I need the images to both not repeat themselves nor have the same displayed in two images. For example, i dont want images 1/2/3 to be proceeded by images 1/2/3 (in any order), nor do i want images 1/2/2 displayed. Always three new images.

This is the code i am currently using, is slightly adapted from another i found on the web.


--------------------------------------------------------------------------------

fscommand("allowscale", "true");


//Preload Function
_root.bar._visible = 0;
_root.barBorder._visible = 0;

MovieClip.prototype.loadPic = function(pic) {

this.loadMovie(pic);
this._parent.onEnterFrame = function() {
var t = containerMC.getBytesTotal(), l = containerMC.getBytesLoaded();
var t = containerMC1.getBytesTotal(), l = containerMC1.getBytesLoaded();
var t = containerMC2.getBytesTotal(), l = containerMC2.getBytesLoaded();
bar._visible = 1;
barBorder._visible = 1;
per = Math.round((l/t)*100);
if (t != 0 && Math.round(l/t) == 1 && "containerMC", "containerMC1", "containerMC2"._width != 0) {

bar._visible = 0;
barBorder._visible = 0;
loadText3.text = "";
delete this._parent.onEnterFrame;
} else {
bar._width = per * 2;
loadText3.text = per+" % loaded";
}
};
};

//Random images
pics = new Array("park2.jpg", "park5.jpg", "park8.jpg", "park12.jpg");
var randNum = getRandom(0, 3);
_root.containerMC.loadPic(pics[randNum],pics[randNum],100);
_root.containerMC1.loadPic(pics[randNum],pics[randNum],100);
_root.containerMC2.loadPic(pics[randNum],pics[randNum],100);
pics[randNum]._x = 0;
pics[randNum]._y = 0;
// function to generate a random integer
// between minimum and maximum, inclusively
function getRandom(minimum, maximum) {
return Math.floor(Math.random()*3(maximum-minimum+1)+minimum);
}

--------------------------------------------------------------------------------


If anyone could help by modifying this code that would be awesome. Thanks alot.

Skellz
:cowboy:

Skellz
September 20th, 2006, 09:50 AM
The main problem is getting the individual containers to load seperate random images. At the moment they are all loading the same image.

Thanks

Skellz:mario:

VoS
September 20th, 2006, 01:28 PM
hey.
ive done something similar to this, cant find the code atm but ill try to explain the general idea to you. ( and if that dosent work ill rewrite it =p)

ok lets say you have an array with four images

img=[img1.jpg,img2.jpg,img3.jpg,img4.jpg]

this i take it you knnow how to do , read in images names and so on, or just placing them in the array if they are not dynamically changable

get the length of that array

img.length();

or somethin gsimilar to that will do that

create a second array which is as long as the 1st one but for every slot in it make tha value 1 im gonna call this 'secondarray'(this can easily be done with a for loop)

now all you have to do is assign the three movieclips with an image and check if its been taken

for(x=1, x!= img.length()){

Done=false;
while (Done==false){
num=Math.round(Math.random()*img.length());
if(secondarray[num]==1){
Done=true;
secondarray[num]=0;
}
}
mc[x].loadMovie(img[num]); //mc1,mc2,mc3
}

the generall idea is to choose a number
check if that number has already come up by using the second array
if it has get new number

rinse and repeat

hope this was to some help

sorry if this 'help' sux ;) no coffe yet

//VoS