Aug 16 2011
[AS3] Referencing Embedded Assets Of Another Class Using Underscore
This is rather unorthodox but it works. In Flash, when you embed an image into the SWF using a static class member of a class, a new class with the name of CLASSNAME_MEMBERNAME is actually created internally. Using flash.utils.getDefinitionByName(), you can access this class, regardless of whether the static member was declared as public or private. This makes it possible to make a very simple embedded image assets manager.
Consider the following:
[Embed(source="assets/background.png")] private static const BACKGROUNDIMAGE:Class;
Let’s say that is done within a class named AssetsManager. Then, internally, we actually have a class named “AssetsManager_BACKGROUNDIMAGE” created. The name that comes after the underscore matches whatever you have given to that static member.
We can then write a generic handler function to grab the associated Bitmap image, given the asset name:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static function GetImage(name:String):Bitmap { name = name.toUpperCase(); try { var cls:Class = getDefinitionByName("AssetsManager_" + name) as Class; return new cls() as Bitmap; } catch (e:Error) { } // ** else return a default 16x16 opaque white square bitmap ** return new Bitmap(new BitmapData(16,16)); } |
Line 3: This is optional, but it is a simple way to eliminate possible naming confusion.
Line 4: Use a try-catch block, because getDefinitionByName() expects the class to exist, but it is possible that during development we have not embedded the assets yet, and this method is crafted to return a default 16×16 opaque white square where the asset is missing.
Line 6: Here it is, concatenating the helper class name, an underscore, and the given name parameter, we get the class name of the embedded bitmap asset.
Line 7: If the class/asset exists, this line will be executed (instantiating and returning the requested Bitmap object), otherwise the try-catch block kicks in.
Example complete class code below:
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.utils.getDefinitionByName; public class AssetsManager { // ** START EMBED ** [Embed(source="assets/background.png")] private static const BACKGROUNDIMAGE:Class; [Embed(source="assets/iconfileopen.png")] private static const ICONFILEOPEN:Class; [Embed(source="assets/iconfileclose.png")] private static const ICONFILECLOSE:Class; // ** embed other assets as required ** // ** END EMBED ** /** * Returns a Bitmap display object identified by the specified name. * @param The name of the bitmap asset to return. This name must match one of the * @return A Bitmap display object. */ public static function GetImage(name:String):Bitmap { name = name.toUpperCase(); try { var cls:Class = getDefinitionByName("AssetsManager_" + name) as Class; return new cls() as Bitmap; } catch (e:Error) { } // ** else return a default 16x16 opaque white square bitmap ** return new Bitmap(new BitmapData(16,16)); } } }
The above makes it easy to add more assets later on, compared to just accessing the public static members directly. What is more, if you are compiling a library of embedded images and distributing it as a SWC, this makes it so much easier to modify/update the library, and for other collaborators to use the assets.
Other Posts You Might Enjoy:
- uiImage: Using Embedded And/Or Dynamically Generated BitmapData
- [AS3] Serializing A Bundle Of Bitmaps As Data Objects
- [AS3] Hiding Assets And Code By Embedding SWF Within Another SWF
- Compiling embedded font SWFs with the Flex compiler for use with the uiFonts manager
- [AS3] Applying ROT128 Encryption On Embedded/Module SWFs








Thanks for sharing this interesting undocumented quirk, but I fail to see how:
AssetsManager.getImage(“BACKGROUNDIMAGE”);
…is any better than the old way (public static const) :
AssetsManager.BACKGROUNDIMAGE;
Infact, surely this way would be worse; because your IDE (in my case FlashDevelop) wouldn’t be able to give you an autocomplete list of all your assets when you’re entering the name string?