5 Replies Latest reply on Jan 9, 2015 10:46 AM by justincode

    Problem with my code: Animation using variables and if / else statments.

    justincode Level 1

      Hi All..

       

      Before i get into my problem i will briefly discuss what it is im trying to achieve

       

      The user inputs a number, the possible numbers that can be entered are 0,1,2,3,4 and 5

      Once the number is submitted a function is run which stores the user input and starts the animation

      Depending on the number the user inputs the slider moves accordingly, much like a scoring system... If the user inputs 0 then the animation does not move, if the user inputs 1 then the animation moves a little and if the user inputs 5 then the animation moves all the way.

       

       

      I have a trimmed down demo file that i have created.

       

      Download: HERE

       

      In this demo you will see that in composition ready you can set a variable called "division" to either 0,1,2,3,4, or 5


      Currently the variable is set at 0 (please leave it at zero because its only when it is set to zero that there is a problem)


      Then please run the page in a browser and you have the ability to click a box, once you click this box the "division" variable is passed to a symbol called "numbers" using set.variable. Then the slider animation checks the "numbers" data at trigger points along the way using if else statments to decide if the animation should stop or continue.


      The problem i have is that if the variable is set to 0 the animation should not run, but you will notice that:


      1st click: Animation does not run (This is correct because the "division" variable is set to 0).

      2nd click: Animation ignores "division" variable and runs past all triggers until the end.

      3rd click: Animation does not run

      4th click: Animation runs

      and so on in this fashion.


      Feel free to whack F12 to view the console logging i have put in place.


      Please if anybody here can shed some light i would be very greatfull!!!


      Much love, happy new year!



      Justin.

        • 1. Re: Problem with my code: Animation using variables and if / else statments.
          justincode Level 1

          Anybody had a chance to look at this..

           

          many thanks!

          • 2. Re: Problem with my code: Animation using variables and if / else statments.
            resdesign Adobe Community Professional & MVP

            Hi there. I was on vacation and did not get to look at it yet. I will try to give you a corrected file soon.

            • 3. Re: Problem with my code: Animation using variables and if / else statments.
              resdesign Adobe Community Professional & MVP

              I have something working here but this might not exactly be what you want. I commented out your code on the Rectangle and your code in compostionReady and added this code below. I tried all the numbers and they work. However I am not sure this would work with your original intent.

               

              var inputStops = ['zero','one','two','three','four','end']

              var division = 2;

              sym.$('Rectangle').bind('click',function(){

               

               

              sym.$("average").html(division);

                inputStops = division;

                console.log("average :"+division);

               

               

              sym.getSymbol("slider").setVariable("numbers", (division));

              sym.getSymbol("slider").play(division);

              });

              • 4. Re: Re: Problem with my code: Animation using variables and if / else statments.
                justincode Level 1

                Hope you had a fab vacation!!!!!

                 

                So, i have put your code in the rectangle symbol.

                 

                What happens for me:

                 

                Division is set to 2.

                FIRST CLICK: Nothing happens

                SECOND CLICK:Slider moves to position 2 (Correct)

                ALL CLICKS AFTER: Slider moves to position 2 (Correct)

                So apart from the first click not initiating the code correctly all is fine here

                 

                 

                But.....

                 

                Division is set to 0.

                FIRST CLICK: Nothing happens

                SECOND CLICK:Nothing happens (but the console logs that Zero has occured, meaning the animation checked the division value and stopped accordingly) (Correct)

                ALL CLICKS AFTER: Animation plays all the way

                This means that your code is exhibiting the same issue as mine. (Incorrect)

                 

                So, where does the problem lye..

                 

                Is it my IF / ELSE statement causing a problem

                 

                var loop = sym.getVariable("numbers");
                console.log("ZERO");
                
                if (loop ==0) {
                    sym.stop("zero");
                    }
                else {
                    sym.play("zero");
                    }
                

                This looks fine to me...

                 

                Im stumped....

                 

                Any ideas, friends of the code..?

                 

                Justin.

                • 5. Re: Problem with my code: Animation using variables and if / else statments.
                  justincode Level 1

                  Had another look at it,

                   

                  Not sure if this is a work around or if it is the correct way of doing it (would love to know)

                   

                  Instead of me starting the animation at the ZERO label I have added a START label with no code in it and no animation until it reaches the ZERO label . Now when the button is clicked it begins at START label, then it gets to the ZERO label and executes the code within. Everything is working fine now..

                   

                  Not sure how to explain it but the problem seems to be that there needs to be a position BEFORE the code is run in the ZERO label,

                   

                  either way.. if anyone wants to see the working example..

                   

                  Click HERE