<?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>Anselm Bradford &#187; AS3</title>
	<atom:link href="http://blog.anselmbradford.com/category/flash-platform/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anselmbradford.com</link>
	<description>wrangling the Internet's wildest</description>
	<lastBuildDate>Mon, 27 Jun 2011 19:17:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>July 2010 Auckland Flash Platform User Group Recap</title>
		<link>http://blog.anselmbradford.com/2010/07/08/july-2010-auckland-flash-platform-user-group-recap/</link>
		<comments>http://blog.anselmbradford.com/2010/07/08/july-2010-auckland-flash-platform-user-group-recap/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 12:03:56 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash Builder]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=998</guid>
		<description><![CDATA[This is old news now, but as per my personal tradition I wanted to post a short recap of the Flash Platform User Group meetup that happened July 1. First to present was Michael Andrew, who demoed FlexPMD, an auditing tool for ActionScript/Flex projects. Essentially the tool provides a set of recommendations of how your [...]]]></description>
			<content:encoded><![CDATA[<p>This is old news now, but as per my personal tradition I wanted to post a short recap of the Flash Platform User Group meetup that happened July 1. </p>
<p>First to present was <a href="http://hazardmedia.co.nz" onclick="pageTracker._trackPageview('/outgoing/hazardmedia.co.nz?referer=');">Michael Andrew</a>, who demoed <a href="http://opensource.adobe.com/wiki/display/flexpmd/FlexPMD" onclick="pageTracker._trackPageview('/outgoing/opensource.adobe.com/wiki/display/flexpmd/FlexPMD?referer=');">FlexPMD</a>, an auditing tool for ActionScript/Flex projects. Essentially the tool provides a set of recommendations of how your code could be improved/cleaned up. According to the Adobe Open Source page (linked above), the tool can be invoked from a number of different environments:</p>
<ul>
<li>The command line</li>
<li>Maven</li>
<li>Ant</li>
<li>Automator on Mac OS X</li>
<li>Eclipse</li>
<li>TextMate</li>
<li>FlashDevelop</li>
</ul>
<p>Oddly Flash is not listed, but perhaps Flash is beyond hope as a quality code IDE! <img src='http://blog.anselmbradford.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Second to present was <a href="http://greg-dove.com" onclick="pageTracker._trackPageview('/outgoing/greg-dove.com?referer=');">Greg Dove</a>, who live-coded a simple touch screen demo on a <a href="http://accessories.us.dell.com/sna/products/Displays/productdetail.aspx?c=us&#038;l=en&#038;cs=19&#038;sku=320-1172" onclick="pageTracker._trackPageview('/outgoing/accessories.us.dell.com/sna/products/Displays/productdetail.aspx?c=us_038_l=en_038_cs=19_038_sku=320-1172&amp;referer=');">Dell SX2210T</a>, an inexpensive touch screen display with two touch points capability. Greg also mentioned the best display he&#8217;d seen, the <a href="http://solutions.3m.com/wps/portal/3M/en_US/TouchSystems/TouchScreen/Solutions/TouchScreenDisplays/M2256pw/?WT.mc_id=www.3m.com/multitouch" onclick="pageTracker._trackPageview('/outgoing/solutions.3m.com/wps/portal/3M/en_US/TouchSystems/TouchScreen/Solutions/TouchScreenDisplays/M2256pw/?WT.mc_id=www.3m.com/multitouch&amp;referer=');">3M M2256PW</a>, which is capable of a whooping 20 simulations touch inputs (<a href="http://www.engadget.com/2010/01/07/3m-m2256pw-10-finger-multitouch-display-hands-on-with-video/" onclick="pageTracker._trackPageview('/outgoing/www.engadget.com/2010/01/07/3m-m2256pw-10-finger-multitouch-display-hands-on-with-video/?referer=');">view a demo on engadget</a>). Greg&#8217;s demo created a group of colored stars that could be resized and zoomed—both as a group and individually. The specific event Greg demoed was the recently added AS3 <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/TransformGestureEvent.html?allClasses=1" onclick="pageTracker._trackPageview('/outgoing/help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/TransformGestureEvent.html?allClasses=1&amp;referer=');">TransformGestureEvent</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2010/07/08/july-2010-auckland-flash-platform-user-group-recap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>June 2010 Auckland Flash Platform User Group Recap</title>
		<link>http://blog.anselmbradford.com/2010/06/04/june-2010-auckland-flash-platform-user-group-recap/</link>
		<comments>http://blog.anselmbradford.com/2010/06/04/june-2010-auckland-flash-platform-user-group-recap/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 09:24:17 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=993</guid>
		<description><![CDATA[This month&#8217;s Auckland Flash Platform User Group Meetup began with a show-and-tell by the group&#8217;s usual host, VisFleet. CTO Aisha Fenton explained the business-side of VisFleet&#8217;s vWork software, a web-based task management application that allows a dispatcher to disseminate jobs to remote workers, who can log in via an iPhone app. Aisha provided an overview [...]]]></description>
			<content:encoded><![CDATA[<p>This month&#8217;s <a href="http://aucklandflash.eventbrite.com/" onclick="pageTracker._trackPageview('/outgoing/aucklandflash.eventbrite.com/?referer=');">Auckland Flash Platform User Group Meetup</a> began with a show-and-tell by the group&#8217;s usual host, <a href="http://www.visfleet.com" onclick="pageTracker._trackPageview('/outgoing/www.visfleet.com?referer=');">VisFleet</a>. CTO Aisha Fenton explained the business-side of VisFleet&#8217;s vWork software, a web-based task management application that allows a dispatcher to disseminate jobs to remote workers, who can log in via an iPhone app. Aisha provided an overview and demonstration of the software. <a href="http://twitter.com/squeedee" onclick="pageTracker._trackPageview('/outgoing/twitter.com/squeedee?referer=');">Rasheed Abdul-Aziz</a> then delved into the technology behind the application, which utilizes <a href="http://robotlegs.org" onclick="pageTracker._trackPageview('/outgoing/robotlegs.org?referer=');">Robotlegs</a> and the Flex framework. Rasheed mentioned <a href="http://hudson-ci.org" onclick="pageTracker._trackPageview('/outgoing/hudson-ci.org?referer=');">Hudson CI</a> for continuous integration, <a href="http://github.com" onclick="pageTracker._trackPageview('/outgoing/github.com?referer=');">GitHub</a> for source control and collaborative development, and <a href="http://www.charlesproxy.com" onclick="pageTracker._trackPageview('/outgoing/www.charlesproxy.com?referer=');">Charles Web Proxy</a>, a tool for analyzing HTTP data. Charles has some local fame, as it is developed by Auckland-based <a href="http://www.cactuslab.com/products/" onclick="pageTracker._trackPageview('/outgoing/www.cactuslab.com/products/?referer=');">Cactus Labs</a>.</p>
<p>After pizza <a href="http://twitter.com/love_jam" onclick="pageTracker._trackPageview('/outgoing/twitter.com/love_jam?referer=');">Jove Lam</a>, of TVNZ, demoed a Flash application he helped create for the Sony PS3 in-game browser to view on-demand content. Called <a href="http://tvnz.co.nz/ps3/video" onclick="pageTracker._trackPageview('/outgoing/tvnz.co.nz/ps3/video?referer=');">TVNZ OnDemand</a> the application featured an intuitive columnar layout for browsing content in different categories. On first impression it was not apparent that the application was essentially a full-screen Flash website. In his presentation, Jove mentioned the <a href="http://www.osmf.org" onclick="pageTracker._trackPageview('/outgoing/www.osmf.org?referer=');">Open Source Media Framework (OSMF)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2010/06/04/june-2010-auckland-flash-platform-user-group-recap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>March 2010 Auckland Flash Platform User Group Recap</title>
		<link>http://blog.anselmbradford.com/2010/03/18/march-2010-auckland-flash-platform-user-group-recap/</link>
		<comments>http://blog.anselmbradford.com/2010/03/18/march-2010-auckland-flash-platform-user-group-recap/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 12:09:46 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=984</guid>
		<description><![CDATA[Last night I attended the March meeting of the Auckland Flash Platform UG, held in the spacious offices of VisFleet. This was a great opportunity to freshen my knowledge on what&#8217;s happening in the Flash world. There were two presentations, with a break in the middle for the traditional developers meeting pastime of pizza and [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I attended the March meeting of the <a href="http://aucknzfpug.eventbrite.com" onclick="pageTracker._trackPageview('/outgoing/aucknzfpug.eventbrite.com?referer=');">Auckland Flash Platform UG</a>, held in the spacious offices of <a href="http://www.visfleet.com" onclick="pageTracker._trackPageview('/outgoing/www.visfleet.com?referer=');">VisFleet</a>. This was a great opportunity to freshen my knowledge on what&#8217;s happening in the Flash world. There were two presentations, with a break in the middle for the traditional developers meeting pastime of pizza and beer. </p>
<p>First up was <a href="http://twitter.com/squeedee" onclick="pageTracker._trackPageview('/outgoing/twitter.com/squeedee?referer=');">Rasheed Abdul-Aziz</a>, Flex/Rails developer at VisFleet. Unfortunately I arrived a little late and missed the beginning of Rasheed&#8217;s presentation, but he covered a diversity of Rails and Flex development topics, including discussing refactoring and <a href="http://en.wikipedia.org/wiki/Domain-specific_language" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Domain-specific_language?referer=');">Domain Specific Languages (DSL)</a>. In the later half of Rasheed&#8217;s presentation the focus turned to <a href="http://www.robotlegs.org" onclick="pageTracker._trackPageview('/outgoing/www.robotlegs.org?referer=');">Robotlegs</a>, a fairly recent (as to my knowledge) dependency injection ActionScript 3.0 framework that Rasheed swears by. I&#8217;m excited to find some time to try it out! Next, Rasheed demonstrated some of the benefits of using <a href="http://blogs.jetbrains.com/idea/2008/02/creating-flex-applications-with-intellij-idea/" onclick="pageTracker._trackPageview('/outgoing/blogs.jetbrains.com/idea/2008/02/creating-flex-applications-with-intellij-idea/?referer=');">IntelliJ IDEA</a> for Adobe Flex development, and gave it a glowing recommendation. Robert Penner&#8217;s <a href="http://github.com/robertpenner/as3-signals" onclick="pageTracker._trackPageview('/outgoing/github.com/robertpenner/as3-signals?referer=');">AS3Signals</a> (a new approach for AS3 events) was mentioned at the end during Q&amp;A.</p>
<p>Next <a href="http://vivace.co.nz" onclick="pageTracker._trackPageview('/outgoing/vivace.co.nz?referer=');">Vinnie Vivace</a> showed some simple 3D examples using <a href="http://away3d.com/away3d-lite-v1-0-fastest-and-smallest-3d-engine-in-flash" onclick="pageTracker._trackPageview('/outgoing/away3d.com/away3d-lite-v1-0-fastest-and-smallest-3d-engine-in-flash?referer=');">Away3DLite</a>, a slimmed-down version of <a href="http://away3d.com" onclick="pageTracker._trackPageview('/outgoing/away3d.com?referer=');">Away3D</a>. Vinnie also briefly showed a <a href="http://unity3d.com" onclick="pageTracker._trackPageview('/outgoing/unity3d.com?referer=');">Unity3D</a> example.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2010/03/18/march-2010-auckland-flash-platform-user-group-recap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>This isn&#8217;t right&#8230; MovieClip nested inside Button throwing null object reference error in Flash CS4</title>
		<link>http://blog.anselmbradford.com/2009/09/10/this-isnt-right-movieclip-nested-inside-button-throwing-null-object-reference-error-in-flash-cs4/</link>
		<comments>http://blog.anselmbradford.com/2009/09/10/this-isnt-right-movieclip-nested-inside-button-throwing-null-object-reference-error-in-flash-cs4/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 12:02:21 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=933</guid>
		<description><![CDATA[A great thing about teaching is that your students approach problems in ways you haven&#8217;t done before and run into problems that you never knew existed. This is one such problem in Flash CS4 and a curious one at that. THE PROBLEM: A Button symbol is placed on the Stage on a frame other than [...]]]></description>
			<content:encoded><![CDATA[<p>A great thing about teaching is that your students approach problems in ways you haven&#8217;t done before and run into problems that you never knew existed. This is one such problem in Flash CS4 and a curious one at that.</p>
<p><strong>THE PROBLEM:</strong> A Button symbol is placed on the Stage on a frame other than the first frame and given an instance name in the Properties panel. In the Actions panel, ActionScript is added that references the instance name of the button on the same frame that the button instance first appears on. The movie is tested and the following error occurs:<br />
<code><br />
TypeError: Error #1009: Cannot access a property or method of a null object reference.<br />
at MovieClipInsideButton_fla::MainTimeline/frame2()<br />
</code></p>
<p>Normally this error means that a particular instance has not been given an instance name in the Properties panel on the frame where the ActionScript appears. However, nothing looks amiss in the ActionScript or on the Stage. Adding a trace statement, such as <code>trace(myButton);</code> reveals <code>null</code> in the output window, meaning the trace statement has run before the instance name has been set to the button instance on the Stage. Hmm&#8230; very perplexing, since you may have other buttons and movieclips that work fine when referenced from ActionScript.</p>
<p><strong>THE SOLUTION:</strong> From what I found, one scenario will produce this error. If a MovieClip symbol (instance) appears inside a Button symbol and the button appears on any frame other than the first frame, and the frame where the ActionScript references it is the same frame it first appears on—then this behavior will result.</p>
<p><img class="aligncenter size-full wp-image-935" title="MC_inside_button" src="http://blog.anselmbradford.com/wp-content/uploads/2009/09/MC_inside_button.png" alt="MC_inside_button" width="569" height="267" /></p>
<p>There are a few workarounds:</p>
<ol>
<li><strong>Retrieve the instance from the display list of its parent.</strong> Instead of referencing the Button instance on the stage directly, use <code>getChildByName(.)</code> to retrieve a reference to the instance, this can even be set to instance name at the beginning of your timeline code, like so:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// myButton is the name of the instance as set in the Properties panel.</span>
<span style="color: #808080; font-style: italic;">// Retrieved child is cast as a SimpleButton, since this is the </span>
<span style="color: #808080; font-style: italic;">// class of the Button symbol</span>
myButton = SimpleButton<span style="color: #66cc66;">&#40;</span>getChildByName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myButton&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

</li>
<li><strong>Do not nest a MovieClip inside a Button symbol.</strong> A Button nested inside a Button, a MovieClip nested inside a MovieClip, and a Button nested inside a MovieClip work fine.</li>
<li><strong>Place the first occurance of the Button instance on any frame before the frame where ActionScript first references it.</strong> This behavior appears to indicate Flash is taking too long to parse over the MovieClip inside a Button and runs the ActionScript on the timeline prematurely. Having the instance appear on a frame prior to the frame the ActionScript appears on ensures the instance is completely present in memory before it is referenced.</li>
<li><strong>Place the instance referencing code inside an enter frame event handler function.</strong> To ensure the entire frame has been parsed before any ActionScript code is run, enclose the offending code inside the event handler function of the enter frame event, and only make it run once, like so:

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">addEventListener<span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">ENTER_FRAME</span> , ensureRendered <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ensureRendered<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;">ENTER_FRAME</span> , ensureRendered <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// All instance referencing code appears below this point</span>
	<span style="color: #808080; font-style: italic;">// In this case, instance is named &quot;myButton&quot; on the Stage</span>
	myButton.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</li>
</ol>
<p>This behavior doesn&#8217;t seem right to me and even seems it could be a bug. If anyone has an explanation for this behavior, I would love to hear it. You may download an example FLA here: <a href='/wp-content/uploads/2009/09/movieclipinsidebutton.zip'>Download Source</a></p>
<p><strong>UPDATE September 20, 2009:</strong> I added a new workaround, which is probably the most succinct and reliable one yet to use. See <em>Retrieve the instance from the display list of its parent</em> in the list of workarounds.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/09/10/this-isnt-right-movieclip-nested-inside-button-throwing-null-object-reference-error-in-flash-cs4/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Creating a date countdown timer in ActionScript 3 / Flash</title>
		<link>http://blog.anselmbradford.com/2009/08/03/creating-a-date-countdown-timer-in-actionscript-3-flash/</link>
		<comments>http://blog.anselmbradford.com/2009/08/03/creating-a-date-countdown-timer-in-actionscript-3-flash/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 15:23:41 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=873</guid>
		<description><![CDATA[Creating a countdown timer in ActionScript is quite easy. The essentials of it are: Specify a target date to countdown to using the Date class. Create a Timer instance to check the countdown time each second or so. At each tick of the timer calculate the number of milliseconds between now and the target date. [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a countdown timer in ActionScript is quite easy. The essentials of it are:</p>
<ol>
<li>Specify a target date to countdown to using the <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Date.html" onclick="pageTracker._trackPageview('/outgoing/livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Date.html?referer=');">Date</a> class.</li>
<li>Create a <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html" onclick="pageTracker._trackPageview('/outgoing/livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html?referer=');">Timer</a> instance to check the countdown time each second or so.</li>
<li>At each tick of the timer calculate the number of milliseconds between now and the target date.</li>
<li>Determine how many days, hours, minutes, seconds, etc. that number of milliseconds is equal to.</li>
<li>Update the graphics to reflect the amount of time left.</li>
</ol>
<p>The following is an example of a countdown timer, with a link to the source code below. This utilizes a reusable class file for the countdown timer. All code specific to this particular implementation can be found in com/anselmbradford/Main.as.<br />

<object width="200" height="45">
<param name="movie" value="/wp-content/uploads/2009/08/counter.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<param name="allowScriptAccess" value="always"></param>
<embed type="application/x-shockwave-flash" width="200" height="45" src="/wp-content/uploads/2009/08/counter.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
</p>
<p><a href='/wp-content/uploads/2009/08/counter.zip'>Download Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/08/03/creating-a-date-countdown-timer-in-actionscript-3-flash/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to build an Object-Oriented ActionScript 3 Preloader in Flash CS4: 2 Methods</title>
		<link>http://blog.anselmbradford.com/2009/06/20/how-to-build-an-object-oriented-actionscript-3-preloader-in-flash-cs4-2-methods/</link>
		<comments>http://blog.anselmbradford.com/2009/06/20/how-to-build-an-object-oriented-actionscript-3-preloader-in-flash-cs4-2-methods/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 03:26:05 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=836</guid>
		<description><![CDATA[The magic of creating a preloader in ActionScript 3 lies in the LoaderInfo class. Every instantiated DisplayObject instance (all objects that appear on the stage, plus the stage itself) have a loaderInfo property that returns a LoaderInfo instance that contains information about the loading progress of that particular display object. Creating a preloader for the [...]]]></description>
			<content:encoded><![CDATA[<p>The magic of creating a preloader in ActionScript 3 lies in the <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html?referer=');">LoaderInfo</a> class. Every instantiated DisplayObject instance (all objects that appear on the stage, plus the stage itself) have a <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#loaderInfo" onclick="pageTracker._trackPageview('/outgoing/livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html_loaderInfo?referer=');">loaderInfo</a> property that returns a LoaderInfo instance that contains information about the loading progress of that particular display object. Creating a preloader for the whole application is a matter of monitoring these LoaderInfo instances.</p>
<h3>Method 1: Monitor Stage LoaderInfo instance</h3>
<p>Under this method the loading progress is monitored via the LoaderInfo instance associated with the stage. Since all assets that will appear in the application need to be attached to the stage, the stage&#8217;s LoaderInfo instance will reflect the loading of all these assets.</p>
<h4>Graphics</h4>
<ol>
<li>Go to File &#8594; New&#8230; and select Flash File (ActionScript 3.0).</li>
<li>Using the Rectangle tool create a rectangle (include a stroke) on the stage that is 100 pixels wide by 10 pixels high.</li>
<li>Click on the fill of the newly created rectangle and select Modify &#8594; Convert to Symbol&#8230; Name the new instance <em>ProgBar</em> and set the registration point to left-middle. This will be the progress bar of the preloader.
<p><img class="border" src="http://blog.anselmbradford.com/wp-content/uploads/2009/06/preloader_pbar.png" alt="preloader_pbar" title="preloader_pbar" width="626" height="289" class="alignnone size-full wp-image-844" /></p>
</li>
<li>With the new ProgBar instance selected on the stage, go to the Properties panel and name the instance <em>progBar</em>.</li>
<li>Select the whole rectangle (stroke and progress bar instance). Select Modify &#8594; Convert to Symbol&#8230; Name the new instance <em>Preloader</em> and set the registration point to left-middle. Check the <em>Export for ActionScript</em> button.
<li>Click <em>OK</em>. If you get a warning that says the &#8220;definition for this class could not be found&#8221; click <em>OK</em> again.</li>
<li>With the new Preloader instance selected, go to the Properties panel and name the newly created instance <em>preloader</em>. Your library should look like the following:
<p><img class="border" src="http://blog.anselmbradford.com/wp-content/uploads/2009/06/preloader_library.png" alt="preloader_library" title="preloader_library" width="285" height="153" class="alignnone size-full wp-image-848" /></p>
</li>
<li>Select frame 2 of the main timeline, go to Insert &#8594; Timeline &#8594; Blank Keyframe. All content for the application will appear on frame 2 and beyond.</li>
<li>Go to Insert &#8594; New Symbol&#8230; Name the new instance <em>Content</em>. If you check <em>Export for ActionScript</em> be sure to <strong>uncheck <em>Export in frame 1</em></strong>, otherwise the preloader will not function properly.</li>
<li>Place all content of the application on the timeline of the newly created Content instance.</li>
<li>Return to the main timeline. Select frame 2 and drag an instance of the Content symbol from the library and place it on the stage.</li>
<li>In the Properties panel of the stage name the document class <em>Main</em>. If you get a warning that says the &#8220;definition for this class could not be found&#8221; click <em>OK</em>.
<p><img class="border" src="http://blog.anselmbradford.com/wp-content/uploads/2009/06/preloader_docclass.png" alt="preloader_docclass" title="preloader_docclass" width="283" height="200" class="alignnone size-full wp-image-854" /></p>
</li>
<li>Save the file.</li>
</ol>
<h4>Code</h4>
<ol>
<li>Go to File &#8594; New&#8230; and select ActionScript File.</li>
<li>Paste the following code and save the file as <em>Preloader.as</em> in the same directory as the .fla file saved earlier.

<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: #006600;">LoaderInfo</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> Preloader <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		* Alias for stage LoaderInfo instance
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _targetLoaderInfo:LoaderInfo;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* The percent loaded
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _loadPercent:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Constructor
		* Listen for when the preloader has been added to the stage 
		* so that the progress of the remaining load can be monitored.
		*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Preloader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><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: #808080; font-style: italic;">/**
		* Initialize variables.
		* Set initial width of the progress bar to 0 
		* and listen for enter frame event.
		*/</span>
		<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>
			_targetLoaderInfo = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">loaderInfo</span>;
&nbsp;
			progBar.<span style="color: #006600;">scaleX</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><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: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, _onCheckLoaded<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Check the status of the load, once complete dispatch a complete event.
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _onCheckLoaded<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>
			_loadPercent = _targetLoaderInfo.<span style="color: #0066CC;">bytesLoaded</span> <span style="color: #66cc66;">/</span> _targetLoaderInfo.<span style="color: #0066CC;">bytesTotal</span>;
			progBar.<span style="color: #006600;">scaleX</span> = _loadPercent;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>progBar.<span style="color: #006600;">scaleX</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, _onCheckLoaded<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span><span style="color: #66cc66;">&#41;</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>	
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</li>
<li>Go to File &#8594; New&#8230; and select ActionScript File a second time.</li>
<li>Paste the following code and save the file as <em>Main.as</em> in the same directory as the .fla file saved earlier.

<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: #0066CC;">MovieClip</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> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		* Constructor
		* Stop timeline and add event listener to preloader.
		*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> 
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			preloader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> , _initContent <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Load has finished, remove preloader and preceed to next frame.
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _initContent<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>
			preloader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> , _initContent <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>preloader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">nextFrame</span><span style="color: #66cc66;">&#40;</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>

</li>
<li>To test the preloader go to Control &#8594; Test Movie. Go to View &#8594; Simulate Download.</li>
<li><a href='/wp-content/uploads/2009/06/preloader_method1.zip'>Download method 1 complete source</a></li>
</ol>
<h3>Method 2: Monitor content SWF LoaderInfo instance</h3>
<p>Under this method two SWFs are created, one encapsulating the preloader and the other encapsulating all content. The content SWF is loaded inside of the preloader SWF, which monitors the loading progress.</p>
<h4>Graphics</h4>
<ol>
<li>Follow steps 1 &#8211; 7 in the previous method.</li>
<li>In the Properties panel of the stage name the document class <em>PreloaderWrapper</em>. If you get a warning that says the &#8220;definition for this class could not be found&#8221; click <em>OK</em>.</li>
<li>Save the file.</li>
<li>Go to File &#8594; New&#8230; and select Flash File (ActionScript 3.0).</li>
<li>Add the content for your application to the timeline of this file.</li>
<li>Save the file as <em>content.fla</em>.</li>
<li>Go to Control &#8594; Test Movie to create the content.swf that will be loaded by the preloader SWF.</li>
</ol>
<h4>Code</h4>
<ol>
<li>Go to File &#8594; New&#8230; and select ActionScript File.</li>
<li>Paste the following code and save the file as <em>PreloaderWrapper.as</em> in the same directory as the .fla file saved earlier.

<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: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">LoaderInfo</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</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;">ProgressEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PreloaderWrapper <span style="color: #0066CC;">extends</span> Sprite 
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/**
		* Alias for content LoaderInfo instance
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _targetLoaderInfo:LoaderInfo;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* The percent loaded
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _loadPercent:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Constructor
		* Creates Loader instance, adds event listeners and begins loading content SWF.
		*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PreloaderWrapper<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> loader:Loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			_targetLoaderInfo = loader.<span style="color: #006600;">contentLoaderInfo</span>;
			_targetLoaderInfo.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> ProgressEvent.<span style="color: #006600;">PROGRESS</span>, _loadingData <span style="color: #66cc66;">&#41;</span>;
			_targetLoaderInfo.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> , _finishedLoading <span style="color: #66cc66;">&#41;</span>;
			loader.<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;content.swf&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Monitor loading progress and update progress bar.
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _loadingData<span style="color: #66cc66;">&#40;</span> evt:ProgressEvent <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> 
		<span style="color: #66cc66;">&#123;</span>
			_loadPercent = _targetLoaderInfo.<span style="color: #0066CC;">bytesLoaded</span> <span style="color: #66cc66;">/</span> _targetLoaderInfo.<span style="color: #0066CC;">bytesTotal</span>;
			preloader.<span style="color: #006600;">progBar</span>.<span style="color: #006600;">scaleX</span> = _loadPercent;	
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Remove event listeners and preloader, and attach content SWF to stage.
		*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _finishedLoading<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>
			_targetLoaderInfo.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> ProgressEvent.<span style="color: #006600;">PROGRESS</span>, _loadingData <span style="color: #66cc66;">&#41;</span>;
			_targetLoaderInfo.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> , _finishedLoading <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>preloader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span> DisplayObject<span style="color: #66cc66;">&#40;</span>LoaderInfo<span style="color: #66cc66;">&#40;</span>evt.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">content</span><span style="color: #66cc66;">&#41;</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>

</li>
<li>To test the preloader go to Control &#8594; Test Movie. Go to View &#8594; Simulate Download.</li>
<li><a href='/wp-content/uploads/2009/06/preloader_method2.zip'>Download method 2 complete source</a></li>
</ol>
<p><strong>UPDATE July 30, 2009:</strong> I noticed I forgot the <em>private</em> designation on two of the methods in the PreloaderWrapper class. They have been added.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/06/20/how-to-build-an-object-oriented-actionscript-3-preloader-in-flash-cs4-2-methods/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Common Flash Compiler Errors: #1046</title>
		<link>http://blog.anselmbradford.com/2009/03/25/common-flash-compiler-errors-1046/</link>
		<comments>http://blog.anselmbradford.com/2009/03/25/common-flash-compiler-errors-1046/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 08:39:26 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=755</guid>
		<description><![CDATA[This error shows the following in the Compiler Errors window: 1046: Type was not found or was not a compile-time constant: [Class name]. Quick Answer and Solution Where I have &#8220;[Class name]&#8221; above you will have any number of names listed, for example it may say Event, Sprite, TextField, etc. What this means is you [...]]]></description>
			<content:encoded><![CDATA[<p>This error shows the following in the Compiler Errors window:</p>
<p><code><br />
1046: Type was not found or was not a compile-time constant: [Class name].<br />
</code></p>
<h3>Quick Answer and Solution</h3>
<p>Where I have &#8220;[Class name]&#8221; above you will have any number of names listed, for example it may say <code>Event</code>, <code>Sprite</code>, <code>TextField</code>, etc. What this means is you are using a piece of code that utilizes a class (typically seen as files with a &#8220;.as&#8221; extension) that you have not &#8220;imported&#8221; so that your code knows where to find it. To fix this problem perform the following:</p>
<ol>
<li>Open a web browser and navigate to Google.</li>
<li>Enter the search &#8220;[class name] actionscript 3 site:livedocs.adobe.com&#8221; (replacing [class name] with whatever the class name is in the error message, such as TextField, MouseEvent, URLLoader, etc.). For example, &#8220;Event actionscript 3 site:livedocs.adobe.com&#8221;</li>
<li>Look for a search result that lists the class in a format of a series of words with dots between them, probably will be the top result. Examples include:
<ul>
<li>flash.events.Event</li>
<li>flash.display.Sprite</li>
<li>fl.transitions.Tween</li>
<li>flash.geom.Rectangle</li>
</ul>
<p>This is the path to the class, which you can use to import the class into your code.</li>
<li>Without clicking on the search result, copy the class name as it appears in the previous step.</li>
<li>Return to Flash and double-click the error message.</li>
<li>Scroll to the top of the code area. Type &#8220;import&#8221; and paste the path to the class. For example,<br />
<code>import flash.events.Event;</code></li>
</ol>
<p>If you are using code on the Timeline, this can be placed at the top of the Actions panel. If you have written a class file that is referencing another class, this code appears between the <code>package {</code> curly brace, and class declaration, <code>public class MyClass</code>.</p>
<p>For example,</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: #808080; font-style: italic;">// class import statements appear here</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Technical Overview</h3>
<p>A class file is an external text file that contains variables and functions (known as properties and methods in the object-oriented programming context), essentially this means class files are self-contained blocks of code that contain code related to a particular task. To access these variables and functions within your own block of code the class file needs to be imported. If your code contains any references to a class file that has either: (a) not been imported, or (b) is not within the same folder as your .fla or .as files, then a #1046 error will be thrown. For example, suppose you have the following class file declared for a button:</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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span> , _clicked <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> _clicked<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: #808080; font-style: italic;">// button was clicked!</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Testing this code would produce the following error:<br />
<code><br />
1046: Type was not found or was not a compile-time constant: MouseEvent.<br />
</code></p>
<p>With the source listed as&#8230;<br />
<code><br />
private function _clicked( evt:MouseEvent ) : void<br />
</code></p>
<p>This is because the class <code>MouseEvent</code> is referenced in the code, but is not imported at the top of the class file. The fix is simple and looks like this:</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: #0066CC;">MovieClip</span>;
	<span style="color: #808080; font-style: italic;">// import the MouseEvent class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span> , _clicked <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> _clicked<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: #808080; font-style: italic;">// button was clicked!</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Typically if you are a beginning ActionScripter, you will run into this error when referencing classes created by Adobe. However, as you become more advanced in your scripting abilities you may place your own custom class files in &#8220;packages&#8221; that would then need to be imported using the same above process. In case you are wondering what the stuff is before the actual class name, such as &#8220;flash.display&#8221; and &#8220;flash.events,&#8221; this is the &#8220;package&#8221; that class resides in, which is essentially the folders the class file resides in on disk. For example, suppose I created a class called &#8220;MyButton&#8221; and placed it in the package &#8220;com.anselmbradford.&#8221; This class would appear in a text file called &#8220;MyButton.as&#8221; that would be in a folder called &#8220;anselmbradford&#8221; that would itself be in a folder called &#8220;com.&#8221; The class file would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">anselmbradford</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>To import this class file into another class file I would place the &#8220;com&#8221; folder (containing the class file two levels in) in the same directory as my .fla or .as file that I wanted to import this class into. I would then import it using the syntax shown previously. For example:</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: #0066CC;">MovieClip</span>;
	<span style="color: #808080; font-style: italic;">// class is imported</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">anselmbradford</span>.<span style="color: #006600;">MyButton</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// class is referenced in code, so must be imported</span>
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:MyButton = <span style="color: #000000; font-weight: bold;">new</span> MyButton<span style="color: #66cc66;">&#40;</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>Note: Whole sets of class files that are within the same package can be imported using the &#8220;*&#8221; wildcard operator. For example, <code>flash.events.Event</code>, and <code>flash.events.MouseEvent</code>, both reside in the <code>flash.events</code> package. Instead of having two lines to import both of these classes, you can have <code>import flash.events.*;</code>, which will import all the necessary classes from the <code>flash.events</code> package.</p>
<p><strong>UPDATE October 4, 2009:</strong>This error will also occur if you have a symbol on your stage that has an instance name that is the same as a class name associated with the symbol. Check the Library panel, under the Linkages column, does any of your symbols have a name next to &#8220;Export:&#8221; that is the same as the instance name in the Properties panel of one of those symbols on the stage?</p>
<p><img src="http://blog.anselmbradford.com/wp-content/uploads/2009/03/type_error.png" alt="type_error" title="type_error" width="267" height="216" class="alignnone size-full wp-image-970" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/03/25/common-flash-compiler-errors-1046/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Common Flash Compiler Errors: #1042</title>
		<link>http://blog.anselmbradford.com/2009/03/19/common-flash-compiler-errors-1042/</link>
		<comments>http://blog.anselmbradford.com/2009/03/19/common-flash-compiler-errors-1042/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 01:26:47 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=729</guid>
		<description><![CDATA[This error shows the following in the Compiler Errors window: 1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code. Quick Answer and Solution Most likely you are you using the keyword &#8220;this&#8221; inside a class file, but it is not [...]]]></description>
			<content:encoded><![CDATA[<p>This error shows the following in the Compiler Errors window:</p>
<p><code><br />
1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.<br />
</code></p>
<h3>Quick Answer and Solution</h3>
<p>Most likely you are you using the keyword &#8220;this&#8221; inside a class file, but it is not inside the curly braces of a function. By double-clicking on the error it will take you to the offending line of code. Check carefully to make sure where you have placed the keyword &#8220;this&#8221; is actually between the opening &#8220;{&#8221; and closing &#8220;}&#8221; braces of a function declaration. If it looks correct, but is accompanied by error #1126 (<a href="/2009/03/13/common-flash-compiler-errors-1126/">described here</a>), then fix that error first. Also, if the &#8220;this&#8221; keyword appears in a function that has the &#8220;static&#8221; keyword in its declaration, error #1042 will also be thrown.</p>
<p>The following are three scenarios where a class file will throw this error:</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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">// &quot;this&quot; keyword used outside a function</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// function does not have a body because of misplaced semicolon,</span>
		<span style="color: #808080; font-style: italic;">// so error #1126 is thrown</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">hide</span><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: #808080; font-style: italic;">// the &quot;this&quot; keyword is actually not in the body of a function</span>
			<span style="color: #808080; font-style: italic;">// because of the above #1126 error</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// function is a &quot;static method,&quot; meaning it runs inside the class,</span>
		<span style="color: #808080; font-style: italic;">// not the object created from the class</span>
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">hide</span><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: #808080; font-style: italic;">// the &quot;this&quot; keyword is inside a static method</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The corrected form of the above examples would be:</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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">hide</span><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: #808080; font-style: italic;">// the reference to &quot;this&quot; is inside a non-static function</span>
			<span style="color: #808080; font-style: italic;">// (called an instance method)</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Technical Overview</h3>
<p>There are two types of methods (functions) that can appear in a class file, static methods and instance methods. Static methods refer to methods that are run via the class itself, while instance methods refer to methods run via an instance of the class. Consider the following class file:</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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> staticMethod<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;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;static method called!&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> instanceMethod<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;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;instance method called!&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></pre></div></div>

<p>To call the first method, a new &#8220;<code>Main</code>&#8221; object does not need to be created, as the static method can be called on the class itself, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">Main.<span style="color: #006600;">staticMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Conversely, to access the other method, an (object) instance of the <code>Main</code> class needs to be created and then the method can be called through that object, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> main:Main = <span style="color: #000000; font-weight: bold;">new</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
main.<span style="color: #006600;">instanceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Use of the &#8220;this&#8221; keyword refers to the object instance of the class that the code is currently dealing with (in the above case if <code>this</code> appeared in the body of the <code>instanceMethod()</code> method it would be referring to the object placed in the <code>main</code> variable). Since no object instance has to be created to use static methods, the &#8220;this&#8221; keyword can not be used within those methods because there is no object in existence to refer to. Additionally, all code outside of a function declaration runs <span style="text-decoration: underline;">before</span> an object instance is created (before the constructor function runs), so any references to &#8220;this&#8221; do not have an object to refer to in that context either.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/03/19/common-flash-compiler-errors-1042/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Common Flash Compiler Errors: #1126</title>
		<link>http://blog.anselmbradford.com/2009/03/13/common-flash-compiler-errors-1126/</link>
		<comments>http://blog.anselmbradford.com/2009/03/13/common-flash-compiler-errors-1126/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 22:12:00 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=686</guid>
		<description><![CDATA[This error shows the following in the Compiler Errors window: 1126: Function does not have a body. Quick Answer and Solution By double-clicking on the error it will take you to the offending line of code. This error commonly happens when the end of line designator &#8220;;&#8221; is placed at the end of a function [...]]]></description>
			<content:encoded><![CDATA[<p>This error shows the following in the Compiler Errors window:</p>
<p><code><br />
1126: Function does not have a body.<br />
</code></p>
<h3>Quick Answer and Solution</h3>
<p>By double-clicking on the error it will take you to the offending line of code. This error commonly happens when the end of line designator &#8220;<code>;</code>&#8221; is placed at the end of a function name declaration, as in:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><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: #808080; font-style: italic;">/*function body*/</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice the presence of the semicolon at the end of line 1. To fix the error the semicolon is removed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><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: #808080; font-style: italic;">/*function body*/</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>Technical Overview</h3>
<p>A function is a block of code that will only run when it is &#8220;called,&#8221; meaning the above example would be run by writing the function name elsewhere, like so: <code>init();</code>. When a particular function is called, the code that appears between the curly braces (&#8220;{}&#8221;) is run. This is referred to as the function &#8220;body.&#8221; When you test a movie in Flash the code is read line by line by the compiler and eventually turned into a SWF. However, what you see as a single line of code and what the compiler sees as a single line of code are two different things. The compiler ignores extra white space and carriage returns. For example, to the compiler the following function declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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: #808080; font-style: italic;">/*function body*/</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>&#8230;is the same as:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><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: #808080; font-style: italic;">/*function body*/</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Because of this, it depends on the semicolon and closing curly brace (&#8220;}&#8221;) to denote that a line of code has ended. The closing curly brace is used for every piece of code that creates a block of code (functions, for example), while the semicolon is used for everything else. If a semicolon were to appear before the opening curly brace (&#8220;{&#8220;) in the above line of code, the compiler would hit the semicolon and throw an error that the function did not have a &#8220;body&#8221; because it had reached the end of the line of code without encountering an opening and closing curly brace. </p>
<p><strong>Note:</strong> The one exception to this rule is the constructor function found in class files, which can oddly enough contain a semicolon after the function name declaration and the compiler will not complain, for example the following will not throw an error:</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: #0066CC;">MovieClip</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">/*constructor function body*/</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>However, while the compiler will not complain, the constructor function actually ends at the semicolon, so what&#8217;s written between the curly braces (the &#8220;constructor function body&#8221;) will not be run when a new object of this class is instantiated (well, actually it will run, but it will be in the scope of the class, not the object, so it will run prior to the creation of the object). Constructor functions are different in one other aspect as well, they do not specify a return type (no &#8220;<code>:void</code>&#8221; after the parentheses, for example).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/03/13/common-flash-compiler-errors-1126/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Flash Compiler Errors: #1009</title>
		<link>http://blog.anselmbradford.com/2009/02/26/common-flash-errors-1009-cannot-access-a-property-or-method-of-a-null-object-reference/</link>
		<comments>http://blog.anselmbradford.com/2009/02/26/common-flash-errors-1009-cannot-access-a-property-or-method-of-a-null-object-reference/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 02:36:54 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=661</guid>
		<description><![CDATA[My students often run into the same kinds of errors, so I thought I would post some info on the more common ones. One very common error to receive in Flash is this one: TypeError: Error #1009: Cannot access a property or method of a null object reference. Quick Answer and Solution Somewhere in your [...]]]></description>
			<content:encoded><![CDATA[<p>My students often run into the same kinds of errors, so I thought I would post some info on the more common ones. One very common error to receive in Flash is this one:</p>
<p><code><br />
TypeError: Error #1009: Cannot access a property or method of a null object reference.<br />
</code></p>
<h3>Quick Answer and Solution</h3>
<p>Somewhere in your code there is a variable that you are using to refer to a property or method of an object (such as a movie clip), however, it does not actually contain an object so it throws an error. Look for the parts of your code where you have a dot following a variable name and some property or method, such as <code>myMC.width</code> or <code>myMC.startDrag()</code>. Place a trace statement above this variable to see if its value is <code>null</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> myMC <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// if this lists &quot;null&quot; in the output window the next line of code will throw an error</span>
<span style="color: #808080; font-style: italic;">// look for the first occurrence of myMC (where it was created) to see why it does not </span>
<span style="color: #808080; font-style: italic;">// contain a reference to something other than null</span>
myMC.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>;</pre></div></div>

<h3>Technical Overview</h3>
<p>Objects are collections of variables and functions that are defined in a class file. In the context of Object-Oriented Programming, variables are referred to as properties, and functions are referred to as methods. A movie clip object (which is an instance of the MovieClip class) contains properties and methods for dealing with the appearance and interactivity of movie clips, such as <code>x</code>, <code>y</code>, <code>width</code>, <code>height</code>, <code>startDrag()</code>, <code>addEventListener(.)</code>, etc. When a new object is created it is typically stored in a variable—technically meaning a reference to the object is stored in the variable, not the actual object—the variable acts like a unique identifier to lookup the object within a particular section of code (called the scope of the variable). </p>
<p>For example, a new movie clip object can be created with <code>new MovieClip()</code>, this would then be stored in a variable (named <code>myMC</code> in this example) with the code <code>var myMC:MovieClip = new MovieClip();</code>. Properties and methods of this movie clip object can be accessed using dot notation. For example, to return the width in pixels of the movie clip, the following may be typed:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myMC:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> myMC.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// outputs 0 because the movie clip doesn't have any graphics, </span>
<span style="color: #808080; font-style: italic;">// and is therefore 0 pixels wide</span></pre></div></div>

<p>If the variable was created, but not set to reference a new movie clip object instance, it will be empty and contain the special value of <code>null</code>, denoting the absence of a reference to an object. Trying to access a property or method via the variable would then mean the computer would attempt to find the property or method of a <code>null</code> value, which being a placeholder for &#8220;no value,&#8221; throws an error.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myMC:<span style="color: #0066CC;">MovieClip</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> myMC.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// throws error #1009 because the variable myMC does not contain a reference to an object</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/02/26/common-flash-errors-1009-cannot-access-a-property-or-method-of-a-null-object-reference/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

