3 Replies Latest reply on Jul 30, 2010 12:32 AM by Balakrishnan V

    Get button label using 'getElementAt()'

    GVG-Vit

      Hello!

      I have 100 buttons in a container and I'm trying to get label of one of them. The problem: getElementAt() returns IVisualElement and it doesn't have label property. It returns other properties right.

      This line: if (cellContainer.getElementAt((row - 1)*10 + col-1).label == 'X')

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/mx" width="640" height="480" addedToStage="init()" backgroundColor="#CCAAAA">
           <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
           </fx:Declarations>     
           
           <fx:Script>
                <![CDATA[
                     import mx.core.IVisualElement;
                     import spark.components.Button;
      
                     private function init():void
                     {
                          for (var i:uint = 0; i < 100; i++)
                          {
                               var myCell:Button = new Button();
                               
                               myCell.width = 32;
                               myCell.height = 32;
                               myCell.label = 'O';
                               myCell.addEventListener(MouseEvent.CLICK, clickCell);
                               
                               cellContainer.addElement(myCell);
                          }
                     }
                     
                     public function checkForWinner():void
                     {
                          var sameMarkerInARow:uint = 0;
                          for (var row:uint = 1; row < 11; row++)
                          {
                               sameMarkerInARow = 0;
                               for (var col:uint = 1; col < 11; col++)
                               {
                                    if (cellContainer.getElementAt((row - 1)*10 + col-1).label == 'X')
                                    {
                                         sameMarkerInARow++;
                                    }else{
                                         sameMarkerInARow = 0;
                                    }     
                               }
                               if (sameMarkerInARow == 5){
                                    trace('WINRAR');
                                    break;
                               }
                          }
                     }
                     
                     private function clickCell(event:Event):void
                     {
                          event.currentTarget.label = 'X';
                          checkForWinner();
                     }
      
                ]]>
           </fx:Script>
           
           <s:TileGroup id="cellContainer" x="10" y="10" width="320" height="320" horizontalGap="0" verticalGap="0" rowHeight="32" columnWidth="32" requestedRowCount="10" requestedColumnCount="10">
           </s:TileGroup>
      </s:Application>
      

       

      Thanks!