2 Replies Latest reply on Jan 13, 2010 12:28 PM by pauland

    Targeting objects made at runtime

    Post726

      I have placed 500 points on my flex application and want to be able to control them each individually. here is the code that I have so far:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
           
           <mx:Script>
                <![CDATA[
                     import mx.core.UIComponent;
                     import flash.display.Sprite;
                     import mx.effects.easing.Linear
                     
                     private var arr:Array = new Array;
                     
                     private function init():void
                     {
                          for(var i:int = 0; i < 500; i++)
                          {
                               arr[i] = i/2;
                          }
                          
                          var num:int = arr.length;
                          
                          for(var j:int = 0; j < num; j++)
                          {
                               var point:Shape = new Shape();
                               point.graphics.beginFill(0x000000);
                               point.graphics.drawCircle(j, arr[j], 1);
                               point.graphics.endFill();
                               
                               var ui:UIComponent = new UIComponent;
                               ui.addChild(point);
                               ui.id = "p"+j;
                               
                               this.addChild(ui);
                          }
                          
                     }
                ]]>
           </mx:Script>
           
      </mx:Application>
      

       

      When I try to target any of the points or UIComponents, Flex builder cannot see them because they will be created at run time. How can I target them with something like an animiateProperty tag?

       

      Ex.

          <mx:AnimateProperty id="moveY" target="{p499}" property="y" toValue="0" fromValue="250" duration="2000" easingFunction="Linear.easeNone"/>
      
      
        • 1. Re: Targeting objects made at runtime
          Gregory Lafrance Level 6

          This code provides a workaround:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
            layout="absolute" creationComplete="init();">
            <mx:Script>
              <![CDATA[
                import mx.core.UIComponent;
                import flash.display.Sprite;
                import mx.effects.easing.Linear
                private var arr:Array = new Array;
                private function init():void{
                  for(var i:int = 0; i < 500; i++){
                    arr[i] = i/2;
                  }
                  var num:int = arr.length;
                  for(var j:int = 0; j < num; j++){
                    var point:Shape = new Shape();
                    point.graphics.beginFill(0x000000);
                    point.graphics.drawCircle(j, arr[j], 1);
                    point.graphics.endFill();
                    var ui:UIComponent = new UIComponent;
                    ui.addChild(point);
                    ui.name = "p"+j;
                    this.addChild(ui);
                  }
                }
              ]]>
            </mx:Script>
            <mx:AnimateProperty id="moveY" property="y" toValue="0" 
              fromValue="250" duration="2000" easingFunction="Linear.easeNone"/>
            <mx:Button click="moveY.play([this.getChildByName('p499')]);"/>
          </mx:Application>
          


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          1 person found this helpful
          • 2. Re: Targeting objects made at runtime
            pauland Level 4
             private var arr:Array = new Array;

             

            add

             

            private var pArr:Array = new Array();

             

             

             var point:Shape = new Shape();

            add

             

            pArr.push(point);

             

             <mx:AnimateProperty id="moveY" target="{p499}" property="y" toValue="0" fromValue="250" duration="2000" easingFunction="Linear.easeNone"/>

             

            Instead of {p499} use "{pArr[499]}"

             

            Paul