02 October 2010

Embed assets on Actionscript projects

An Actionscript project is, like its name involve, a pure actionscript project.
No .fla file is needed.....so how do you create your asset ?

Several ways
- draw it by code using graphics
- load external file (png, swf, etc...)
- embed the file as a class (Flash Builder only)

The cons of embedding is extra bytes the Flex SDK use to handle embed data throught SpriteAsset, MovieClipAsset,etc...

The basic way to embed asset is

private var png:Class;
var tt:BitmapAsset = BitmapAsset( new png() );
addChild( tt );

//for SWF asset
[Embed(source="./assets/assets.swf", symbol="Bulle")]
private var bulle:Class;
var tt:SpriteAsset = SpriteAsset( new bulle() );
addChild( tt );

so, what if you want if bulle is in fact a Bulle class not the raw Class ?
Using this way, you can't do anything...
you can't cast, you can't access to Bulle properties and functions...nothing!
bulle is a closed asset

Thanks to a comment on a post of Bit-101, a better way to embed exists :

import flash.display.Sprite;

[Embed(source="./assets/assets.swf", symbol="Bulle")]
public class Bulle extends Sprite
public function Bulle()
trace("Bulle created");

This method is, for me, a great value because the class code isn't included in the assets.swf file !
It's mean
- my graphist just needs to create her asset and assign it a classname
- the class doesn't need to be coded before the asset to be created (and stop the fight "code or gfx first ?")
- the asset could be used on several games with different code (!marvelous!)
- any extended classes will inherit the embed asset
- no need for SpriteAsset, MovieClipAsset,etc... ex: with the 'bulle' asset below I produce a 20708bytes file using standard embed and a 10586bytes file using optimized one (10kb won!)

Of course, you could do the same for PNG/JPG/GIF
public class Png extends Bitmap{}

and MP3
public class Beep extends Sound{}

No comments: