actionscript question
- Started
- Last post
- 14 Responses
- itstimefortea
if anyone can help me with this actionscript problem, i'd really appreciate it.
i need to randomly load an swf from a set of 15. it seems straightforward enough, and i've gotten a lot of code examples from tutorial sites. the problem is i'm trying to get it so the same image doesn't load twice and i can't find a specific example of this. if anyone can suggest code or where to find it, that would be very helpful.
cheers....
- itstimefortea0
using as2 btw...
- WeLoveNoise0
why dont u use a code that randomly pulls in the swfs stated in a xml file.
- thats how i'm achieving the same effect with a current siteWeLoveNoise
- itstimefortea0
would that avoid loading the same swf twice in a row? thats the main problem i'm having. what is the advantage of using xml instead of actionscript? (or in combination with)
- depends how you coded it i guess but if there is enough swfs, the chance of its showing twice is rareWeLoveNoise
- random does mean that you'll possibly see the same swf twice in a row.bulletfactory
- eryx0
XML is easier to manage and also does not bloat your final .swf file.
- uan0
just make a variable where you store the name of the last loaded image...and then on the random function check if it's the last one, if yes redo random, if no go on.
- like thatbrandelec
- thats exactly what i'm after... except i can't code it myself... do you have any examples of similar things?itstimefortea
- eryx0
If you were going to work with XML I suggest you do this in AS3. It is alot easier.
As for the problem at hand it is hard to understand exactly what the problem is without seeing the code. Do you have an Actionscript.org account?
- brandelec0
i think you need to put it in array, so it checks the last value - preventing the same swf to be loaded again?
- mikotondria30
put the names of the swfs from yr xml into an array, then shuffle :)
Or shuffle the names as they go into the xml...
- ornj0
what happens when all 15 have been loaded? reset?
- they each load one at a time. as in when you click on something, the swf gets replaced with a new random oneitstimefortea
- WeLoveNoise0
maybe this will work for you
http://www.flashsourcecode.com/c…- that one works really well, except it still loads the same image twice in a row occasionally. thats what i'm trying to avoiditstimefortea
- kingsteven0
var pages:Array = new Array("page1.swf", "page2.swf", "page3.swf");
var shuffle:Array = new Array();
while (pages.length > 0) {
shuffle.push(pages.splice(Math.f... 1));
}then just load them sequentially...
- itstimefortea0
this is close, its set up to load 3 separate images, but doesn't work very well when i delete the last 2 lines....
var images = ["img1.jpg","img2.jpg","img3.jpg...
function randomize(arr:Array){
var tmp,rn;
var len = arr.length;
for(var i=0;i < len;i++){
tmp = arr[i];
arr[i] = arr[rn = random(len)];
arr[rn] = tmp;
}
}
randomize(images);
trace(images); //random order..mc1.loadMovie(images[0]);
mc2.loadMovie(images[1]);
mc3.loadMovie(images[2]);
- kingsteven0
Oeps: ^^
pages.splice(Math.floor( Math.random() * pages.length ), 1);haven't tested it though...
- kingsteven0
function randomize(arr:Array){
var tmp:Array = new Array();
while (arr.length > 0) {
tmp.push(arr.splice(Math.floor( Math.random() * arr.length ), 1));
}
return tmp;
}
images = randomize(images);Again, untested but a possible fix...