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 );