Skip to main content
Known Participant
May 3, 2007
Question

how do i modify this existing shuffle array / randomiser ?

  • May 3, 2007
  • 17 replies
  • 1418 views
hello !

some one on this forum posted the below code which has been perfect for my requirements but i would
like to stop the randomiser from repeating previously played .swf files...

in laymens terms, ONCE this random file is played, do not play until all other files have been played once....

WHERE DO I START ? (an explanation of what code does what would be appreciated)

var randomMax:Number = 10;
var randomMin:Number = 1;
var randomFileNum:Number = Math.floor(Math.random() * randomMax + randomMin);

trace(randomFileNum); //will display from 1 to 10;

loadMovieNum(randomFileNum + ".swf", 1);


cheers
This topic has been closed for replies.

17 replies

Inspiring
May 18, 2007
barryels – what you are suggesting is certainly doable, but is far more complex than the given solution. Imagine if we played cards that way.
Participating Frequently
May 18, 2007
To get back to the original question of not repeating previously played swf's...

Couldn't you just create a new Array (call it "filesViewed") to hold the values of any swf's that have played, and when a new request for an swf is made, your function should simply loop through "filesViewed" and have a condition to check whether the requested file exists in the array: if it doesn't exist, play the file and add it to the "filesViewed" array.

I don't have any previous experience with the codebase your using, so the above might not work with your implementation.
kglad
Community Expert
Community Expert
May 18, 2007
what do you mean by "...i get zero results with that code"?

if you mean nothing loads after your first button release, then your swf file names are not 301.swf,...,307.swf or they are not in the same directory as your flash html and swf files.
wAySteadAuthor
Known Participant
May 17, 2007
it looks right but i get zero results with that code
wAySteadAuthor
Known Participant
May 13, 2007
i get frustration...

so i have modified the code as suggested

and get

**Error** Scene=Scene 1, layer=Layer 3, frame=1:Line 16: Identifier expected
var fileToLoad=myArray[count++]+".swf",1);

Total ActionScript Errors: 1 Reported Errors: 1

when launching the movie and the button does nothing

(please see bottom of this post for my current code modifed from your notes)

i do appreciate your help and let me explain exactly my objective.

i have a randomiser that works perfectly other than it repeats some .swf
prior to playing all of them in a RANDOM SHUFFLE

I do not seek a step by step routine shuffle

ie : 5.swf > 6.swf > 7.swf

but totally random

2.swf > 13.swf > 59.swf

with no repeats until the entire range is played.

my current code / elements are as follows.

one button called myButton1 with no attributes

and the below code :


Array.prototype.shuffle = function() {
for (var ivar = this.length-1; ivar>=0; ivar--) {
var p = random(ivar+1);
var t = this[ivar];
this[ivar] = this

;
this

= t;
}
};
//Hide the shuffle prototype from the tyranny of the for-in loop
ASSetPropFlags(Array.prototype,["shuffle"],1,1);

var myArray:Array=new Array(301,302,303,304,305,306);
myArray.shuffle();
trace(myArray);

var fileToLoad=myArray[count++]+".swf",1);
trace("Attempting to load "+fileToLoad;
myButton1.onRelease=function(){
loadMovieNum(fileToLoad,1);
}


IF SOMEONE COULD JUST SUPPLY THE SCRIPT WITH THE OBJECTIVE
OF PLAYING THROUGH AN ARRAY FROM 301.SWF - 307.SWF I WILL BE
ETERNALLY GRATEFULL....




kglad
Community Expert
Community Expert
May 13, 2007
try:

Inspiring
May 12, 2007
Well the reason it shows the numbers is because of the trace(myArray). That is
just so that you could see that the array was being shuffled and you can remove
that.

Having the load say myArray[0] is only going to request the first item from
the array.

Maybe change the part inside the onRelease to this:

var fileToLoad=myArray[count++]+".swf",1);
trace("Attempting to load "+fileToLoad;
loadMovieNum(fileToLoad,1);

What do you get with this?

Inspiring
May 12, 2007
Well the reason it shows the numbers is because of the trace(myArray). That is just so that you could see that the array was being shuffled and you can remove that.

Having the load say myArray[0] is only going to request the first item from the array.

Maybe change the part inside the onRelease to this:

var fileToLoad=myArray[count++]+".swf",1);
trace("Attempting to load "+fileToLoad;
loadMovieNum(fileToLoad,1);

What do you get with this?
wAySteadAuthor
Known Participant
May 12, 2007
hmmmm....

i'm still doing something wrong...

i have pasted the below code in the first empty cell as suggested

Array.prototype.shuffle = function() {
for (var ivar = this.length-1; ivar>=0; ivar--) {
var p = random(ivar+1);
var t = this[ivar];
this[ivar] = this

;
this

= t;
}
};
//Hide the shuffle prototype from the tyranny of the for-in loop
ASSetPropFlags(Array.prototype,["shuffle"],1,1);

var myArray:Array=new Array(300,302,303,304,305,305);
myArray.shuffle();
trace(myArray);
var count=0;
myButton1.onRelease=function(){
loadMovieNum(myArray[0]+".swf",1);
}


and created a button that resides within that cell but has no qualities associated
with it other than the name myButton1

the result is when i run the script it publishes the numbers

301, 305, 302, 304, 303, 306

and displays these in a random order everytime i run the flash movie...

as oppose to loading one movie ((myArray[0]+".swf",1);

and it displays these numbers on the flash stage prior to me even
clicking on myButton1 which does nothing...

doh... i'm a script retard... what are we to do ?

Inspiring
May 9, 2007
Well, unless you files are named:

Array element 0 is 396.swf

That won't work. :) But chin up, you are almost there.

Arrays are kind of like a series of little boxes or cubbys. Each has a number and each holds something in it. So when you want something from, let's say the fifth cubby you ask for it like this:

myArray[4]

Notice that even though I want the fifth thing I ask for number 4. That is because Flash arrays always start with zero not one.

So if you have 396 in the first cubby and you want to make a file name like 396.swf. You would do this:

loadMovieNum(myArray[0]+".swf",1);

You have to put the .swf part in quotes so that Flash knows it isn't some variable or property that you are trying to get. Now if you also know that all your files are named something like OurShow396.swf. Then you can combine that like:

loadMovieNum("OurShow"+myArray[0]+".swf",1);

Now how about that mysterious count++?

Well that is because I assumed you want to step through the array. Notice how outside the button onRelease code I started with count=0? That gives Flash a place to start. Remember Arrays start at zero. The first time they click on the button Flash evaluates the value of count and asks the array for what it is keeping in the box of that number. The ++ afterwards says, "After you have used this value of count add one to it for the next time."

Can you guess what count-- does?

Now this isn't a complete solutions, what happens when count gets bigger than the total number of cubbies in the array? Well you could add this code inside the onRelease – put it after the loadMovieNum.

if(count>myArray.length){
count=0;
}

That says, "if count is larger than the length (number of cubbies) in myArray, then reset count to zero."
wAySteadAuthor
Known Participant
May 9, 2007
no joy...

on(Release) in my code is a reference to the button being selected
to play a random .swf movie file....

if the user gets tired of that movie, they can hit the button again and it
will load a new one, but not repeat previous....

i replaced the

myButton1.onRelease=function(){
trace("Array element "+count+" is "+myArray[count++]);
}


with

myButton1.onRelease=function(){
loadMovieNum("Array element "+count+" is "+myArray[count++]);
}

but don't know where to place the .swf extention

i tried this but with no success

myButton1.onRelease=function(){
loadMovieNum("Array element "+count+" is "+myArray[count++] .swf, 1);
}