15 Replies Latest reply on Apr 6, 2017 8:55 AM by andyr15145041

    Random Frame

    emrek16464832

      I have identified a button.

      This button was going to the next frame.

      But I want a random frame.

      Would you like to see sample code.

      thank you so much

        • 1. Re: Random Frame
          Szalam Adobe Community Professional & MVP

          What software are you using?

          • 2. Re: Random Frame
            emrek16464832 Level 1

            Animate CC

             

            I have just started and I haven't any information about coding

            • 3. Re: Random Frame
              Szalam Adobe Community Professional & MVP

              I'm going to move your post over to the Animate forum. The Coding Corner forum is more for general coding stuff whereas the Animate forum folk will be able to offer you more specific help for Animate.

              • 4. Re: Random Frame
                Colin Holgate MVP & Adobe Community Professional

                Code to go to the next frame might look like this:

                 

                this.gotoAndStop(this.currentFrame+1);

                 

                Code to go a frame that is up to 10 frames ahead of where you are now would look like this:

                 

                this.gotoAndStop(this.currentFrame+Math.random()*10);

                 

                If you wanted to jump to a random frame starting at 15 and ending at 25, it would be like this:

                 

                this.gotoAndStop(15+Math.random()*10);

                 

                Math.random() gives you a value between 0 and almost 1. Multiplying that by 10 would make the number be from 0 to almost 10.

                • 6. Re: Random Frame
                  AMULI Level 4

                  A downloadable example of playing the different frames/states of a timeline in random order here :

                  https://forums.adobe.com/message/9309084#9309084

                  1 person found this helpful
                  • 7. Re: Random Frame
                    emrek16464832 Level 1

                    Thank you, AMULI but I am so bad in coding that I do not understand most of the writings.

                    I will explain my problem;

                    Adsız.jpg

                     

                    There were sample codes in Animate CC

                     

                    Random number generation code;

                     

                     

                    function fl_GenerateRandomNumber_4(limit:Number):Number

                    {    var randomNumber:Number = Math.floor(Math.random()*(limit+1));

                         return randomNumber;

                    }

                    trace(fl_GenerateRandomNumber_4(100));

                     

                     

                    AND

                     

                     

                    Next frame pass code;

                    newnumber.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);

                    function fl_ClickToGoToNextFrame(event:MouseEvent):void

                    {  nextFrame();   }

                     

                     

                     

                     

                    My question is;

                    How do I add this code any button?

                     

                    For example, I have a button which name is "newnumber"

                    and I want, when I click this button, it goes to random frame

                    • 8. Re: Random Frame
                      AMULI Level 4

                      emrek16464832  a écrit

                      when I click this button, it goes to random frame

                      I guess, it's not totally random ! Which range of frames ? Said differently, what are the minimum and maximum frame number your random computation should return ?

                      • 9. Re: Random Frame
                        AMULI Level 4

                        Say your button has instance name randomFrameBtn, add this code (adapt the minFrameNb and maxFrameNb to your timeline) :

                         

                        randomFrameBtn.addEventListener( MouseEvent.CLICK, goToRandomFrame);

                         

                        function goToRandomFrame( event:MouseEvent): void

                        {

                            var minFrameNb = 5;

                            var maxFrameNb = 20;

                            var randomFrameNb = minFrameNb +Math.floor( Math.random() * (maxFrameNb - minFrameNb + 1));

                            gotoAndStop( randomFrameNb);

                        }

                        2 people found this helpful
                        • 10. Re: Random Frame
                          emrek16464832 Level 1

                          Thank you very very very much .

                          It works like a machine

                          • 11. Re: Random Frame
                            AMULI Level 4

                            You're welcome, emrek16464832

                            • 12. Re: Random Frame
                              andyr15145041

                              I'm looking for almost exactly the same technique (just don't want to have to click anything for the function to work — want it to happen on load/refresh). That said, I'm simply trying to get this example to work, but to no avail. When I preview, I see nothing (but I should be seeing my button, at least, or numbers if it's working). Only way I see anything (which consists of the timeline simply looping through) is when I start to modify the code a bit to more closely match other examples I have.  Couple questions:

                              1. Does this code work for an HTML5 canvas file?

                              2. Is there a working file I can reference?

                              3. How would one modify this to randomly select a frame upon load/refresh (no need to click a button)?

                               

                              Really appreciate any insight/help. I think I'm close, and yet so far away!

                              • 13. Re: Random Frame
                                Colin Holgate MVP & Adobe Community Professional

                                What I said in message 4 would work from Canvas, and doesn't need a click:

                                 

                                this.gotoAndStop(15+Math.random()*10);

                                 

                                or:

                                 

                                this.gotoAndPlay(15+Math.random()*10);

                                 

                                if you want it to play from there. In my example it would jump to a random frame in the range of 15 to 24. Change the start frame number and the number of different frames, to match your case.

                                 

                                One thing to be aware of, if you are going to play sound it will fail. In most browsers HTML5 requires a user action before it allows sound to play. 

                                2 people found this helpful
                                • 14. Re: Random Frame
                                  Colin Holgate MVP & Adobe Community Professional

                                  Something else, if you don't see your button you should look at the developer Console, to see what error message it's giving you.

                                  1 person found this helpful
                                  • 15. Re: Random Frame
                                    andyr15145041 Level 1

                                    So easy … thank you! Works just as expected. My apologies for not inferring the code in message 4 to work for my needs.