2 Replies Latest reply on Mar 11, 2013 7:01 PM by grouchou

    Trigger random part of an animation

    grouchou Level 1

      Hi everyone,

       

      I have an animation with 5 different parts. I would like to trigger a random part when clicking on a button.

      In order to do that I put labels on all parts (play1,...,play5). Then I put a code on the button on the click event :

       

      var RandomNb=Math.floor(Math.random()*5)

      if(RandomNb=1){sym.play("play1");}

       

      It is just a simple test but it does not work : the animation play1 is always played no matter the value of RandomNb.

      I obviously make something wrong but I don't know what...

       

      Thanks in advance for your help !

        • 1. Re: Trigger random part of an animation
          elainecc Adobe Employee

          Hi, grouchou-

           

          There are a couple of issues with your code.  First off, Math.random() will give you a number between 0 and 1.  I'd suggest you multiply by 100, apply Math.floor(), then modulo 5 to get a random number between 0 and 4.  Secondly, you will need to use the comparison == instead of the assignment = in order to get your if statement to work.  So (if RandomNb == 1) {...}.  Right now, you're doing an assignment in the if statement, which always returns true after it finishes.

           

          Hope that helps,

           

          -Elaine

          • 2. Re: Trigger random part of an animation
            grouchou Level 1

            Thanks a lot Elaine !

            It worked. It was the == thing that resolved the case.

            And just so you know the formula RandomNb=Math.floor(Math.random()*5); works great to generate an integer betwwen 0 and 4. (Don't ask me why, I don't know anything about javascript!)

             

            Thanks again !