7 Replies Latest reply on Jul 18, 2013 1:47 PM by AMULI

    How do I get the index number of the clicked on item

    blvdgardens

      I created a symbol from a PNG image and then replicated it on the stage 9 times. I need to indentify the clicked on instance of the symbol (which I suppose is an element) and store its index number. Any ideas?

        • 1. Re: How do I get the index number of the clicked on item
          Departure Level 1

          Dont know much about index numbers of elements, but here is how i solved a dynamic symbol interactivity problem by creating an array and interacting with each copy of the symbol using the array index. I am actually interested to see other people's reply on your question and learn the index method as well. 

           

          var imageSymbolArray = [];

           

          for (var i = 0; i < 9; i++){

          // Create an instance element of a symbol as a child of the given parent element

          var mySymbolObject = sym.createChildSymbol("PNGSymbol", "stage");

          //set the value of a Symbol variable, this could function as the INDEX you are looking for.

          mySymbolObject.setVariable("id", i);

          imageSymbolArray.push(mySymbolObject);

          }

           

          $.each(imageSymbolArray, function( count, symbolInTheArray ){

          //create jQuery reference to the item

          var menuItem = $(symbolInTheArray);

          //create a jQuery reference to the DIV element inside the symbol.

          var menuElement = $(symbolInTheArray.getSymbolElement());

          //now you can bind interactivity to the menu items' DIVs

          menuElement.css({margin:'0 auto',position:'relative'});

          menuElement.bind ("click",function(){

          alert("You clicked a symbol with the index id of: "+symbolInTheArray.getVariable("id"))});

          });

          • 2. Re: How do I get the index number of the clicked on item
            AMULI Level 4

            Hi blvdgardens,

             

            You can define in the Stage.composition event handler a function callable from somewhere else in the Stage symbol :

             

            instanceNum = function( eRollover)

            {

            }

             

            This function receives the event object associated with the rollover of each of the instances. A symbol yellowRect is instanciated four times on the stage, with instance names yellowRect1, yellowRect2, yellowRect3 and yellowRect4.

             

            Each yellowRect<i>.mouseover event handler is a simple call to the global function :

             

            instanceNum( e);

             

            Inside the body of the function, eRollover.target gives a jQuery handle on the currently hovered over object.

             

            The latter is the rounded rectangle RoundRect in an instance yellowRect<i>, instanciated on Stage. Edge builds an id attribute reflecting this inclusion path. Use console.log() to display it in the console of your debugger to count the index of the instance number in that string : here the 17th character, so 16 (0-based).

             

            Extract that number via String.substr(). In the example, we display in a text "instance" followed by the number.

             

            instanceNum = function( eRollover)

            {

              // console.log( eRollover.target.getAttribute("id"));

              // => "Stage_yellowRect1_RoundRect", "Stage_yellowRect2_RoundRect", etc.

             

              var num = eRollover.target.getAttribute("id").substr(16,1);

              sym.$("numberTxt").html( "instance " +num);

            }

             

            Example downloadable here : https://app.box.com/s/zw9p0hmok4w5szv2yu2r

             

            Gil

            1 person found this helpful
            • 3. Re: How do I get the index number of the clicked on item
              blvdgardens Level 1

              Hi Gil,

               

              Thanks you gave me the info I needed. Here is my finished whack-a-mole script.

               

              //Stage's CompositionReady Code

               

              function randomNum(n) {

                   var rn = n;

               

                    do {

                          if (n != rn) {

                                 break;

                          }

                          rn = Math.floor(Math.random() * (9 - 1 + 1)) + 1

                    }

                   while (n == rn)

               

                   return rn

              }

               

               

              instanceNum = function(eClick) {

               

                var num = eClick.target.getAttribute("id").substr(10,1);

               

                //console.log(eClick.target.getAttribute("id"));

               

                //alert( "instance " +num);

               

                sym.$("mole" + num).hide();

               

                var rnum = randomNum(num);

               

                 sym.$("mole" + rnum).show();

              }

               

              //Code for the mole symbol Click function

               

              instanceNum(e);

              • 4. Re: How do I get the index number of the clicked on item
                blvdgardens Level 1

                Thanks for the help. Though the script didn't fully work for this game it did give me a better understanding of scripting in Edge.

                • 5. Re: How do I get the index number of the clicked on item
                  AMULI Level 4

                  it did give me a better understanding of scripting in Edge.

                   

                  That's fine. I feel very happy about it

                   

                  One step beyond !

                   

                        do {

                              if (n != rn) {

                                     break;

                              }

                              rn = Math.floor(Math.random() * (9 - 1 + 1)) + 1

                        }

                       while (n == rn)

                   

                  The inner test (in bold) is redundant and you can get rid of it :

                  - rn variable is assigned ;

                  - then (n == rn) is tested and if it is false (so if (n != rn)), the do /while loop is exited.

                   

                  Gil

                  1 person found this helpful
                  • 6. Re: How do I get the index number of the clicked on item
                    blvdgardens Level 1

                    Thanks again for the help and for showing how to simplify my script by removing reduncy.

                     

                    BTW, do you know of a good book that would explain in layman's terms Edge scripting and the API. Most books that I have seen only touch upon the scripting aspects. I see the API that Adobe has on their website but I only partly understand it.

                    • 7. Re: How do I get the index number of the clicked on item
                      AMULI Level 4

                      Hi blvdgardens,

                      a good book that would explain in layman's terms Edge scripting and the API. Most books that I have seen only touch upon the scripting aspects.

                       

                      That's true for the two books I used to practise :

                      http://forums.adobe.com/message/5452500#5452500

                       

                      I see the API that Adobe has on their website but I only partly understand it.

                       

                      I totally agree with you :

                      http://forums.adobe.com/message/5289867#5289867

                       

                      The book you're looking for (and that I would have been very happy to find !) is to be written .

                       

                      The best place to learn is here. So persevere and do not hesitate to ask any question, as you are certainly not the only one asking for that question and answers benefit to everyone. You're welcome !

                       

                      Gil