all groups > flash (macromedia) > august 2003 >
You're in the

flash (macromedia)

group:

random clip order


random clip order terence hegarty
8/19/2003 7:33:42 PM
flash (macromedia): I would like to write a script that would fire a random clip order each
time the user visits the page. The script needs to encompass a total of
15 clips.
There can be no redundancy in the random selection and each one of the
15 clips needs to fire.

I can logically deduce a conditional (if, than else) routine that would
work, but that sounds like to much trouble.
I bet there is an array function that will take the order of an array
like i.e.:
firingOrder= [squ1,squ2,squ3,squ4];

and reshuffle them
i.e.:
firingOrder= [squ3,squ2,squ4,squ1];

then all I will need to do is call the array's result to call each
square instance on a randomly generated basis.
that I figure would work- yet I do not know the array function to
achieve those results..

Any help is appreciated.

Thanks,
terence.




Re: random clip order a
8/20/2003 11:23:26 AM
Hi Terence,

This should work:

Array.prototype.shuffleObjects = function()
{
var i;
for(i=0; i<this.length; this[i++].shufflekey = Math.random());
this.sortOn("shufflekey");
for(i=0; i<this.length; delete this[i++].shufflekey);
};

the above makes the method shuffleObjects() available to all Array
objects, but it can shuffle only arrays that have Object elements
(which includes your movieclips).

usage:

myarray=[mc1,mc2,mc3,mc4];
myarray.shuffleObjects();


Note that you have to use an iterator loop like
for( it in myarray ) { ... }
to see the new random ordering.
For some reason (bug?) flash sorts the array indexes along with their
values (at least in my player version).

Here is a test script you can try to see how it works:

var i,it;
firingOrder = [squ1,squ2,squ3,squ4];

trace("before:");
trace("using index loop:");
for(i=0;i<firingOrder.length;i++){trace("firingOrder["+i+"]=="+firingOrder[i]);}
trace("using iterator loop:");
for(it in firingOrder){trace("firingOrder["+it+"]=="+firingOrder[it])}

firingOrder.shuffleObjects();

trace("after:");
trace("using index loop:");
for(i=0;i<firingOrder.length;i++){trace("firingOrder["+i+"]=="+firingOrder[i]);}
trace("using iterator loop:");
for(it in firingOrder){trace("firingOrder["+it+"]=="+firingOrder[it])}



Have fun.


On Tue, 19 Aug 2003 19:33:42 -0400, terence hegarty
[quoted text, click to view]
AddThis Social Bookmark Button