Skip navigation
Lenaped
Currently Being Moderated

Playing random movieclips via timeline without repeating

Aug 31, 2012 12:30 PM

I have eight movie clips on my main timeline. The idea is to play the first movie clip, once a button is clicked, go to a random frame 2-8 on the timeline and play the movielip on that frame, the get sent back to frame 1 to start the process over. I have this figured out, but I also don't want any frames repeating until they have all been played. Inside the first movieclip is a button with the following actionscript attached. Everything works perfectly EXCEPT my frames are repeating before they have all played. I can't see in my script where I have gone wrong. Please help!

 

 

var nums:Array = new Array();

1,2,3,4....

for(var i:uint=1; i<7; i++){

nums.push(i);

}

 

// for shuffling the array

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a[p];

        a[p] = t;

    }

}

 

// shuffle the array

shuffle(nums);

 

// set up the controls for the button

var count:uint = 0;

 

boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);

 

function plyRndmMv(evt:MouseEvent):void {

    MovieClip(root).gotoAndStop(nums[count]+2);

    count++;

    if(count == 8){

        shuffle(nums);

        count = 0;

    }

}

 
Replies
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 12:46 PM   in reply to Lenaped

    if that code is on frame 1, you have a problem.  if it is, you can use a boolean to prevent that code from repeatedly executing every time you return to that frame.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 31, 2012 12:46 PM   in reply to Lenaped

    var nums:Array = new Array();

    1,2,3,4....

    for(var i:uint=1; i<7; i++){

    nums.push(i);

    }

     

    // for shuffling the array

    function shuffle(a:Array):Array {

        var randomArray=new Array();

        var p:int;

        var t:*;

        var ivar:int;

         while(a.length>0){

           p=Math.round(a.length-1*Math.random());

            randomArray.push(a[p])

            a .splice(p, 1);

         }

         return randomArray;


     

     

    }

     

    // shuffle the array

    shuffle(nums);

     

    // set up the controls for the button

    var count:uint = 0;

     

    boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);

     

    function plyRndmMv(evt:MouseEvent):void {

        MovieClip(root).gotoAndStop(nums[count]+2);

        count++;

        if(count == 8){

            nums=shuffle(nums);

            count = 0;

        }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 31, 2012 12:49 PM   in reply to esdebon

    the problem is:

     

     

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];  // <  HERE p can be a number for example 2 then p can be again 2 and then 2...

            a[p] = t;

        }

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 1:08 PM   in reply to esdebon

    esdebon,

     

    there's nothing wrong with the shuffle() function.  that's an implementation of the fisher-yates shuffle.

     

    go experiment/study it but please don't confuse the op.

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 1:10 PM   in reply to Lenaped

    use:

     

    var alreadyExecuted:Boolean;

     

    if(!alreadyExecuted){

    alreadyExecuted=true;

    var nums:Array = new Array();

    1,2,3,4....

    for(var i:uint=1; i<7; i++){

    nums.push(i);

    }

     

    // for shuffling the array

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     

    // shuffle the array

    shuffle(nums);

     

    // set up the controls for the button

    var count:uint = 0;

     

    boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);

     

    function plyRndmMv(evt:MouseEvent):void {

        MovieClip(root).gotoAndStop(nums[count]+2);

        count++;

        if(count == 8){

            shuffle(nums);

            count = 0;

        }

    }

    }

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 2:11 PM   in reply to Lenaped

    it is assigned to true if you copied that code.

     

    what do the traces reveal:

     

    var alreadyExecuted:Boolean;

     

    if(!alreadyExecuted){

    alreadyExecuted=true;

    var nums:Array = new Array();

    //1,2,3,4....

    for(var i:int=2; i<=8; i++){

    nums.push(i);

    }

     

    // for shuffling the array

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     

    // shuffle the array

    shuffle(nums);

    trace(nums);

     

    // set up the controls for the button

    var count:uint = 0;

     

    boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);

     

    function plyRndmMv(evt:MouseEvent):void {

        MovieClip(root).gotoAndStop(nums[count]);

    trace(nums[count]);

        count++;

        if(count == 8){

            shuffle(nums);

            count = 0;

        }

    }

    }

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 3:17 PM   in reply to kglad

    are you sure you're using the code i suggested?  did you change anything??

     

    it looks like you're either not assigning alreadyExecuted=true or you're using:

     

    var alreadyExecuted:Boolean = false;

     

    instead of


    var alreadyExecuted:Boolean;

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 31, 2012 3:58 PM   in reply to Lenaped

    if that code is in a movieclip and that movieclip is re-initialized each time some other timeline (ie, the parent's timeline) returns to frame 1, that will cause the problem you're seeing.

     

    put that code (or, at least, the alreadyExecuted part of it) on your main timeline.

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 4, 2012 12:51 PM   in reply to Lenaped

    if your button is being reinitialized, you need to re-execute its listener code.  ie, remove  boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv); from the if(!alreadyExecuted) block:

     

     


    var alreadyExecuted:Boolean;

    boxButton.addEventListener(MouseEvent.CLICK, plyRndmMv);

     

    if(!alreadyExecuted){

    alreadyExecuted=true;

    var nums:Array = new Array();

    //1,2,3,4....

    for(var i:int=359; i<=365; i++){

    nums.push(i);

    }

     

    // for shuffling the array

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     

    // shuffle the array

    shuffle(nums);

    trace(nums);

     

    // set up the controls for the button

    var count:uint = 0;

     

     

     

    function plyRndmMv(evt:MouseEvent):void {

        MovieClip(root).gotoAndStop(nums[count]);

    trace(nums[count]);

        count++;

        if(count == 7){

            shuffle(nums);

            count = 0;

        }

    }

    }

     

     
    |
    Mark as:
  • kglad
    62,194 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 4, 2012 1:39 PM   in reply to Lenaped

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points