2 Replies Latest reply on Aug 15, 2013 3:43 AM by Xoco

    Calling an array of childSymbols created in compositionReady

    Xoco Level 3

      Hi, I'm creating a little puzzle game for fun, I've created a symbol and this is cloned into an array with createChildSymbol 9 times (3x3 puzzle) in document.compositionReady event of Stage. Every element of array store data of id, row, and column.

       

      Then the first, I need to declare by code click function for each element of array or is enough with primary click event of parentSymbol? if is necessary how it's made?

       

      Once this, how I access to this array when user click on any childSymbol for get data stored? how do I know which childSymbol (of nine symbols) has been clicked?

       

      Thank you very much.

        • 1. Re: Calling an array of childSymbols created in compositionReady
          YOSHIOKA Ume Level 3

          Hi, Xoco.

           

          In the case of Edge Animate symbol, I think "declare each elem" is better.

          like this,

          //params
          var cells = [];
          var CELL_SIZE = 100;
          var CELL_PADDING = 10;
          var cellPrefixID = "cell_";
          var ROWS = COLUMNS = 3;
          var baseElem = sym.$("baseElem");  //parent element, if you use symbol:sym.$("baseElem").getSymbolElement();
          var size = CELL_SIZE + CELL_PADDING;
          
          //create child syms
          for(var i=0;i<9;i++){
            var cell = sym.createChildSymbol("Cell",baseElem);  //create child sym from sym named "Cell"
            var $cell = cell.getSymbolElement();
            $cell.css({
                position:"absolute",
                top:i%ROWS*size,
                left:(i/COLUMNS|0)*size
              })
              .attr("id",cellPrefixID+i)  //add ID(as you like)
              .data("index",i)            //add index(as you like)
              .click(onClickCell);        //add click event handler every symbol.getSymbolElement()
            cells.push(cell);
          }
          
          //event handler method for every child symbol
          function onClickCell(evt){
            //use "this" or "evt.currentTarget" for access to cell.
            console.log("id:",$(this).attr("id"));  //get @id
            console.log("index:",$(this).data("index")); //get data(index)
            console.log("sym from cells:",cells[$(this).data("index")]); //get symbol from array by data(index)
            cells[$(this).data("index")].play(0); //control symbol
          }
          

           

          thanks.

          Ume.

          • 2. Re: Calling an array of childSymbols created in compositionReady
            Xoco Level 3

            Correct!

             

            Thank you for your structured, commented and optimized code!