PDA

View Full Version : making random backgrounds more random w AS



zerosignal0
September 20th, 2004, 01:52 AM
Hello,
Im working on my site and I have a random background script setup. Heres the code that Im using now.


choice = Math.round(Math.random()*4);
switch (choice) {
case 0 :
_root.background.loadMovie("background1.swf");
break;
case 1 :
_root.background.loadMovie("background2.swf");
break;
case 2 :
_root.background.loadMovie("background5.swf");
break;
case 3 :
_root.background.loadMovie("background7.swf");
break;
case 4 :
_root.background.loadMovie("background8.swf");
break;
}

Now I love the way thats its working and but one thing that is kinda crappy is that sometimes it suffers from repeating the same background a couple of times before going to another random background. Now I know that its because the script is working properly but can anyone think of a method that I could use to make it not be able to pull up the last background that it has already loaded? I dont want to make it static because not only do I have this setup for when you enter the site but also its random when you click on one of the nav buttons because I want to try to keep my site dynamic.

Any ideas?
Thanks in advance.

VoS
September 20th, 2004, 11:40 AM
make a do while loop that checks if the last random number was the same as the new one if it is just loop it again




do {
choice = Math.round(Math.random()*4);
} while (zzz == choice); // check so the previous value that was spit out is not the same one it spits out this time
zzz = choice; // if it is makes it do the loop again, till it gets one that wasent same






hope it helps =)

stringy
September 20th, 2004, 02:50 PM
Another way to do this

myArray = ["background1.swf", "background2.swf", "background3.swf", "background4.swf", "background5.swf"];
function shuffle() {
return Math.round(Math.random()*2-1);
}
myArray.sort(shuffle);
Then just loop through the array

zerosignal0
September 20th, 2004, 03:16 PM
well I tried both of them but they dont seem to work... they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together... Im open to suggestions ;)
thanks for responding by the way I appriciate it

stringy
September 20th, 2004, 03:31 PM
well I tried both of them but they dont seem to work... they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together... Im open to suggestions ;)
thanks for responding by the way I appriciate it
Not sure what you are doing with the backgrounds. If you want a different background each time a visitor returns to your site, you will have to have a look at sharedObject. But if you are changing the background periodically while a user is on the site, my code will work.

for example

myArray = ["background1.swf", "background2.swf", "background3.swf", "background4.swf", "background5.swf"];
function shuffle() {
return Math.round(Math.random()*2-1);
}
createEmptyMovieClip("container", 1);
myArray.sort(shuffle);
i = 0;
function changebackground() {
if (i>=myArray.length) {
i = 0;
}
loadMovie(myArray[i], "container");
i++;
}
myInterval = setInterval(changebackground, 2000);

ipaqflash
September 20th, 2004, 03:31 PM
r ur bkgrds swf's just colored bkgrds?




well I tried both of them but they dont seem to work... they still repeat when I put in either snippits of code you gave. I might have to change around how Im calling to the backgrounds all together... Im open to suggestions ;)
thanks for responding by the way I appriciate it

zerosignal0
September 20th, 2004, 04:01 PM
r ur bkgrds swf's just colored bkgrds?
no you can see them here
www.zerosignaldesigns.com/site.htm (http://www.zerosignaldesigns.com/site.htm)