2 Replies Latest reply on Apr 19, 2010 11:53 PM by langzifan

    New way to do Dynamic Icons in Flex


      Hi All,


      I figured out another way to make dynamic icons in Flex:


      Flex Code:


      In the container mxml:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
      paddingLeft="2" paddingRight="2" horizontalGap="2">
         import mx.core.Application;
        [Embed(source="common/swf/dynamicIcon.swf") ]
        public var isDynamicIcon:Class;
      <mx:Image id="ico_mag_sm" source="{isDynamicIcon}" />
      <mx:Spacer width="1"/>
      <mx:Text id="_name" text="{data.ApplicationName}"
      paddingLeft="5" selectable="false"/>       


      In the Applicaton mxml:


          i// ActionScript file
              import inc.ApplicationProperties;
              //NOTE: do NOT type the commonPath variable. Typing it to String breaks the code.
               public var commonPath = ApplicationProperties.COMMON_PATH;//this is the url to the server where the icons are located.




      dynamicIcon.swf AS 3.0 code:


      var iconFolder:String = "inc/icons/";
      var  parentArray:Array = new Array();

      //find the highest display container
      function getIconName(myObject){
            while (myObject.parent){
               myObject = myObject.parent;
                var topParent = myObject;
               var objectName:String =  myObject.name;

          // for some reason I can't just set the iconName equal to the myObject.name. The only way I could get it to work is to push

         //it onto an array first and then access it in the array.
            for(var i:Number=0;i<parentArray.length; i++){
                   var  iconName:String = parentArray[i];
           var myURL:String =  topParent.getChildAt(0).application["commonPath"];
           var  loadPath:String = myURL+iconFolder+iconName+".png";
           var loadit =  new Loader();
           loadit.load(new  URLRequest(loadPath));


      BUT, I am new to Flex and AS 3.0 and I know this code is uglier than it needs to be and have the following two issues.


      1) In the AS 3.0 code, to determine the parent application, I should be able to just reference the parent application directly rather than have to iterate through .parent.  Since I am using the Image tag and not using a loader, the only way I could figure out how is to iterate thought .parent. Is there a more direct way to get the parent or more specifically, the COMMON_PATH attribute of the ApplicationProperties class?


      2) In the AS 3.0 code, for some reason, I could not just stick the retrieved myObject.name in into the iconName variable and add it to the URL string. Just out of desperation, I pushed it onto an array first and then accessed it and it worked.


      Anyone got any ideas on either or both item 1 or 2 above?