GhostWire Studios - Flash/Flex UI Components Development And Consulting Services
Quality User Interface Controls For Flash Application DevelopmentAspireUI Components

Aug 16 2011

[AS3] Referencing Embedded Assets Of Another Class Using Underscore

Published by at 10:18 am under Flash,Flash AS3,Tips

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.

pixelstats trackingpixel
Share or Bookmark This Post:
  • StumbleUpon
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • Yahoo! Buzz
  • Netvibes
  • NewsVine
  • Reddit
  • Slashdot
  • Technorati
  • BlinkList
  • Mixx
  • Diigo
  • Faves
  • Suggest to Techmeme via Twitter
  • Twitter

Other Posts You Might Enjoy:

       

One response so far

One Response to “[AS3] Referencing Embedded Assets Of Another Class Using Underscore”

  1. John Don 17 Aug 2011 at 1:17 am

    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?

Comments RSS

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word