apache-flexactionscript-3embedded-resourcedynamic-class

Creating a class dynamically in actionscript


Let me present the problem first. I need to load all images that I have used in my project externally without embedding. The images are present either in skins or as icons for items in trees. I came across the IconUtility class here I was able to modify it and use it for trees but the problem is we cannot use iconutility for the same component to set 2 different skins (like for a button - upskin downskin). I was not able to think of a workaround with iconutility. Is it possible to simulate embed and create a class dynamically and return the class at runtime?


Solution

  • the most simple thing to accomplish these things is to create one/multiple swfs containing your assets, load it, and then pull out the classes from there (i.e. from the loaded swfs application domain) ...

    there are multiple solutions to that:

    hope that helps

    edit: the second solution is about creating assets on the server, using a suitable tool ... or coding the tool yourself, but that was more of a joke ... :) ... i realized, the link to the flex compiler was wrong ... the idea would be to simply plug it into your web server, and then have it compile some ActionScript, that'll do the embed ... so you would generate one ActionScript file like this:

    package {
        import flash.display.Sprite;
        import flash.utils.describeType;
        public class Assets extends Sprite {
            [Embed(source='asset_1_Location')]
            public static var asset_1:Class;
            [Embed(source='asset_2_Location')]
            public static var asset_2:Class;
            ...
            [Embed(source='asset_n_Location')]
            public static var asset_n:Class;    
            public function Assets() { }
            public static function getAll():Object {
                var ret:Object = { };
                for each (var x:XML in describeType(Assets).variable.(@type=="Class")) {
                    var name:String =  x.@name;
                    ret[name] = Assets[name];
                }
                return ret;
            }
        }
    }
    

    then have the flex compiler compile it ... when loaded, extract data with LoaderInfo::applicationDomain.getDefinition("Assets").getAll(), which will give you a key-value map with all the classes needed ...

    with other tools, it would work differently, but i hope this clarifies, how it should work ...

    on the server, you need a service, that will build these asset swfs for you, and cache them ... so you send up some POST request, for example with a JSON array of files/images you need, and it'll give you back the swf ... the server should do appropriate caching of course ...

    hope, now it helps ... :)

    greetz

    back2dos