<?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>GhostWire Studios &#187; Module SWF</title>
	<atom:link href="http://www.ghostwire.com/blog/archives/tag/module-swf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ghostwire.com/blog</link>
	<description>Flash UI Components</description>
	<lastBuildDate>Tue, 23 Aug 2011 03:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[AS3] Applying ROT128 Encryption On Embedded/Module SWFs</title>
		<link>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 04:02:43 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Copy Protection]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1382</guid>
		<description><![CDATA[This post is a supplement to &#8220;Applying ROT128 Encryption On ByteArray&#8221;. Some time back, we posted a simple technique for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a supplement to <a href="http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">&#8220;Applying ROT128 Encryption On ByteArray&#8221;</a>.</p>
<p>Some time back, we posted <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">a simple technique</a> for hiding assets and AS3 code from prying eyes by embedding one SWF within another SWF. In this post, we revisit that topic and look at how ROT128 can be used to provide an additional layer of protection.</p>
<p><span id="more-1382"></span><br />
<strong>Step I: Apply ROT128 To Actual SWF</strong><br />
Using the following code, we will apply ROT128 to the SWF that is going to be embedded:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// assuming the raw data of the SWF has been stored in a variable swfBytes</span>
<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfBytes.<span style="color: #0066CC;">length</span>;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	swfBytes<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The following shows a simple tool you can create to load a SWF file, apply ROT128 to it, and save the encrypted file.</p>
<ul>
<li>Flash Player 10 is required.</li>
<li>Click the Browse button to bring up a native file dialog.</li>
<li>Select a Flash Movie (.swf) file.</li>
<li>Click the Save button that will be shown after loading the SWF file.</li>
</ul>
<p>(80&#215;80 SWF, 2KB)<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ROT128SWF_1619456365"
			class="flashmovie"
			width="80"
			height="80">
	<param name="movie" value="/swf/ROT128SWF.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/swf/ROT128SWF.swf"
			name="fm_ROT128SWF_1619456365"
			width="80"
			height="80">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>You can verify that encryption has been done by running the saved SWF file &#8220;ActualSWF.swf&#8221; &#8211; it should show a blank screen.</p>
<p>Remember, applying ROT128 twice restores the file, so the tool can also be used to restore a previously ROT128-encrypted file if the algorithm used was the same (same n increment, same conditional loop).</p>
<p>The ROT128SWF class:<br />
(please feel free to customize to fit your own requirements)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileFilter</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">FileReference</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ROT128SWF <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** minimalist text button **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> opButton:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ** browse/load/save **</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swfFile:FileReference;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ROT128SWF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// entry point</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span>			= StageAlign.<span style="color: #0066CC;">LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span>			= StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">showDefaultContextMenu</span>	= <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** draw minimalist text button **</span>
			opButton			= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">autoSize</span>		= TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			opButton.<span style="color: #0066CC;">background</span>		= <span style="color: #000000; font-weight: bold;">true</span>;
			opButton.<span style="color: #0066CC;">backgroundColor</span>	= 0x000000;
			opButton.<span style="color: #006600;">defaultTextFormat</span>	= <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Tahoma&quot;</span>, <span style="color: #cc66cc;">14</span>, 0xFFFFFF, <span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">selectable</span>		= <span style="color: #000000; font-weight: bold;">false</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;BROWSE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			opButton.<span style="color: #006600;">y</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> - opButton.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>opButton<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** button click listener **</span>
			opButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, on_buttonClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse or save
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_buttonClick<span style="color: #66cc66;">&#40;</span>evt:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">TextField</span> = evt.<span style="color: #0066CC;">target</span> as <span style="color: #0066CC;">TextField</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;BROWSE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swfFile = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
					swfFile.<span style="color: #006600;">browse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> FileFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Flash Movie&quot;</span>,<span style="color: #ff0000;">&quot;*.swf&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>btn.<span style="color: #0066CC;">text</span> == <span style="color: #ff0000;">&quot;SAVE&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swfFile<span style="color: #66cc66;">&#41;</span>
					<span style="color: #66cc66;">&#123;</span>
						<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swfFile.<span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">length</span>;
						<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
						<span style="color: #66cc66;">&#123;</span>
							swfFile.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
						<span style="color: #66cc66;">&#125;</span>
						<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
						<span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>swfFile.<span style="color: #0066CC;">data</span>, <span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle browse, load swf file
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfSelect<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, on_swfSelect<span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			swfFile.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* handle load, change browse button to save button
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_swfComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			swfFile.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_swfComplete<span style="color: #66cc66;">&#41;</span>;
			opButton.<span style="color: #0066CC;">text</span>		= <span style="color: #ff0000;">&quot;SAVE&quot;</span>;
			opButton.<span style="color: #006600;">x</span>			= <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - opButton.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Step II: Embed Actual SWF In Shell SWF</strong><br />
If you have not done so already, please see <a href="http://www.ghostwire.com/blog/archives/as3-hiding-assets-and-code-by-embedding-swf-within-another-swf/">&#8220;Hiding Assets And Code By Embedding SWF Within Another SWF&#8221;</a> for the original discussion on how the technique works.</p>
<p>The following is a <code>MainShell</code> class you can use to embed the ROT128-encrypted SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainShell <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>		
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;ActualSWF.swf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const bytes:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* REMINDER
		* 
		* MAKE SURE THAT THIS SHELL SWF IS PUBLISHED USING THE
		* ORIGINAL WIDTH/HEIGHT DIMENSIONS OF THE EMBEDDED SWF
		*/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainShell<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> swf:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> bytes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// ** BEGIN ROT128 **</span>
				<span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> = swf.<span style="color: #0066CC;">length</span>;
				<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>j--<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					swf<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> += <span style="color: #cc66cc;">128</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #808080; font-style: italic;">// ** END ROT128 **</span>
				Loader<span style="color: #66cc66;">&#40;</span>addChild<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">loadBytes</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>With the above code, the &#8220;ActualSWF.swf&#8221; file is embedded into the shell SWF.  It is then instantiated as a ByteArray during run-time.  ROT128 is then applied to the ByteArray before it is loaded into a Loader object.</p>
<p><!-- --><br />
<strong>Why Do This At All?</strong><br />
Hiding assets and code by embedding one SWF within another SWF is simple to implement and so far, there has not been any report on any decompiler overcoming the protection.</p>
<p>In the original post on the technique, I suggested that you could, if you wish, add another layer of protection by encrypting the embedded SWF just in case a decompiler may in future automatically identify a binary blob and &#8220;guess&#8221; that it is an embedded SWF (and decompile it separately).</p>
<p>I feel that ROT128 is light-weight and simple enough that can achieve that objective.  Remember that the objective here is not to overcome meticulous hacking, but merely to thwart possible attempts to implement <em>automatic</em> extraction and identification of embedded binary blobs as separate SWFs.</p>
<p>The above is only an example of how the concept can be applied.  As mentioned in <a href=""http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-bytearray/">the previous post on ROT128</a>, you can also consider applying ROT128 only to part of the file so that the logic for reversing the encryption could be a little less predictable.</p>
<p><!-- --><br />
<strong>Module SWFs</strong><br />
Although the above mentioned only embedded SWFs, you can definitely apply the same concept to module SWFs that will be loaded into your application during run-time.  By applying ROT128 to module SWFs, even if they are obtained via unauthorized means such as via the web browser&#8217;s cache, they are non-executable (or will just show a blank screen when run) if the ROT128 encryption is not reversed first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-applying-rot128-encryption-on-embeddedmodule-swfs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[AS3] Difference Between Stage Size, Screen Size And SWF Size</title>
		<link>http://www.ghostwire.com/blog/archives/as3-difference-between-stage-size-screen-size-and-swf-size/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-difference-between-stage-size-screen-size-and-swf-size/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 05:05:19 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://www.ghostwire.com/blog/?p=1028</guid>
		<description><![CDATA[In this post, we look at three different pairs of width and height properties that are commonly misunderstood &#8211; &#8220;stage.width/stage.height&#8220;, &#8220;stage.stageWidth/stage.stageHeight&#8221; and &#8220;loaderInfo.width/loaderInfo.height&#8220;. Stage Size: stage.width / stage.height The Stage refers to the main drawing area on which all Flash content is drawn. This area will encompass all display objects on the display list since [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we look at three different pairs of <code>width</code> and <code>height</code> properties that are commonly misunderstood &#8211; &#8220;<code>stage.width</code>/<code>stage.height</code>&#8220;, &#8220;<code>stage.stageWidth</code>/<code>stage.stageHeight</code>&#8221; and &#8220;<code>loaderInfo.width</code>/<code>loaderInfo.height</code>&#8220;.</p>
<p><span id="more-1028"></span><br />
<strong>Stage Size: stage.width / stage.height</strong><br />
The <code>Stage</code> refers to the main drawing area on which all Flash content is drawn.  This area will encompass all display objects on the display list since all display objects are ultimately contained within the same <code>Stage</code> object.</p>
<p>The <code>stage.width</code> and <code>stage.height</code> properties give you the dimensions of the current rectangular bounds of the <code>Stage</code>.  You can actually get the same results by querying the <code>getBounds()</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> bounds:Rectangle = <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">getBounds</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bounds.<span style="color: #0066CC;">width</span> == <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bounds.<span style="color: #0066CC;">height</span> == <span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span></pre></div></div>

<p>These dimensions only take into account the area occupied by display objects and are therefore not necessarily computed from the origin (coordinates 0,0).  For example, if there is only one display object on the display list and this object is 100px wide by 50px tall, the <code>stage.width</code> property returns 100 regardless of the <code>x</code> position of the object.  Likewise, the <code>height</code> property returns 50 regardless of the <code>y</code> position of the object.</p>
<p>If you wish the dimensions to take into account the origin (0,0), you can place an invisible dot there:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> dot:Shape = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
dot.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// black transparent</span>
dot.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 1x1</span>
dot.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>dot<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>If there is nothing on the display list, or if all the display objects on the display list are empty (or of size zero), both <code>stage.width</code> and <code>stage.height</code> return zero (0).  That is why if you query these properties at the very beginning of your application before you have anything on the display list, you can expect the value of both properties to be zero.</p>
<hr /></p>
<p><!-- --><br />
<strong>Screen Size: stage.stageWidth / stage.stageHeight</strong><br />
The <code>stage.stageWidth</code> and <code>stage.stageHeight</code> properties give you the current dimensions of the viewable screen area of the Flash Player (standalone window area or browser plug-in area).</p>
<p>Although these properties are read-write, setting them in your application code has no effect.  No error will be thrown, but there will simply be no effect.  A change in the value of these properties is typically end-user invoked when the end-user resizes the Flash Player.  Your application can get notified of changes in these properties by listening for the <code>Event.RESIZE</code> event, dispatched by the <code>Stage</code> object.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">RESIZE</span>, on_stageResize<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> on_stageResize<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;screen width : &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;screen height : &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Adobe&#8217;s documentation indicates that if you do not set <code>stage.scaleMode</code> property to <code>StageScaleMode.NO_SCALE</code>, then the <code>stage.stageWidth</code> and <code>stage.stageHeight</code> properties give you the original dimensions of the SWF file, the size that was specified during author-time in the Document Properties dialog box.</p>
<p>However, that is not true.</p>
<p>Contrary to what is stated in Adobe&#8217;s documentation, the <code>stage.scaleMode</code> property has no effect on the <code>stage.stageWidth</code> and <code>stage.stageHeight</code> properties.  Regardless of the value of the <code>stage.scaleMode</code> property, be it set to <code>StageScaleMode.NO_SCALE</code> or otherwise, the <code>stage.stageHeight</code> will <em>always</em> give you the current height of the screen area occupied by the Flash Player, and the <code>stage.stageWidth</code> the current width.  To implement these properties in the way the documentation described would have been wrong in the first place (it would have made the results confusing and less useful), so I would say this is simply a documentation error, not a Flash Player bug.</p>
<p>Knowing the current size of the Flash Player is useful if you wish to create fluid layout that responds to changes in the viewable screen area.  These values are also required when checking if a certain display object is within or outside the bounds of the viewable area.</p>
<hr /></p>
<p><!-- --><br />
<strong>SWF Size: loaderInfo.width / loaderInfo.height</strong><br />
To obtain the original <code>width</code> and <code>height</code> of the SWF file, you must use <code>loaderInfo.width</code> and <code>loaderInfo.height</code>.  These are read-only properties and stay constant.  The values are hard-coded into the SWF file during compile time (you specify this size during authoring).  Adobe&#8217;s documentation refer to these dimensions as the &#8220;nominal&#8221; <code>width</code> and <code>height</code> of the SWF file.</p>
<p>You must wait for the content to be sufficiently loaded before you can query these properties:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">loaderInfo.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, on_loadComplete<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> on_loadComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;swf width : &quot;</span> + loaderInfo.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;swf height : &quot;</span> + loaderInfo.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you are loading external content:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> ldr:Loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,on_loadComplete<span style="color: #66cc66;">&#41;</span>;
ldr.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;module.swf&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>ldr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> on_loadComplete<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> ldrInfo:LoaderInfo = evt.<span style="color: #0066CC;">target</span> as LoaderInfo;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>ldrInfo<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>ldrInfo.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>ldrInfo.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Knowing the nominal dimensions is useful when your application is hosting external module SWFs and you would like to know how much screen space to allocate to a loaded SWF (assuming that the dimensions that are hard-coded into the SWF should be honored).  It is also useful if you wish to know the original aspect ratio of the content loaded into a <code>Loader</code> object (the <code>width</code> and <code>height</code> properties of the <code>Loader</code> object may be modified, but the values of <code>loaderInfo.width</code> and <code>loaderInfo.height</code> will always stay the same).</p>
<hr /></p>
<p><!-- --><br />
<strong>Conclusion</strong><br />
There is generally no correlation between the size of the <code>Stage</code>, the size of the viewable area, and the original size of the SWF.</p>
<p>If you want to know the current viewable area, use <code>stage.stageWidth</code> and <code>stage.stageHeight</code>.</p>
<p>If you want to know the original dimensions of a SWF when it was compiled, use <code>loaderInfo.width</code> and <code>loaderInfo.height</code>.</p>
<p>If you want to know the composite, total area occupied  by display objects, you can use the <code>stage.width</code> and <code>stage.height</code> properties (or the <code>getBounds()</code> or <code>getRect()</code> methods).</p>
<p>It is easy to get confused over these three pairs of properties, so hopefully the above has helped to clarify on the differences.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-difference-between-stage-size-screen-size-and-swf-size/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>[AS3] Avoiding NULL Object Reference Error When Loading Module SWF</title>
		<link>http://www.ghostwire.com/blog/archives/as3-avoiding-null-object-reference-error-when-loading-module-swf/</link>
		<comments>http://www.ghostwire.com/blog/archives/as3-avoiding-null-object-reference-error-when-loading-module-swf/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 02:58:39 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash AS3]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Loader]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://ghostwire.com/blog/?p=675</guid>
		<description><![CDATA[When building modular Flash applications, a common error that may be encountered is that related to accessing a property or method of the stage property when it is still null. If a display object is not yet added to the display list, its stage property returns null. An ActionScript error has occurred: &#8220;Cannot access a [...]]]></description>
			<content:encoded><![CDATA[<p>When building modular Flash applications, a common error that may be encountered is that related to accessing a property or method of the <code>stage</code> property when it is still <code>null</code>. If a display object is not yet added to the display list, its <code>stage</code> property returns <code>null</code>.</p>
<p><em>An ActionScript error has occurred: &#8220;Cannot access a property or method of a null object reference.&#8221;</em></p>
<p>Quite a cryptic message, especially if you test the module SWF standalone and nothing seems wrong, but the error is thrown when you load the module SWF into a host SWF.</p>
<p><span id="more-675"></span><br />
<strong>Background of the Problem</strong><br />
When a module SWF is loaded into a host SWF, the constructor of the top-level class of the module SWF will be called before the instance itself (Sprite or MovieClip) is added to the display list.  In other words, the <code>stage</code> property will still be <code>null</code> when the class is being instantiated and therefore, referencing a property or method of the Stage object (via the <code>stage</code> property) in the constructor of the top-level class will result in a &#8220;null object reference&#8221; error.</p>
<p>The same occurs, of course, if the constructor does not reference a property or method of the <code>stage</code> directly, but calls another member (function or get/set property) that does so.</p>
<p><!-- --><br />
<strong>Example of the Problem</strong><br />
Let&#8217;s take for example we want the application to be notified when the Stage is resized and will therefore attach a listener to the Stage.  The following will work if the SWF is run standalone, but will throw the &#8220;null object reference&#8221; error if it is loaded into a host SWF:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Module <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Module<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">RESIZE</span>, on_stageResize<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// stage is null, cannot access method of null object, error is thrown</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_stageResize<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// ** actual application code begins **</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;stageWidth: &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> + <span style="color: #ff0000;">&quot; stageHeight: &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>Fix for the Problem</strong><br />
To avoid such an error, simply wait until the display object has been added to the Stage before referencing its <code>stage</code> property. You can do this by listening to the <code>Event.ADDED_TO_STAGE</code> event:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Module <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Module<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// remove the event listener, because it is possible for the instance</span>
			<span style="color: #808080; font-style: italic;">// to be removed from its container, and re-added to the display list</span>
			<span style="color: #808080; font-style: italic;">// and invoking the event again but we want to call the init method</span>
			<span style="color: #808080; font-style: italic;">// only once per instance of the class</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** actual application code begins **</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// stage is no longer null</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">RESIZE</span>, on_stageResize<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> on_stageResize<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// ** actual application code begins **</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;stageWidth: &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> + <span style="color: #ff0000;">&quot; stageHeight: &quot;</span> + <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
As mentioned above, even if the constructor itself does not reference a property or method of the <code>stage</code> directly, but calls another member (function or get/set property) that does so, or the member calls another member that does so, and so on (you get the idea)&#8230; this &#8220;null object reference&#8221; error will still occur.  The referencing may be deeply nested, and therefore, it is probably best to make it a habit to have your top-level class listen for the <code>Event.ADDED_TO_STAGE</code> event before executing other code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Module <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Module<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">else</span> addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			removeEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ADDED_TO_STAGE</span>, init<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// ** actual application code begins **</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// stage is no longer null</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p><!-- --><br />
<strong>FlashDevelop</strong><br />
If you are using the <a href="http://www.flashdevelop.org">FlashDevelop</a> IDE, the Main.as class file, written in the way described above, will be created automatically for you when you create a new Project using the &#8220;AS3 Project&#8221; template. However, when <a href="http://ghostwire.com/blog/archives/compiling-module-swfs-using-flashdevelop/">writing module classes</a> and compiling them using the Quick Build command, it will be up to you to remember to write them in the way described above (and it is especially for module SWFs that you need to do this).</p>
<p><!-- --><br />
<strong>Reminder:</strong><br />
A Flash application has only one Stage object &#8211; display objects in both the host SWF and in any loaded SWF(s) reference the same Stage.  Do not confuse <code>stage</code> with the <code>root</code> property.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/as3-avoiding-null-object-reference-error-when-loading-module-swf/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Compiling Module SWFs Using FlashDevelop</title>
		<link>http://www.ghostwire.com/blog/archives/compiling-module-swfs-using-flashdevelop/</link>
		<comments>http://www.ghostwire.com/blog/archives/compiling-module-swfs-using-flashdevelop/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 07:28:28 +0000</pubDate>
		<dc:creator>sunny</dc:creator>
				<category><![CDATA[Aspire UI]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Flash Components]]></category>
		<category><![CDATA[FlashDevelop]]></category>
		<category><![CDATA[Module SWF]]></category>

		<guid isPermaLink="false">http://ghostwire.com/blog/?p=439</guid>
		<description><![CDATA[Breaking a large application down into modules allows the application to be loaded in more manageable chunks. A &#8220;module&#8221; is simply a term borrowed from Flex to refer to a child SWF that is loaded into a main (host) SWF &#8211; in our context, a &#8220;module&#8221; is just like any other SWF. But, if built [...]]]></description>
			<content:encoded><![CDATA[<p>Breaking a large application down into modules allows the application to be loaded in more manageable chunks.  A &#8220;module&#8221; is simply a term borrowed from Flex to refer to a child SWF that is loaded into a main (host) SWF &#8211; in our context, a &#8220;module&#8221; is just like any other SWF.  But, if built correctly, it should be a &#8220;stripped down&#8221; SWF &#8211; it will not contain classes that already exist in the host SWF that it is going to be loaded into.  This usually means that modules cannot run standalone (which is the desired outcome in most cases, considering that the modules are intended to be loaded into the main application).</p>
<p>When using <a href="http://flashdevelop.org">FlashDevelop</a>, building a Project (pressing F8 or CTRL-ENTER) compiles a single SWF using the AS class file that has been flagged as &#8220;Always Compile&#8221;.  If you are building an application that consists of a host SWF and multiple module SWFs, FlashDevelop does not automate the job for you.</p>
<p><span id="more-439"></span><br />
<strong>Use Multiple Projects</strong><br />
One way to build modules in FlashDevelop is to set up a different Project for each module, with each Project using the same assets but different build configurations (the module class in each Project is flagged as &#8220;Always Compile&#8221;).  The Project for a module class would be set up such that duplicate classes are excluded (via compiler options). However, this can get troublesome and difficult to manage/maintain.</p>
<p><!-- --><br />
<strong>Use Quick Build</strong><br />
What you can do is stick to a single Project &#8211; when building the Project, the resulting SWF will be the main (host) SWF.  Modules are built using the Quick Build command, accessed via the menu -> Tools -> Flash Tools -> Build Current File (CTRL-F8). This compiles the currently open class file, so you need to open the module class you want to compile and then press CTRL-F8.</p>
<p>The Quick Build command ignores your Project settings, so in order for this to work, you need to insert build options into each module class file. At the top of each module class, insert the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
* @mxmlc -o=bin/ClassName.swf -external-library-path=AspireUIStandard.swc
*/</span></pre></div></div>

<p>The above is only an example, you can also specify other MXMLC options (optimize, default-size, etc.).  In the above example, we exclude the classes in the AspireUIStandard.swc from being compiled into the module SWF.  If you are using other libraries that are going to exist in the host SWF, remember to exclude them too.</p>
<p>To make testing easy, you can do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
* @mxmlc -o=bin/ClassName.swf -l=AspireUIStandard.swc
* //@mxmlc -o=bin/ClassName.swf -external-library-path=AspireUIStandard.swc
*/</span></pre></div></div>

<p>-l is alias for -compiler.library-path<br />
Use that when you want to compile the module for testing standalone.</p>
<p><!-- --><br />
<strong>&#8220;exclude.xml&#8221;</strong><br />
Instead of using the -external-library-path option, you can also use an &#8220;exclude.xml&#8221; file and the -load-externs option.</p>
<p>Generate an &#8220;exclude.xml&#8221; from the main Project &#8211; in the project settings, compiler options, under &#8220;Additional Compiler Options&#8221;, add the following:</p>
<p>-link-report exclude.xml</p>
<p>Now build your project (CTRL-ENTER).  Verify that the &#8220;exclude.xml&#8221; is generated and is in the root folder of your Project.</p>
<p>With the &#8220;exclude.xml&#8221; file generated, you can now use the -load-externs directive for your modules:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
* //@mxmlc -o=bin/ClassName.swf -l=AspireUIStandard.swc
* @mxmlc -o=bin/ClassName.swf -load-externs=exclude.xml
*/</span></pre></div></div>

<p>This is recommended if the host SWF may not include all the classes in the libraries the modules are using.</p>
<p><!-- --><br />
<strong>uiSWF</strong><br />
Once you have created the module SWFs, you can have your host SWF load them using the <a href="/aspireui/docs/usage/uiswf">uiSWF</a> component (<a href="/aspireui/">Aspire UI</a> Standard Edition v1.2+). This control is a subclass of uiPane, making it suitable to be plugged in as the content of a uiScrollPane instance (in which case the module should not implement its own wrapper scrollers).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ghostwire.com/blog/archives/compiling-module-swfs-using-flashdevelop/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

