4 Replies Latest reply on Jul 18, 2013 8:04 AM by chino_10

    How to use the array elements

    chino_10

      Hi, i'm trying to automate a questionary using array.

       

      // In the compositionReady for the stage

       

       

      sym.actual = 0;

      sym.arr = [ "Hi","Love","You" ]              // This is my question array.

       

      sym.checkArr = function(){                  // this the function that i use to check whis question of the array i'm going to use.

                sym.Question = arr[actual];      // i want to use the value "Hi", and then increase my "actual" var and then use the value "Love"

      }

       

       

      // In my symbol timeline i use a trigger, in that trigger i use

       

       

      sym.stop();

      sym.quest = sym.getComposition().getStage().Question;         // I'm getting the Question value;

      sym.$("Ask").html(quest );                                                         //"Ask" is an empty textfield;

       

      sym.getComposition().getStage().actual ++;                    // Increase "actual" for the nex time.

       

       

      But the code doesn't work, i think the problem is the way i use the array index.

       

      Please help!

       

      Thanks =)

        • 1. Re: How to use the array elements
          jbowden Adobe Employee

          Hi there,

           

          One problem I see is you have a function (sym.checkArr) defined in your compositionReady handler, but it never actually gets called. Another problem I see is too many unnecessary sym variables. I think you could do this much simpler this way:

           

          // in compositionReady event

          sym.actual = 0;                         // initialize the symbol variable for indexing the array

          sym.arr = ["Hi","Love","You"]  // define the symbol array

           

          // in timeline trigger

          sym.stop();

          var quest = sym.arr[sym.actual];  // get the symbol array element at index. Note I'm assigning it to a local variable here.   

          sym.$("Ask").html(quest);            // change the text to the value of the local "quest" variable               

          sym.actual++;                             // increment sym.actual variable for the next time

           

          Give that a try, and let us know if it works.

           

          hth,

          Joe

          1 person found this helpful
          • 2. Re: How to use the array elements
            chino_10 Level 1

            Hi it work's perfectly,

             

            but i have a few question about your code and i hope you can answer it because i want lo learn more.

             

            in te timeline trigger  why did you use a local variable?

             

            and if you are calling the global variable "actual" from de compositionReady handler why didn'y you call it with all the path like "sym.getComposition().getStage().actual ++;"?

             

            You're great dude!

             

            Thank you very much

            • 3. Re: How to use the array elements
              jbowden Adobe Employee

              Hi there,

              chino_10 wrote:

               

              in te timeline trigger  why did you use a local variable?

               

               

              It's good practice to use local variables whenever possible. The quest variable will only apply to that trigger, and it won't carry over to another trigger in the same timeline. So when using a trigger to set the HTML of a text element in the same timeline, I find it cleaner to assign a new HTML value from a local variable.

               

              chino_10 wrote:

               

              and if you are calling the global variable "actual" from de compositionReady handler why didn'y you call it with all the path like "sym.getComposition().getStage().actual ++;"?

               

               

              I think maybe I didn't express it as clearly as I could have. When you declare sym.actual in compositionReady, you actually have declared a variable scoped to the main stage symbol - it's not a global variable. To declare a global variable (which is not always best practice), your code in compositionReady would be:

               

              actual = 0;

               

              This actual variable would now be accessible from any element/symbol in the composition simply by assigning a value to actual. You may see how this could quickly get messy.

               

              So getting back to variable scope: if you declare a symbol variable using sym.varName, then any element in the same scope can call it using sym.varName. So your main timeline triggers can call it without using the full addressing of sym.getComposition().getStage().actual++. Instead, you could just use sym.actual++. Less code, and easier to read. But if you wanted to call it from another symbol (outside of the scope of the main stage), then you'd have to use the full addressing to actually address the variable, i.e., sym.getComposition().getStage().actual ++.

               

              hth,

              Joe

              • 4. Re: How to use the array elements
                chino_10 Level 1

                That's perfect!

                 

                I understand everything, thanks for your time and knowledge.

                I'm very greatful, you helpe a lot.