<?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>iD Tech Camps - America's #1 Tech Camp&#187; AS3</title>
	<atom:link href="http://www.internaldrive.com/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.internaldrive.com</link>
	<description>America's #1 Tech Camp</description>
	<lastBuildDate>Thu, 19 Nov 2009 21:24:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Properly Referencing Your Main Class in Flash Actionscript</title>
		<link>http://www.internaldrive.com/2008/12/19/referencing-main-class-in-flash-actionscript/</link>
		<comments>http://www.internaldrive.com/2008/12/19/referencing-main-class-in-flash-actionscript/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 00:25:27 +0000</pubDate>
		<dc:creator>KenK</dc:creator>
				<category><![CDATA[iD Tech Bloggers]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Objects]]></category>
		<category><![CDATA[Stage]]></category>

		<guid isPermaLink="false">http://www.internaldrive.com/?p=20316</guid>
		<description><![CDATA[For those of you moving from Actionscript 2 to 3 (with a heavier focus on proper object-oriented principles), you may find that accessing your Main class and Stage isn&#8217;t as straight-forward. In this post we&#8217;ll highlight organizing your classes to properly reference your main class. First, let&#8217;s take a look at our Main class:
package {
    [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>For those of you moving from Actionscript 2 to 3 (with a heavier focus on proper object-oriented principles), you may find that accessing your Main class and Stage isn&#8217;t as straight-forward. In this post we&#8217;ll highlight organizing your classes to properly reference your main class. First, let&#8217;s take a look at our Main class:</p>
<pre>package {</pre>
<pre>    import flash.display.Stage;</pre>
<pre>    import flash.display.MovieClip;</pre>
<pre>    public class Main extends MovieClip {</pre>
<pre>        private static var _instance:Main = null;</pre>
<pre>        public function Main() {</pre>
<pre>            _instance = this;</pre>
<pre>        }</pre>
<pre>        public static function getInstance():Main { return _instance; }</pre>
<pre>        public static function getStage():Stage { return getInstance().stage; }</pre>
<pre>    }</pre>
<pre>}</pre>
<p>First we define a static variable,<strong> _instance</strong>, to hold a static reference to Main. We are assuming throughout this that we will use Main statically. In other words, there will only be a single instance of our Main class. Next, in our constructor, we use the line <strong>_instance = this;</strong> to set our static version of Main to the one that is created when the constructor is called.</p>
<p>We will use two methods to get access to Main and the main stage. First, <strong>getInstace()</strong> is a static function that always returns a copy of <strong>_instance</strong>. If you want the stage instead, use <strong>getStage()</strong>. All <strong>getStage() </strong>does differently from <strong>getInstance()</strong> is return the <strong>stage</strong> property of <strong>_instance</strong> rather than the object itself. Using this format simplifies your code in other classes.</p>
<p>Now let&#8217;s say you want to use these functions in other classes to add something to the stage. Let&#8217;s pretend we have a Tree class that needs to add itself to the main stage when it is created. You could use this code:</p>
<pre>package {
    import flash.display.Sprite;&lt;/code&gt;

    public class Tree extends Sprite {
        Main.getStage().addChild(this);

        x = Math.random() * Main.getStage().stageWidth;
        y = Math.random() * Main.getStage().stageWidth;
    }
}</pre>
<p>You can see that we can easily get properties of the stage (<strong>stageWidth</strong> and <strong>stageHeight</strong>) simply by statically referencing Main. Note that we could have used the following lines instead:<br />
<code><br />
x = Math.random() * Main.getInstance().stage.stageWidth;<br />
y = Math.random() * Main.getInstance().stage.stageWidth;<br />
</code><br />
It really just depends on which example you find more elegant. One additional benefit of using the techniques described here is that you can use the <strong>getInstance()</strong> to determine if Main has loaded. Simply do:<br />
<code><br />
if (Main.getInstance() != null) { /* some function */ }<br />
</code><br />
Hope that gives you an easy framework for accessing your stage and Main class in AS3!</p>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://www.internaldrive.com/2008/12/19/referencing-main-class-in-flash-actionscript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
