020 7920 7120 | public@publicreative.com

July 13th, 2009

Embedding asset classes with AS3.

For many projects these days I find that just about everything is created in code except for a handful of elements that need to be created in the Flash IDE. In these cases on small projects I recently discovered this method of embedding each asset class within a swf file very handy.

You can define any stage instances within the asset as public variables in the defining class. This will mean everything is strongly typed and nice and tidy. Just make sure “Automatically declare stage instances” is turned off in your ActionScript 3 settings. It’s probably easiest if I show you what I mean.

package com.publicreative.assets 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
 
       // In the assets.fla library you set the linkage class to the symbol definition here
	[Embed(source="/assets.swf", symbol="DemoTextField")]
	public class DemoTextField extends MovieClip
	{
               // there is a textfield on the stage with this instance name
		public var field_txt:TextField;
		public function DemoTextField() 
		{
		}
	}
}

So now when you want a new DemoTextField… you just need to go.

import com.publicreative.assets.DemoTextField;
var _demoTF:DemoTextField = new DemoTextField();
_demoTF.field_txt.text = “some text”;

You can have as many asset classes as you need all published from the same .fla file. Clearly this will get out of hand on a larger project and when assets need to be loaded dynamically. But on smaller projects I think this is an excellent tool to use.

Tell us what you think!