16 Replies Latest reply on Aug 19, 2013 1:53 PM by Cuwen

    Loop Random Frame Labels

    Cuwen Level 1

      Hello.  I recently got a code from David Stiller's blog that I really like.  It's about how to get a series of frame labels to play in random order without ever repeating.  That's awesome, and I understand most of the code.

       

      However, when all the frame labels have been 'played', I don't want it to just go and rest/stop on the last frame.  I want it to start the process all over again.  Is there a way to do this?  Here is the code I currently have:

       

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

      var labels:Array = ["a", "b", "c", "d", "e"];
      shuffle(labels);

      var currentLabel:Number = 0;
      function gotoNextLabel():Void {
        if (currentLabel < labels.length) {
          gotoAndPlay(labels[currentLabel]);
          currentLabel++;
        } else {
          stop();
        }
      }
      gotoNextLabel();

       

       

      Here is the blog entry I got it from:  http://www.quip.net/blog/2007/flash/how-to-jump-randomly-to-frame-labels-without-repeatsht tp://

       

      I'm guessing I have to replace 'stop' toward the end with something else, but what?  Repeat?

        • 1. Re: Loop Random Frame Labels
          kglad Adobe Community Professional & MVP

          use:

           

           

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

          var labels:Array = ["a", "b", "c", "d", "e"];
          shuffle(labels);

          var currentLabel:Number = 0;
          function gotoNextLabel():Void {
            if (currentLabel < labels.length) {
              gotoAndPlay(labels[currentLabel]);
              currentLabel++;
            } else {
          currentLabel = 0;

          shuffle(labels);  //<-if you want to re-randomize labels. otherwise, omit this line

          gotoNextLabel();

            }
          }
          gotoNextLabel();

           

           

          • 2. Re: Loop Random Frame Labels
            Cuwen Level 1

            Yep, I definitely want to re-shuffle.  So, I'll keep that line in.

             

            Thank you so much for your help!  I think it's cool that you're helping me with this b/c in David's blog, he said that the shuffle script came from you.  So thank you twice!

            • 3. Re: Loop Random Frame Labels
              Cuwen Level 1

              One more quick question.  Can I put this into another string of code?  I'm basically creating a video player, and I have it set up currently so that a listener object is created for each video I insert into a frame label.  When the video is done playing, I want it to do that shuffle command.  So, could I put all of the previous code into this:

               

               

              var listenerObject:Object = new Object();

              listenerObject.complete = function(eventObject:Object):Void {

               

              And then, after Void { I'd put in all the previous code.  Would that work?

              • 4. Re: Loop Random Frame Labels
                kglad Adobe Community Professional & MVP

                if you use an flvplayback component, you can use a "complete" listener:

                 

                var lo:Object={};

                lo.complete=gotoNextLabel;

                 

                flv_pb.addEventListener("complete",lo);

                 

                p.s.  that shuffle function is an implementation of the Fisher-Yates algorithm from 1938.

                1 person found this helpful
                • 5. Re: Loop Random Frame Labels
                  Cuwen Level 1

                  Ok.  Thank you very much.

                   

                  While I was waiting for a response, I did some more googling and tried some things out myself.  I got the player working, but it still is repeating some videos.  I have 12 videos that I need it to play, and before it plays all 12 videos in random order, it still plays some 3 or 4 times.  Do you know why this might be?  Should I start another thread about this specific issue?

                  • 6. Re: Loop Random Frame Labels
                    kglad Adobe Community Professional & MVP

                    you can simplify things (and eliminate errors) by using one flvplayback (eg, flv_pb) on 1 keyframe to play all 12 videos:

                     

                    var lo:Object={};

                    lo.complete=playNextF;

                    flv_pb.addEventListener("complete",lo);


                    var videos:Array = ["vid1.flv", "vid2.flv", etc];
                    shuffle(videos);

                    var index:Number = 0;
                    function playNextF():Void {
                      if (index < videos.length) {
                        flv_pb.contentPath = videos[index];
                        index++;
                      } else {
                    index = 0;

                    shuffle(videos);

                    playNextF();

                      }
                    }
                    playNextF();

                     

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

                    1 person found this helpful
                    • 7. Re: Loop Random Frame Labels
                      Cuwen Level 1

                      I would do that, but I need to use buttons in this player, too.  The 'invisible' buttons are above a thumbnail image, and when a person clicks on that image, it takes them to that "video", although it's really just taking them to the set of frames that I assigned the video to.

                      • 8. Re: Loop Random Frame Labels
                        kglad Adobe Community Professional & MVP

                        have the button handlers assign the appropriate index and call playNextF().

                        1 person found this helpful
                        • 9. Re: Loop Random Frame Labels
                          Cuwen Level 1

                          Wow.  That might be a little too advanced for me.  I really appreciate your help, though!  If only the code I used from earlier with your code didn't play some of the same videos several times, it'd be perfect!  I don't know if the code won't work, won't do random frame labels without going back to some a few times, b/c I'm using flvs or not.

                          • 10. Re: Loop Random Frame Labels
                            kglad Adobe Community Professional & MVP

                            there's not much to that.

                             

                            if button0 plays the first video in that array and button1 plays the next etc, use:

                             

                            for(var i:Number=0;i<videos.length;i++){

                            this["button"+i].onRelease=function(){

                            index=Number(this._name.substring(6));

                            playNextF();

                            }

                            }

                            1 person found this helpful
                            • 11. Re: Loop Random Frame Labels
                              Cuwen Level 1

                              Does it matter that my buttons are nested in a movie clip?  I had to do that b/c they're in a scrollpane.  And for the number '6' in substring, do I replace the 6 ith how many buttons I have (12)?

                              • 12. Re: Loop Random Frame Labels
                                kglad Adobe Community Professional & MVP

                                how do you reference your buttons?

                                • 13. Re: Loop Random Frame Labels
                                  Cuwen Level 1

                                  Okay, I know this is really bad practice, and I usually try to avoid it, but I put the code right on the buttons.  That way, I don't have to reference the button in my main code.  I googled for hours trying to find out how I could 'call to' the buttons through the mc in my main script, but nothing worked for me.  I tried multiple _root codes and other codes, nothing worked.

                                  • 14. Re: Loop Random Frame Labels
                                    Cuwen Level 1

                                    Oh, and I'm trying to figure out how to do a .flv array.  I thought it'd be just like a swf array, but I was very wrong.  I've done plenty of swf arrays before, but have never done an flv array.  I can load an external flv into a video object just fine, but when it comes to setting up an array, I can't quite figure it out.

                                    • 15. Re: Loop Random Frame Labels
                                      kglad Adobe Community Professional & MVP

                                      you should remove your buttons from the scrollpane unless there's no other way to display all your buttons.  that's going to make your coding more difficult.

                                       

                                      click on a button to select it.  in the properties panel assing an instance name (eg, button0).  do the same for each button, using different names for each.

                                       

                                      if you choose names wisely they'll be related to the position in the videos array (shown in message 5) corresponding to the video that should play when that button is clicked.

                                       

                                      message 5 shows how to assign your flvs to an array.

                                       

                                      how you reference your buttons depends on whether you can get them out of that scrollpane.

                                      • 16. Re: Loop Random Frame Labels
                                        Cuwen Level 1

                                        Thank you so much for all of your help.   I'm going to save this discussion and reference back to it when I have more time to try this project from scratch and use your ideas.  If I get everything to work, I'll let you know!  It's always good to learn about this type of stuff, and I like becoming a better programmer.  I just usually have to come back to old discussions and work on the new codes and practices when I have time.  For now, I'll mark your original answer as correct.  Thank you so much again!