<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NAS.C.O.D.E &#187; Asset</title>
	<atom:link href="http://nascode.com/tag/asset/feed/" rel="self" type="application/rss+xml" />
	<link>http://nascode.com</link>
	<description>Visualization is Fun!</description>
	<lastBuildDate>Sun, 25 Apr 2010 07:35:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Embedding Asset at Compile Time in Pure AS3 Project</title>
		<link>http://nascode.com/2010/02/01/embedding-asset-at-compile-time-in-pure-as3-project/</link>
		<comments>http://nascode.com/2010/02/01/embedding-asset-at-compile-time-in-pure-as3-project/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 15:38:32 +0000</pubDate>
		<dc:creator>nascode</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Asset]]></category>
		<category><![CDATA[Embedding]]></category>

		<guid isPermaLink="false">http://nascode.com/?p=8</guid>
		<description><![CDATA[When we are doing pure AS3 project especially game, somehow we need to put our game art asset into the game. We can achieve this using either runtime asset loading or embedding asset at compile time. If we are developing under Flash IDE, then embedding asset can be done automatically. Just import the assets to library, <a href="http://nascode.com/2010/02/01/embedding-asset-at-compile-time-in-pure-as3-project/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>When we are doing pure AS3 project especially game, somehow we need to put our game art asset into the game. We can achieve this using either runtime asset loading or embedding asset at compile time. If we are developing under Flash IDE, then embedding asset can be done automatically. Just import the assets to library, and it will be embedded next time you publish your SWF. Even better, Flash IDE has many options to compress the embedded images or resampling audio in the library. However, these options are some reasons why on large size FLA project, Flash IDE publish SWF so slow. Compressing image and resampling audio takes a while, plus Flash IDE compiler is not fast enough.</p>
<p>Some Flashers nowadays prefer to embed asset manually without Flash IDE in order to avoid frustation when publishing swf in a large project, especially when we should test our code a lot. Using Flex Builder/FDT/Flashdevelop for example will fasten compile time, debugging support, and also offer excellent code-hinting. But unfortunately, embedding asset is not as easy as using Flash IDE, now we must write more code to achieve it.</p>
<p>When building large application, it is often beneficial when you split up SWF into smaller pieces, and load it at runtime,  this method is called <em>runtime asset loading</em>, i will not discuss it now. Here i will only write about embedding asset at compile time using pure AS3, i will not discuss embedding asset using MXML tag or using CSS on Flex Framework. For references i am using <a title="Cool Book" href="http://www.amazon.com/Essential-ActionScript-3-0-Colin-Moock/dp/0596526946" target="_blank">Moock&#8217;s book</a> especially chapter 28 &#8220;Loading External Display Assets&#8221; on the last part, and also some good articles on the net from fellow AS3 developers.</p>
<p><span id="more-8"></span></p>
<h2>Basic Embedding</h2>
<p>In first method we will use <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">[Embed]</span> metadata supported by <em>mxmlc </em>command-line compiler for embedding asset.  Before using it, i must warn you that using <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">[Embed]</span> will not give you any access to script on embedded SWF like on MovieClip asset. It is ideal for graphical and animated asset but not more, see <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=embed_4.html" target="_blank">Adobe Resource Center</a> for more information. As a personal note, i discourage the use of timeline script except for really simple task such as stopping and playing <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">MovieClip</span>.</p>
<p>When using <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">[Embed]</span> metadata, you can assign it in <em>instance-variable definition level</em> or <em>class definition level</em>.</p>
<h3>Instance-Variable Definition Level</h3>
<pre class="brush: as3;">
[Embed (source = &quot;path&quot;)]
var ClassWithEmbed:Class;
// compiler will create new Class as embedded asset representation at compile-time, then you can instantiate  new object
var newObject:* = new ClassWithEmbed();
</pre>
<p>replace &#8220;path&#8221; with relative path or absolute path.</p>
<p>You may change * datatype with specific Class, but what is the real datatype for embedded asset? AS3 reflection method will tell us:</p>
<pre class="brush: as3;">describeType(newObject).extendsClass</pre>
<p>the method will eventually tell you what <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">newObject</span>&#8217;s type is actually. If it is bitmap asset it will be a class that extends from <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">mx.core.BitmapAsset</span>. <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">BitmapAsset</span> extends <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">FlexBitmap</span> then extends <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">Bitmap</span>. There is a problem if you wish to create smallest SWF as possible because you just added <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">mx.core</span> Flex Classes which is overkill. You can turn on <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">link-report</span> <em>mxmlc</em> compiler option to see by yourself just how many class definition included when adding Flex Classes. See this <a href="http://blog.brokenfunction.com/2009/03/16/how-to-remove-flex-libraries-completely-from-your-as3-project/" target="_blank">useful blog</a> to know more about this. <a href="http://www.senocular.com/flash/tutorials/as3withmxmlc/" target="_blank">Senocular</a> has tutorial on <em>mxmlc</em>, if you wish to know what options <em>mxmlc</em> compiler has.</p>
<h3>Class Definition Level</h3>
<p>To avoid problem in <em>instance-variable definition level</em>, we can opt to <em>class definition level</em>. Look at code below, in other word, we extends specific Class for referencing embedded asset. Below we create GIFBitmap Class to extends Bitmap Class since we are embedding .gif image. After that we can use GIFBitmap Class to instantiate new objects.</p>
<pre class="brush: as3;">
package
{
    import flash.display.Bitmap;

    [Embed (source = &quot;asset/some.gif&quot;)]
    public class GIFBitmap extends Bitmap{}
}
</pre>
<p>class definition level for embedding asset is a better way to do, since there is a concern on <a href="http://www.tage7.net/devblog/2010/01/15/as3-embedding-assets-memory-leak/" target="_blank">memory leak could happening</a> if you use metadata tag directly on instance-variable level.</p>
<p>AS3 will automatically &#8220;know&#8221; these types of files:</p>
<ul>
<li>JPG, for class definition level, extends from flash.display.Bitmap</li>
<li>GIF, for class definition level, extends from flash.display.Bitmap</li>
<li>PNG, for class definition level, extends from flash.display.Bitmap</li>
<li>SVG, for class definition level, extends from flash.display.Sprite</li>
<li>MP3, for class definition level, extends from flash.media.Sound</li>
<li>TTF, there is special case with font embedding</li>
<li>SWF, you must use variable-instance level, but for the symbol inside SWF, you can extends from Sprite or MovieClip</li>
</ul>
<h3>Embedding Bitmap</h3>
<p>You may embed jpg, png, and gif files without so much hassle. If you use instance-variable method, the resulting class would be from  mx.core.BitmapAsset.</p>
<pre class="brush: as3;">
// instance-variable level
[Embed (source = &quot;asset/any.png&quot;)]
var AssetClass:Class;
// intantiation
var temp:* = new AssetClass(); // temp is a class of mx.core.BitmapAsset
</pre>
<p>Class-definition method is preferable if you want smallest possible SWF  size. Your subclass should extends from Bitmap.</p>
<pre class="brush: as3;">
// class-definition level
package
{
    import flash.display.Bitmap;

    // extends Bitmap, not mx.core.BitmapAsset
    [Embed (source = &quot;asset/somepng.png&quot;)]
    public class PNGBitmap extends Bitmap
    {
        public function PNGBitmap(){}
    }
}
</pre>
<h3>Embedding Sound</h3>
<p>You may embed MP3 file. If you use instance-variable method, the resulting class would be from <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">mx.core.SoundAsset.</span></p>
<pre class="brush: as3;">
// instance-variable level
[Embed (source = &quot;asset/any.mp3&quot;)]
var AssetClass:Class;
// intantiation
var temp:* = new AssetClass(); // temp is a class of mx.core.SoundAsset
temp.play(); // play the cool sound
</pre>
<pre class="brush: as3;">
// class-definition level
package
{
    import flash.media.Sound;
    // extends Sound
    [Embed (source = &quot;asset/cool.mp3&quot;)]
    public class SoundTrack extends Sound
    {
        public function SoundTrack(){}
    }
}

// try to instantiate it
var sound:flash.media.sound = new SoundTrack();
sound.play();
</pre>
<h3>Embedding Old Swf (targeting below Flash Player 9)</h3>
<p>When you are developing newer swf targeting Flash Player 9 and later, it is better that you use SWC Embedding method, since IDE such as FlashDevelop or Flex/Flash builder can see through the SWC and lists all of the symbol inside that swc.</p>
<p>Now for embedding the SWF, if you wish to embed entire SWF, unfortunately you must use instance-variable method and for the datatype for the class will be <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">mx.core.MovieClipLoaderAsset</span>, meaning you must go with adding Flex Library. There is a panacea though, by using binary file method discussed later.</p>
<pre class="brush: as3;">
[Embed (source = &quot;star.swf&quot;)]
var SWFAsset:Class;
var swf:* = new SWFAsset();
addChild(swf);
</pre>
<p>If you wish to reference MovieClip or Sprite on the swf, you can use class definition method, remember to add linkage id on the symbol you wish to be referenced.</p>
<pre class="brush: as3;">
package
{
    import flash.display.*;
    import flash.utils.describeType;

    [Embed (source=&quot;asset/star.swf&quot;, symbol=&quot;Star&quot;)]
    public class SWFAsset extends Sprite
    {
        public function SWFAsset(){}
    }
}
</pre>
<p>You can actually use SWF embedding and <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">getDefinition()</span> method to get the symbol reference. But it is better to use SWC embedding though.</p>
<h3>Embedding Anything</h3>
<p>Yes, you can embed just anything if you use binary file embedding. You can embed text files, xml, or whatever as long as you can parse it. AS3 XML class can parse ByteArray to XML for example. In fact if you loaded SWF this way, you may not use Flex Library.</p>
<pre class="brush: as3;">
package
{
    import flash.utils.ByteArray;

    [Embed (source=&quot;asset/star.swf&quot;, mimeType=&quot;application/octet-stream&quot;)]
    public class ByteAsset extends ByteArray
    {
        public function SWFAsset(){}
    }
}

// then use it on somewhere
import flash.display.Loader

var loader:Loader = new Loader();
loader.loadBytes(new ByteAsset());
addChild(loader);
</pre>
<p>If you read SWF binary, you can actually use SWF embedding and getDefinition() method to get the  symbol reference too.</p>
<h2>SWC Embedding</h2>
<p>Actually SWC is like a compiled library file plus the class definition. For Action Script 3, it is quite common to see custom-made library developed by people to be distributed in SWC, but more than that, SWC can also be made to package asset files. Flash IDE can publish SWC instead of just SWF, and so we can add the SWC to our library path and &#8220;pull&#8221; assets from our SWC using class instantiation. This method is also preferable workflow of Flash IDE and Flex Builder/Flash Builder/FlashDevelop integration. See also <a href="http://www.airtightinteractive.com/news/?p=327" target="_blank">Airtightinteractive blog</a> for more information about this workflow and <a href="http://blog.geewa.com/post/2009/03/16/Integrating-Flash-Professional-and-Flex-Builder-Using-SWC.aspx">Geewa blog</a> for strategy using the SWC.</p>
<p>For Flash IDE to generate SWC instead of just SWF, you must enable it on Publish Settings &gt; Flash &gt; Export SWC. Do not forget to add linkage id on flash symbol you want to instantiate on Actionscript 3. Set the base Class to <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">MovieClip</span> or <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">Sprite.</span></p>
<p>Then add the SWC, it will be easier if you use Flex Builder or FlashDevelop. If you named your linkage <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">Star</span> that use <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">Sprite</span> as a base class for example, you may use it directly:</p>
<pre class="brush: as3;">
var temp:Sprite = new Star();
aDisplayContainer.addChild(temp);
</pre>
<p>For font embedding and more things including Flex SDK related embed method, you may see <a href="http://www.gskinner.com/talks/flexlovesflash/" target="_blank">Grant Skinner&#8217;s Presentation</a> and of course article on <a href="http://www.adobe.com/devnet/flash/articles/embed_metadata_04.html" target="_blank">Flash Developer Center</a>. Embedding binary file is another approach to embed arbitrary file, but maybe that is the topic for future post <img src='http://nascode.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://nascode.com/2010/02/01/embedding-asset-at-compile-time-in-pure-as3-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

