20 Replies Latest reply on May 18, 2007 6:17 AM by Rothrock

    how do i modify this existing shuffle array / randomiser ?

    wAyStead
      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
        • 1. Re: how do i modify this existing shuffle array / randomiser ?
          dr_ross Level 1
          easiest just to make an array of the numbers you need and shuffle it.

          //And ths code to add the shuffle method to the array class

          Array.prototype.shuffle = function() {
          var i:Number;
          this.sort(function(a,b) { i = Math.round((Math.random() * 100) - 50); return i;});
          return this;
          };


          var fileNum_array = [];//declare empty array
          for( var i=randomMin;i<=randomMax;i++)
          {
          fileNum_array.push(i);
          }
          fileNum_array.shuffle();

          then pull the values out of the array incrementally


          loadMovieNum(fileNum_array[0] + ".swf", 1);
          loadMovieNum(fileNum_array[1] + ".swf", 1);
          loadMovieNum(fileNum_array[2] + ".swf", 1);
          etc...

          or use an incrementing variable everytime you need to return a value

          var myIncVar = 0;

          loadMovieNum(fileNum_array[myIncVar++] + ".swf", 1);
          loadMovieNum(fileNum_array[myIncVar++] + ".swf", 1);
          etc...

          Hope that helped, Ade
          • 2. Re: how do i modify this existing shuffle array / randomiser ?
            Rothrock Level 5
            I concur with dr_ross' approach, but offer a slightly different version of the shuffle method. This is something that we have discussed in some rather long threads in the past. In general the one that provides the best stability and speed is below.

            The random() is gone from AS3, but then again a lot of things have changed there and none of this will work. Except the algorithm – step backward through the array, at each spot choose a random element from the spot or below, swap them, step back to the previous element in the array…

            I've also added the ASSetPropFlag to hide the new method. If you don't know what it does, create a small array and then try the following code both with and without that line.

            for(var a in myArray){
            trace(a+" is "+myArray[ a ]);
            }

            Replace myArray with the name of your Array.
            • 3. Re: how do i modify this existing shuffle array / randomiser ?
              wAyStead Level 1
              thank you doctors for your concuring assements for
              my current dilema,

              i'll be back to concur or maybe i will beg to differ

              • 4. Re: how do i modify this existing shuffle array / randomiser ?
                wAyStead Level 1

                I dont concur

                :(

                it doesn't work...

                i've tried the code and it just gets a THIS SCRIPT IS SLOWING DOWN FLASH
                message and then crashes.....

                would someone mind supplying / editing the script for my requirements ??

                i'm trying to get the shuffle to go through the following files...

                301.swf, 302.swf, 303.swf, 304.swf, 305.swf, 306.swf and 307.swf


                pretty please with sugar on top !

                • 5. Re: how do i modify this existing shuffle array / randomiser ?
                  Rothrock Level 5
                  How, exactly, are you using it? I've used this for years and never had any trouble at all.
                  • 6. how do i modify this existing shuffle array / randomiser ?
                    wAyStead Level 1
                    sorry people, i cant get this to work...

                    my entire script assigned is as follows...

                    the button at present doens't even animate properly with this new code...

                    my code reads as follows..

                    on (rollOver) {
                    gotoAndPlay(2);
                    }
                    on (releaseOutside, rollOut) {
                    gotoAndPlay(11);
                    }
                    on (release) {
                    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[p];
                    this[p] = t;
                    }
                    };
                    //Hide the shuffle prototype from the tyranny of the for-in loop
                    ASSetPropFlags(Array.prototype,["shuffle"],1,1);

                    var myArray:Array=new Array(396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416 );
                    myArray.shuffle();
                    trace(myArray);
                    • 7. how do i modify this existing shuffle array / randomiser ?
                      wAyStead Level 1
                      I don't know much about actionscript (That much is obvious)
                      but i am exporting my movie in ACTIONSCRIPT 2 to support
                      other code...

                      this format seems to support my old method (posted above)
                      but this new array doesn't wont to work for me.

                      i thought the numeric dynamics for the animated button may have
                      been confusing the code so i designed a simple button for the
                      same procedure but still no joy.

                      u can see the context i am trying to use it in on

                      BulletProofTV.Com

                      for example, visit MUSIC, hit BREAKBEAT randomiser..

                      at present it is the old randomiser coz like i said i cant get it to work
                      and need some sleep.........

                      zzzz
                      • 8. Re: how do i modify this existing shuffle array / randomiser ?
                        Rothrock Level 5
                        Ah, there's your problem.

                        This is not something that goes on a clip or a button.

                        Open a brand new document.

                        Select the first frame.

                        Press F9 or alt/opt-F9 depending on your version of flash.

                        Paste the code (Just the part I gave you) in the AS window.

                        The Array.shuffle() method is now available to shuffle any array you desire.

                        Just some advice, stop using on(clip Event) style of coding! That is something that came into being for a short while between 2001 and 2002. Since Flash MX it has been surpassed by using timeline code. So let's continue.

                        Take an instance of your button (which I hope is really a movieclip) and put it on the stage.

                        Make sure to give it an instance name in the property panel. Something like myButton1 or such.

                        Find all that code that you put on it and get rid of it.

                        Now make sure you don't have the button selected, but select the frame and bring up the AS window. If you don't see the array code from before then you have the wrong thing selected.

                        Add the following below that code.

                        var count=0;
                        myButton1.onRollOver=function(){
                        this.gotoAndPlay(2);
                        }
                        myButton1.onRollOut=myButton1.onReleaseOutside=function(){
                        this.gotoAndPlay(11);
                        }
                        myButton1.onRelease=function(){
                        trace("Array element "+count+" is "+myArray[count++]);
                        }

                        I don't really understand what you are trying to do with the on(Release) in your code. So I just have it tracing each element from the array. But you could use that to load or do something else.

                        The advantage to this style of coding is that all your code is in one place. You don't have to go hunting for it.

                        The array was already shuffled when you did myArray.shuffle(); So unless you want to shuffle it again when you get to the onRelease you don't need to do that again. It is kind of like a deck of cards. You shuffle it once and then just deal one from the top everytime you need it.

                        • 9. Re: how do i modify this existing shuffle array / randomiser ?
                          wAyStead Level 1
                          thanks ! i'll give that go

                          =)
                          • 10. Re: how do i modify this existing shuffle array / randomiser ?
                            wAyStead Level 1
                            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);
                            }

                            • 11. Re: how do i modify this existing shuffle array / randomiser ?
                              Rothrock Level 5
                              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."
                              • 12. how do i modify this existing shuffle array / randomiser ?
                                wAyStead Level 1
                                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[p];
                                this[p] = 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 ?

                                • 13. Re: how do i modify this existing shuffle array / randomiser ?
                                  Rothrock Level 5
                                  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?
                                  • 14. Re: how do i modify this existing shuffle array /   randomiser ?
                                    Level 7
                                    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?

                                    • 15. how do i modify this existing shuffle array / randomiser ?
                                      wAyStead Level 1
                                      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[p];
                                      this[p] = 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....




                                      • 17. Re: how do i modify this existing shuffle array / randomiser ?
                                        wAyStead Level 1
                                        it looks right but i get zero results with that code
                                        • 18. Re: how do i modify this existing shuffle array / randomiser ?
                                          kglad Adobe Community Professional & MVP
                                          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.
                                          • 19. Re: how do i modify this existing shuffle array / randomiser ?
                                            barryels
                                            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.
                                            • 20. Re: how do i modify this existing shuffle array / randomiser ?
                                              Rothrock Level 5
                                              barryels – what you are suggesting is certainly doable, but is far more complex than the given solution. Imagine if we played cards that way.