3 Replies Latest reply on May 16, 2013 9:48 PM by billgregg

    Dynamic Symbol Reference Problem

    billgregg

      I have 24 separate symbols (or buttons) all grouped into a parent symbol. Each button sets the same variable (selectedyear) to the main timeline. Patterned after similar things I've done in Flash, the sequence of actions should be:

       

      (1) Send the previously selected button to its unselected state (or frame)

      (2) Set the variable selectedyear (on the main timeline) to its new value

      (3) Send the clicked button to its selected frame

       

      To do this the button reference has to be dynamic. The button instance names are "_1789", "_1793", "_1803", etc. Here's the code I've used, placed on an element (PNG) inside the button symbol:

       

      sym.getComposition().getStage().getSymbol("yearselector").getSymbol("_"+sym.getComposition ().getStage().selectedyear).stop("unselected");

      sym.getComposition().getStage().selectedyear = "1793";

      sym.stop("selected");

       

      It isn't working.

       

      This command does return the expected string, "_1793", for instance:

       

      console.log("_"+sym.getComposition().getStage().selectedyear);

       

      Hard-coding the target instance name however works where the dynamic, concatenated reference fails:

       

      sym.getComposition().getStage().getSymbol("yearselector").getSymbol("_1789").stop("unselec ted");

       

      I've also tried passing four-digit strings ("1789", etc.) to a function on the main timeline, which also fails unless the instance name is hard-coded:

       

      sym.changeyear = function(clickedyear){

          sym.getComposition().getStage().getSymbol("yearselector").getSymbol("_"+selectedyear).sto p("unselected");

          sym.getComposition().getStage().getSymbol("yearselector").getSymbol("_"+clickedyear).stop ("selected");

          selectedyear=clickedyear;

      }

       

      What am I doing wrong?

       

      Bill

        • 1. Re: Dynamic Symbol Reference Problem
          TimJaramillo Level 4

          As a simple hack/workaround, you could push all your buttons into an array initially. Then on click, iterate through the array, setting all button states to "off". Then, leveraging the click event to target the clicket button (e.target.id), set the clicked button to "on".

          1 person found this helpful
          • 2. Re: Dynamic Symbol Reference Problem
            a.jonesLargeAnimal

            I recommend you break this down to more steps and break into it to see if what you think is happening is actually happening.

             

            Try:

             

            var symbolName = "_"+sym.getComposition().getStage().selectedyear;

            console.log("Target symbol name: " + symbolName);

            var unselectSymbol = sym.getComposition().getStage().getSymbol("yearselector").getSymbol(symbolName);

            console.log("got symbol to unselect: " + (unselectedSymbol != null && unselectedSymbol != undefined));

            unselectSymbol.stop("unselected");

             

            sym.getComposition().getStage().selectedyear = "1793";

            sym.stop("selected");

             

            This should give you more information about where your code is failing

            1 person found this helpful
            • 3. Re: Dynamic Symbol Reference Problem
              billgregg Level 1

              Setting the symbol name to a variable before referencing the symbol worked, a.jones.LargeAnimal. Something about the concatenation within the object reference was breaking it. Instead of this function:

               

              sym.changeyear = function(clickedyear){

                   sym.getComposition().getStage().getSymbol("yearselector").getSymbol(" _"+selectedyear).stop("unselected");

                   sym.getComposition().getStage().getSymbol("yearselector").getSymbol(" _"+clickedyear).stop("selected");

                   selectedyear=clickedyear;

              }

               

              I now set the concatenated symbol name to a variable first as in your example:

               

              sym.changeyear = function(clickedyear){

                   sym.getComposition().getStage().getSymbol("yearselector").getSymbol(selectedyear).stop("u nselected");

                   clickedyear="_"+clickedyear;

                   sym.getComposition().getStage().getSymbol("yearselector").getSymbol(clickedyear).stop("se lected");

                   selectedyear=clickedyear;

              }

               

              I now recall running into something like this in ActionScript. Even though logically the code should have worked either way, on rare occasions I'd have to pull some operation out of a complex reference and set it to a variable first to make the reference work.

               

              Iterating through the array would probably have worked as well, but this solution took less effort. Thanks for the help, guys!