2 Replies Latest reply on May 17, 2007 8:47 PM by kglad

    a new approach : non repeat randomiser

    wAyStead
      i have been using the following code asigned to the hit area of my animated button as a randomiser.

      on (rollOver) {
      gotoAndPlay(2);
      }
      on (releaseOutside, rollOut) {
      gotoAndPlay(11);
      }
      on (release) {
      var randomMax:Number = 15;
      var randomMin:Number = 1;
      var randomFileNum:Number = Math.floor(Math.random() * randomMax + randomMin);

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

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

      }


      which is the routine that animates my flash button and then loads up
      a random movie....

      i wish to remove the

      "REPEAT" feature... in other words as the viewer repeatedly clicks
      the PLAY RANDOM MOVIE button it shuffles through all X titles
      in that array prior to repeating them.

      the below array / option has been presented but is not suitable / doesn't
      work amidst the complex cell enviroment already runnning other
      random arrays for other routines which do not need to be discussed,
      but trust me the below is not the solution for this problem...

      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;
      }
      };
      // don't change anything above this line ..................................................

      var swf_ar:Array = new Array("carousel1.swf", "carousel2.swf", "carousel3.swf");
      swf_ar.shuffle();
      index=0;
      function load_random() {
      carousel.loadMovie(swf_ar[index++]);
      if(index==swf_ar.length){
      // do something. they've all played. reset index to zero if you want them to repeat and reuse the shuffle() method if you want to re-randomize etc
      }
      }
      load_random()

      in fact it ***** with my entire site, and turns it into doodoo

      any other simple, efficent suggestions maybe using a variation
      on my existing code ??

      thanks





        • 1. Re: a new approach : non repeat randomiser
          Rothrock Level 5
          I don't know what your issue is, but the prototype is totally stable and usable under an AS2 file. How does it turn your site to doodoo?

          The prototype does nothing to any existing arrays unless you call the shuffle() method. So if you have other arrays that you wish to manipulate other ways then simply don't call shuffle() on them.

          The only thing it could mess up is if you are using for…in loops, you will have a problem that suddenly every array will have one more thing in it, namely the shuffle prototype. I had included a line that hides the prototype from the for…in construction.

          Other than this approach there is no other easy way to do this. You could randomly pick one from the array and then compare if it has already been selected, and keep selecting until you get one that hasn't been selected. But this approach will get complicated and messy.
          • 2. Re: a new approach : non repeat randomiser
            kglad Adobe Community Professional & MVP
            glad you said that rr, because i took those criticisms personally.