6 Replies Latest reply on Sep 17, 2009 10:00 AM by Rothrock



      I’m interested in understanding the ActionScript 2 code that will allow a random decision on the next marker to jump to. Please post any tutorials that may help.


      I have five markers on a time line and want the movie to stop after playing a sequence of elements, then randomly choose the next marker to jump to and play, this would continue forever.

      The goal is to communicate a few concepts without starting from the same concept every time in a linear manner.

        • 1. Re: randomness
          christoon_ca Level 1

          On frame 6 add the code:



          • 2. Re: randomness
            Ned Murphy Adobe Community Professional & MVP

            Store the markers in an array and when it comes time to do things randomly, shuffle the array so that the elements are ordered randomly and walk thru them in order (index 0 thru 4).  If you want them to be random each time the group completes, shuffle them each time the count reaches the end.

            1 person found this helpful
            • 3. Re: randomness
              Rothrock Level 5

              I would use the approach Ned has outlined. I would also probably add something so that the zeroth element in the new array is not the same as the last element in the old array. That way you don't get a repeat.

              • 4. Re: randomness
                linxvisual Level 1

                Shuffling an array, containing markers, sounds like an good solution.


                Can you provide a good resource to learn more about how to setup an array?


                I spend most of my time creating art and manipulating the animation on the timeline. I'm beggining to understand actionscript but the coding is more difficult for me until I've seen it once.



                • 5. Re: randomness
                  Ned Murphy Adobe Community Professional & MVP

                  To define an array with fixed elements....


                  var markerArray = new Array("marker1","marker2","marker3", etc...);


                  To shuffle that array you can use the following function and the subsequent call to it....


                  function shuffle(a) {
                  var len = a.length-1;
                  for (i= len; i>=0; i--) {
                    var p:Number = Math.floor(Math.random()*(i+1));
                    var t = a[i];
                    a[i] = a[p];
                    a[p] = t;
                  return a;


                  markerArray = shuffle(markerArray);

                  • 6. Re: randomness
                    Rothrock Level 5

                    This is a very useful thing, and just to be clear, you can use this for all kinds of things. Say you need some random, non-repeating numbers, etc....


                    I would make a few little changes to Ned's code here. Because in Flash arrays are passed by reference not by value this code will change your original array whether you want it to or not. And make sure to declare the i in the for loop as a var or it can mess up other loops that might be also using i as a loop index.


                    function shuffle(src:Array) {

                    var tmp:Array=src.slice();
                    for (var i= tmp.length-1; i>=0; i--) {
                      var p:Number = Math.floor(Math.random()*(i+1));
                      var t = tmp[i];
                      tmp[i] = tmp[p];
                      tmp[p] = t;
                    return tmp;


                    So in that case if you want to keep the original array -- probably not in this case, but maybe in others you could do


                    newShuffledArray = shuffle(markerArray);


                    And if you want the original array to be shuffled you would do this:


                    markerArray = shuffle(markerArray);


                    With Ned's version the following two lines will produce the same result:



                    markerArray = shuffle(markerArray);


                    I think it is good to have the option to shuffle the original or retain the original.


                    Then to make sure that when you shuffle you don't get the same value that you were just using as the zeroth value in the next series I would add a do...while loop like this:


                    do {
                      var newArray:Array = shuffle(markerArray);
                    } while (markerArray[markerArray.length - 1] == newArray[0]);
                    markerArray = newArray;