6 Replies Latest reply on Oct 8, 2013 3:43 AM by PEPPERFACE

    Play Random Symbols When Button Is Clicked

    PEPPERFACE Level 1

      First off, I do apologise for not being able to make use of the answers that have already been proffered in response to similar questions to this, but none of the posts that I've pored over seem to cover what I'm after (unless, it's a matter of my failing to understand the concept... which may well be the case! Haha!)

       

      Anyway, I have a dozen or so animated symbols on the stage and I'd like a randomly called one to play each time users click a button.

       

      Any advice, or directions to any tutes out there, will be very gratefully received.

       

      Cheers in advance

       

      Frannie x

        • 1. Re: Play Random Symbols When Button Is Clicked
          resdesign Adobe Community Professional & MVP

          hey Frannie,

           

          Try something like this:

           

          var mySymbols = ['Symbol_1','Symbol_2','Symbol_3','Symbol_4'];

          i=0;

          sym.$('btn').click(function(){

          i++;

          var myVar = Math.floor((Math.random()*4));

          sym.getSymbol(mySymbols[myVar]).play();

          });

          1 person found this helpful
          • 2. Re: Play Random Symbols When Button Is Clicked
            jbowden Adobe Employee

            Here's something a little more concise that may help or otherwise be of interest:

             

            var childSymbols = sym.getChildSymbols();

            var r = Math.floor((Math.random()*4));

             

            childSymbols[r].play();

             

            Add that code to your click handler. Only minor tweaks are needed if the button and symbol instances are not at the same symbol level.

             

            hth,

            Joe

            • 3. Re: Play Random Symbols When Button Is Clicked
              resdesign Adobe Community Professional & MVP

              Nice Joe!

               

              A clarification for the API:

              I think that you need to add the curly brackets  in the example because of course without them, the example would not work.

               

              API example under getChildSymbols (missing brackets added below).

              var childSymbols = sym.getChildSymbols();

              for(var i=0; i<childSymbols.length; i++) { // Return the number of direct children   

              childSymbols[i].stop();           // Stop all of the children

              };

              • 4. Re: Play Random Symbols When Button Is Clicked
                jbowden Adobe Employee

                resdesign wrote:

                 

                A clarification for the API:

                I think that you need to add the curly brackets  in the example because of course without them, the example would not work.

                 

                API example under getChildSymbols (missing brackets added below).

                var childSymbols = sym.getChildSymbols();

                for(var i=0; i<childSymbols.length; i++) { // Return the number of direct children   

                childSymbols[i].stop();           // Stop all of the children

                };

                 

                Hi Marie,

                 

                Actually, using opening/closing curly braces for a loop are not required, at least not for the example given:

                 

                var childSymbols = sym.getChildSymbols();

                for(var i=0; i<childSymbols.length; i++) // Return the number of direct children   

                childSymbols[i].stop(); // Stop all of the children

                 

                As a matter of coding style, I'd probably add them myself for clarity - but the above code will work without the curly braces for those who prefer not to use them.

                1 person found this helpful
                • 5. Re: Play Random Symbols When Button Is Clicked
                  resdesign Adobe Community Professional & MVP

                  i have always used curly brackets. I even did not know you could skip them. Thanks for the information!

                  • 6. Re: Play Random Symbols When Button Is Clicked
                    PEPPERFACE Level 1

                    Huge thanks, both of you... top advice and wholly implementable!

                     

                    Cheers!

                     

                    Frannie x