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

    how do i modify this existing shuffle array / randomiser ?

    wAyStead Level 1
      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