<?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/"
		xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>untoldentertainment.com &#187; CS4</title>
	<atom:link href="http://www.untoldentertainment.com/blog/tag/cs4/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.untoldentertainment.com/blog</link>
	<description>We Make Flash Games</description>
	<lastBuildDate>Wed, 01 Feb 2012 15:18:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<copyright>Copyright &#xA9; untoldentertainment.com 2011 </copyright>
	<managingEditor>ryan@untoldentertainment.com (untoldentertainment.com)</managingEditor>
	<webMaster>ryan@untoldentertainment.com (untoldentertainment.com)</webMaster>
	<image>
		<url>http://www.untoldentertainment.com/blog/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
		<title>untoldentertainment.com</title>
		<link>http://www.untoldentertainment.com/blog</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle></itunes:subtitle>
	<itunes:summary>We Make Flash Games</itunes:summary>
	<itunes:keywords></itunes:keywords>
	<itunes:category text="Society &#38; Culture" />
	<itunes:author>untoldentertainment.com</itunes:author>
	<itunes:owner>
		<itunes:name>untoldentertainment.com</itunes:name>
		<itunes:email>ryan@untoldentertainment.com</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://www.untoldentertainment.com/blog/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<item>
		<title>Tutorial: Understanding Classes in AS3 Part 2</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 13:42:14 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[AS3 Pitfalls]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Video Games]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1661</guid>
		<description><![CDATA[In our first thrilling look at Understanding Classes in Actionscript 3.0, we talked about how Classes are structured, and how you can make that leap from stashing all your code in the first frame of your Flash movie like an IT chipmunk, to moving your code to its own Class. I promised that in Part [...]]]></description>
			<content:encoded><![CDATA[<p>In our first thrilling look at <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">Understanding Classes in Actionscript 3.0</a>, we talked about how Classes are structured, and how you can make that leap from stashing all your code in the first frame of your Flash movie like an IT chipmunk, to moving your code to its own Class.</p>
<p>I promised that in Part 2, we’d learn how to identify chunks of code that should be moved into their own Classes.  But before we do that, it’s important to talk about what a Class <em>is</em>.  And if you haven’t learned how to extend a MovieClip symbol in your library to write a Class around it, this tutorial is for you!</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/egg.jpg" alt="Take THAT, eggheads"></p>
</div>
<p><b>Disclaimer</b> Dear Eggheads: This tutorial is between me, a designer-turned-coder, and readers who want to learn more about Actionscript programming who likely are not Computer Science graduates.  The terminology i use may not be 100% accurate, and some of the things i say may inflame your nerd rage and your unwavering demand for technical accuracy.  But this article is not for you.  Stop reading now. Go write some slash fiction about Captain Kirk totally doing it with the dog from Battlestar Galactica, and leave us alone to learn.</p>
<h2>OOP: There It Is</h2>
<p>A Class is an element of OOP – Object Oriented Programming.  OOP is a flavour of coding where different objects – or <em>things</em> &#8211; interact with each other to solve a problem.  If you’re a game programmer, the problem can be described as “How do i make a fun, functional zombie game?”  The word “object” is synonymous with “Class”.  You could say that Actionscript 3.0 is a <em>Class Oriented Programming</em> language, but the eggheads would stab you in the face.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/nerdRage.jpg" alt="She-Nerd"></p>
<p>She-Nerd SMASH!!
</p></div>
<p>So a Class is a <em>thing</em> &#8211; a chunk, a morsel, a <em>module</em> of code.   By writing your Classes the &#8220;right&#8221; way, you’ll find you can re-use Classes from project to project.  By writing them the &#8220;wrong&#8221; way, you’ll still probably wind up with a project that has nice, well-organized code that makes sense if you re-visit it a year later.  </p>
<p>The three principles or rules to follow if you want to maximize the advantages  of OOP are <b>encapsulation</b>, <b>inheritance</b> and <b>polymorphism</b>.  We’ll be taking a brief, gentle look at <b>inheritance</b> in this tutorial, because i think it’s the easiest to understand when you’re starting out.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/twins.jpg" alt="Inbred Twins"></p>
<p>Herbert n&#8217; Zed inherit their genes from their paw n&#8217; their sis
</p></div>
<p>Classes contain methods and properties.  (Another word for &#8220;method&#8221; is &#8220;function&#8221; or &#8220;behaviour&#8221;.  Another word for &#8220;property&#8221; is &#8220;field&#8221;.)  A method is something a Class can <em>do</em>.  A property is something a Class <em>is</em>.<br />
Let’s explain this somewhat better by using a crappy example that a lot of OOP tutorials use.  You have a Class called “Dog”.   Methods of Dog – things that Dog can <em>do</em> &#8211; include:</p>
<ul>
<li>bark
<li>sniff
<li>humpLeg
</ul>
<p>Properties of Dog &#8211; things that dog <em>is</em> &#8211; include:</p>
<ul>
<li>color
<li>breed
<li>favouriteTreat
</ul>
<p>Another way to think of it is that methods are verbs, and properties are adjectives.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/uglyDog.jpg" alt="Ugly Dog"></p>
<p>The OOP Dog example rears its ugly head
</p></div>
<h2>Let&#8217;s Get Real</h2>
<p>The Dog example is almost entirely useless to us as programmers. In all my years of experience, i have never written a Dog Class.  But i HAVE written my fair share of music and movie players in Flash.  Let&#8217;s look at the methods and properties of a media player.</p>
<p>Methods of our MediaPlayer Class &#8211; things that a media player can <em>do</em> &#8211; include:</p>
<ul>
<li>play
<li>pause
<li>stop
<li>scrub
<li>changeVolume
<li>loadMedia
</ul>
<p>Properties of our MediaPlayer Class &#8211; things that a media player <em>is</em>, or things that describe its <em>state</em> &#8211; include:</p>
<ul>
<li>isPaused
<li>songDuration (or movieDuration)
<li>isFullScreen
<li>volume
</ul>
<p>Note that the <em>method</em> &#8220;changeVolume&#8221; probably changes the <em>property</em> &#8220;volume&#8221;.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/snowths.jpg" alt="Snowths"></p>
<p>We&#8217;re gonna need a method called &#8220;turnVolumeWayWayUp&#8221;
</p></div>
<h2>You&#8217;re Already an Expert</h2>
<p>The good news is that as a Flash developer, you’ve been using Classes this whole time, though you may not have known it.  A MovieClip is a Class.  It has methods and properties.  Let&#8217;s look at some of the methods of a MovieClip &#8211; things a MovieClip can <em>do</em>:</p>
<ul>
<li>gotoAndPlay
<li>stop
<li>hitTestPoint (determine whether a point is colliding with me)
</ul>
<p>And some properties of a MovieClip &#8211; things a MovieClip <em>is</em> (or things that describe its <em>state</em>):</p>
<ul>
<li>x and y
<li>alpha
<li>rotation
</ul>
<p>Eggheads: Are you still reading this?  i told you to take a hike. Don&#8217;t jump down my throat.  i know that a few of these methods and properties actually belong to Classes that MovieClip <em>inherits</em> from, but we&#8217;re just learning, alright?  Leave us alone.  Go out and LARP or something.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/larp.jpg" alt="LARP"></p>
</div>
<h2>No Pain, No Awesome</h2>
<p>Just as you can make the leap from stowing all your code in the first frame of your movie to putting it in an .as Class file, you can (and should) make the leap from having all of your MovieClip instances &#8220;physically&#8221; present on your stage, to keeping them in the library and drawing them to the stage using code.  There are many advantages to using a code-only approach which probably won&#8217;t become clear to you until you go through the excrutiating growing pains of &#8220;WHY THE HELL AM I DOING IT THIS WAY??&#8221; like i did.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/growingPains.jpg" alt="Growing Pains"></p>
<p>Growing Pains: excrutiating
</p></div>
<p>So create a MovieClip in your library.  NOTE: If you don&#8217;t know how to do this, you may already be in over your head.  Go check out a beginner Flash tutorial and come on back.</p>
<p>In order for you to make the MovieClip available to you through code, you need to give it a linkage name. Here&#8217;s how:</p>
<ol>
<li>Open your Library (CTRL+L)
<li>Right-click the MovieClip symbol
<li>In Flash CS3, choose &#8220;Linkage&#8221;.  In Flash CS4, choose &#8220;Propeties&#8221;.
<li>Check the box that says &#8220;Export for ActionScript&#8221;
<li>In the &#8220;Class:&#8221; field, type a logical name for your MovieClip.  It&#8217;s best practice to start this name with a capital letter.  Don&#8217;t use any spaces &#8211; capitalize each new word SoThatItLooksLikeThis.  (This is called &#8220;Pascal case&#8221;)
<li>Click &#8220;OK&#8221;
<li>If you are a Mac user, and you&#8217;re saying &#8220;What&#8217;s a CTRL??  What&#8217;s a right mouse button?? i&#8217;m freaking OUT, man!&#8221;, go buy a PC  ;)
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageCS3.jpg" alt="Linkage CS3"></p>
<p>Setting linkage for a library item in Flash CS3 &#8230;
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageCS4.jpg" alt="Linkage CS4"></p>
<p>&#8230; and in Flash CS4.
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/linkageName.jpg" alt="Linkage Name"></p>
<p>Choosing a linkage name
</p></div>
<p>Did you notice, before you hit OK, that there was a field labelled &#8220;Base Class:&#8221; that said &#8220;flash.display.MovieClip&#8221;?  If this is your first time creating a library linkage, or if it&#8217;s old hat and you&#8217;ve never noticed this field, prepare to <em>blow your own mind</em>: you&#8217;ve just used the first principle of OOP &#8211; <em>inheritance</em>.  </p>
<h2>My Mind Done Blow&#8217;d Up</h2>
<p>Here&#8217;s what&#8217;s going on behind the scenes. &#8220;MovieClip&#8221; is a built-in Class that the creators of Actionscript wrote. When you give your library item a linkage name, you are actually subclassing, or <em>inheriting</em>, from the MovieClip Class.  That means that your new Class &#8220;Dog&#8221; or &#8220;MediaPlayer&#8221; or &#8220;EnemySpaceship&#8221; can now do all the stuff that a MovieClip can do.  It can <b>gotoAndPlay</b>, it can <b>stop</b>, it can <b>hitTestPoint</b>, etc.  It also inherits all of MovieClip&#8217;s properties &#8211; <b>x</b> and <b>y</b>, <b>alpha</b>, <b>rotation</b>, etc. &#8211; things that a MovieClip <em>is</em>, things that describe its <em>state</em>.</p>
<p>So now we&#8217;re getting to the point.  When you create a library linkage like this, Flash is automagically creating a new Class (&#8220;Dog&#8221;, &#8220;MediaPlayer&#8221;, whatever).  You don&#8217;t have to write it.  But you CAN write it, if you want to.  And since that&#8217;s what this tutorial is all about, then oh yes &#8211; we want to.</p>
<h2>Oh Yes &#8211; Let&#8217;s</h2>
<p>Start a new .as Class file.  You can do it in the Flash IDE, you can write it with Notepad or Notepad ++ or any other text editor.  i highly recommend Flash Develop.   Remember our <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/">first Understanding Classes tutorial</a>? Here&#8217;s the structure of a Class:</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/AS3ClassStructure.jpg" alt="AS3 Class Structure"></p>
<p>i chose these colours from Arsenio Hall&#8217;s wardrobe
</p></div>
<p>So here&#8217;s what our empty Class code should look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td 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 statement(s)</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dog <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;">// Class definition</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> isBarking:<span style="color: #0066CC;">Boolean</span>; <span style="color: #808080; font-style: italic;">// Instance variable(s)</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Dog<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</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;i am a dog&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Other functions</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> bark<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			isBarking = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> stopBarking<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			isBarking = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s take note of a few important things.</p>
<ol>
<li>i&#8217;m calling my Class &#8220;Dog&#8221;.  That&#8217;s because the linkage name in used in the Flash library was &#8220;Dog&#8221;.  Note: not &#8220;dog&#8221; or &#8220;DogClip&#8221; or anything.  D-o-g.  Same capitalization, exact same name.
<li>And note that the linkage name can be <em>completely different</em> from the library symbol name.  My library symbol could be called &#8220;This is a thing what i created in teh Flashs&#8221;.  Doesn&#8217;t matter.  Actionscript can&#8217;t &#8220;see&#8221; the library item name.  It can only &#8220;see&#8221; the linkage name.
<li>Finally, remember that the Class file name has to be the same as the Class name.  If our Class is called <b>Dog</b>, we save the file as <b>Dog.as</b>.  Pull any other shenanigans, and you&#8217;ll get errors.
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_09/sillyDog.jpg" alt="Silly dog"></p>
<p>i SAID &#8220;no shenanigans&#8221;, DOG
</p></div>
<p>i mentioned mysteriously in the last tutorial that the word &#8220;extends&#8221; is an inheritance word.  When you create a linkage name in the library, Flash automagically writes a Class for you, and automagically <em>extends</em> (inherits from) MovieClip.  Now that we&#8217;re writing the Class ourselves, nothing is automagic.  We have to explicitly say that Dog (or whatever) extends (inherits from) the MovieClip Class.  When this is all done correctly, Flash understands that there is a connection between the linked library Class &#8220;Dog&#8221;, and the &#8220;Dog.as&#8221; Class we just wrote.</p>
<p>You may or may not know the code for dynamically placing a linked library item on the stage.  Here it is, using Dog as the example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> dog:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #808080; font-style: italic;">// or var dog:Dog = new Dog();</span>
addChild<span style="color: #66cc66;">&#40;</span>dog<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>&#8220;BUT WAIT!&#8221;, you say. &#8220;I THOUGHT DOG HAD TO START WITH A CAPITAL LETTER??&#8221;  Okay, first of all, stop yelling. Lower-case &#8220;dog&#8221; is the name of the variable that contains an instance (a stamp, a copy) of our master Dog Class.  We could use &#8220;dog1&#8243;, &#8220;dog2&#8243;, or even &#8220;thisIsACopyOfDog&#8221;.</p>
<p>When you addChild(dog), you add your instance (stamp, copy) of the Dog Class to the list of things for Flash to draw to the stage (the viewable area that the player sees).</p>
<p>To confirm that your custom library Class is working, test your movie.  You should see the trace action &#8220;i am a dog&#8221; in the Output window. (Press F2 to see the Output window &#8211; the trace action won&#8217;t appear on the stage)</p>
<h2>What&#8217;s the Point?</h2>
<p>You may be wondering why we went through all of this agony.  The easy way is just to draw a dog and make it a MovieClip, and then leave it on the stage.  There are many, many advantages to what we&#8217;ve just done.  Let me list a few:</p>
<ul>
<li>You can now add as many Dogs to the stage as you like <em>dynamically</em>. Until now, you had to add them by hand.  If you needed another dog, you (the creator) had to drag one out of the library.  Now you can create an &#8220;Add Dog&#8221; button. When the player presses it at run-time, your code dynamically places dogs on the stage.  Now the code is doing the work, not you.
<li>We&#8217;re edging closer to the next OOP principle, <em>encapsulation</em>.  Within our Dog class, we can store methods (things that a Dog <em>does</em>) and properties (things that a Dog <em>is</em>).  Every time we initialize (make a stamp or copy of) Dog, Dog can run a bunch of Dog-specific setup routines:  initializeSenseOfSmell, testBarkTone, prepareToChewShoes.  If you do it &#8220;right&#8221;, you&#8217;ve created a self-reliant, self-contained Class.  Now every time you have a project that needs a Dog (or a MediaPlayer, an ImageGallery, a CountdownClock), you can just copy that .as Class file to a new project. If you&#8217;ve done everything &#8220;right&#8221;, it&#8217;ll just <em>work</em>.
<li>The more you start abstracting your thinking, the more you&#8217;ll be able to write generic Classes that do a number of different jobs.  You might not have another project that needs a Dog, but your generic Animal Class would still do the trick.
<li>There&#8217;s a problem that Actionscript 3 introduced where you can&#8217;t always access certain pieces of MovieClips until they&#8217;re drawn to the screen.  Writing a custom Class for your library elements can overcome this problem.
</ul>
<h2>What&#8217;s Next?</h2>
<p>Moving your frame code to an .as Class file was the first step. Learning how to write custom Classes for your linked library items is the next step, since it builds on (hopefully) familiar MovieClip territory and your existing knowledge.  In the next tutorial, we&#8217;ll take a look at that big wad of code in your Main.as file and try to figure out which methods and properties can be logically grouped and moved into their own Classes.</p>
<p>For more tips, tricks and tutorials, check out our <b><a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a></b> feature.</p>
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F09%2Ftutorial-understanding-classes-in-as3-part-2%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F09%2Ftutorial-understanding-classes-in-as3-part-2%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1661&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Tutorial &#8211; Fade Movie Clips in and Out Solidly</title>
		<link>http://www.untoldentertainment.com/blog/2009/09/08/tutorial-fade-movie-clips-in-and-out-solidly/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/09/08/tutorial-fade-movie-clips-in-and-out-solidly/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 15:12:10 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Awesomazing]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1654</guid>
		<description><![CDATA[Chalk this one up to Things Jeff Taught Me. Jeff Gold (ue_jeff) is our Game Developer and, as such, is terribly helpful. It amazes me that i can use Flash professionally for ten years and i still find myself discovering features that have existed for many versions. This is one feature that Jeff tipped me [...]]]></description>
			<content:encoded><![CDATA[<p>Chalk this one up to Things Jeff Taught Me.  Jeff Gold (<a href="http://www.untoldentertainment.com/boards/memberlist.php?mode=viewprofile&#038;u=55">ue_jeff</a>) is our Game Developer and, as such, is terribly helpful.  It amazes me that i can use Flash professionally for ten years and i still find myself discovering features that have existed for many versions.  This is one feature that Jeff tipped me off to.</p>
<p>Have you ever noticed when you fade a vector MovieClip in or out in Flash, that it goes all ghosty / X-ray and you can see the separate layers inside of it?  This has always driven me ninety-nine percent nuts, and i&#8217;ve cooked up work-arounds that essentially flatten the artwork before i fade the clip.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_08/ghostbusters.jpg" alt="Ghostbusters Logo"></p>
<p>Bustin&#8217; makes me feel good
</p></div>
<p>Here&#8217;s what that horrendous ghosty alpha fade looks like:</p>
<p><center><br />

<object width="250" height="150">
<param name="movie" value="http://www.untoldentertainment.com/blog/img/2009_09_08/bloat_badFade.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></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="250" height="150" src="http://www.untoldentertainment.com/blog/img/2009_09_08/bloat_badFade.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
</center></p>
<p>You can totally tell that the blimple&#8217;s four legs are separate MovieClips because they fade in and out independantly of the body.</p>
<p>You can avoid the X-ray effect using a Blend Mode called &#8220;Layer&#8221;.</p>
<ol>
<li>Select your MovieClip
<li>Open the Properties panel
<li>In the &#8220;Blend&#8221; drop-down list, choose &#8220;Layer&#8221;
</ol>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_09_08/blendModeLayer.jpg" alt="Flash Blend Mode Layer"></p>
<p>The process is identical in Flash CS4 and Flash CS3
</p></div>
<p>Now, when the clip fades in and out, it looks decent and is presentable in polite society:</p>
<p><center><br />

<object width="250" height="150">
<param name="movie" value="http://www.untoldentertainment.com/blog/img/2009_09_08/bloat_goodFade.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></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="250" height="150" src="http://www.untoldentertainment.com/blog/img/2009_09_08/bloat_goodFade.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
</center></p>
<p>Cheers, Jeff!</p>
<p>Fancy blend modes may affect performance, so don&#8217;t go nuts with them.  But if you need a clip here and there to look proper and you don&#8217;t have time for an artwork-flattening work-around, give this a shot.</p>
<p>To read more tutorials on Flash and Actionscript, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F08%2Ftutorial-fade-movie-clips-in-and-out-solidly%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F09%2F08%2Ftutorial-fade-movie-clips-in-and-out-solidly%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/09/08/tutorial-fade-movie-clips-in-and-out-solidly/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1654&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/09/08/tutorial-fade-movie-clips-in-and-out-solidly/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial: Understanding Classes in AS3 Part 1</title>
		<link>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/</link>
		<comments>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 13:31:29 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=1527</guid>
		<description><![CDATA[A reader named Ryan (no relation) recently requested that we create a post to help him and other beginners understand Classes. So this is an ultra-gentle primer on Classes &#8211; what they are, when to use them, and how to write them. If you&#8217;re stuck in an Actionscript 2 rut, or you&#8217;re new to Actionscript [...]]]></description>
			<content:encoded><![CDATA[<p>A reader named Ryan (no relation) recently requested that we create a post to help him and other beginners understand Classes.  So this is an ultra-gentle primer on Classes &#8211; what they are, when to use them, and how to write them.  If you&#8217;re stuck in an Actionscript 2 rut, or you&#8217;re new to Actionscript 3 and it&#8217;s <em>blowing your mind</em>, this should help ease you in a bit better.</p>
<p>The path i travelled from complete code newbie to the shock-inducing AS3 god that i am today ( er &#8230; <em>hardly</em>) went like this:</p>
<p><b>Beginner</b> &#8211; i put my Actionscript code <em>everywhere</em> &#8211; on different frames of the timeline, embedded in nested MovieClips &#8211; if i can write code on it, i do.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/spaghetti.jpg" alt="Spaghetti Code"></p>
<p>That&#8217;s called &#8220;spaghetti code&#8221;, and it makes your life as a programmer difficult.
</p></div>
<p><b>Intermediate</b> &#8211; Scattering code through my Flash movie makes my project an enormous pain to manage and update.  i&#8217;m going to put ALL of my code on the very first frame of my movie, so i only have to look in one place for it.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/firstFrame.jpg" alt="First Frame"></p>
<p>Placing all your code in Frame 1 was a good idea with AS2, but you can do better
</p></div>
<p><b>Pro</b> &#8211; i don&#8217;t understand Classes, but i have to take the plunge somehow.  i&#8217;m going to move all of my first-frame code into a separate .as Class file. i hope that&#8217;ll be the first step to breaking everything up into Classes.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/codeGoesHere.jpg" alt="Code Goes Here"></p>
<p>This is what we&#8217;re striving for
</p></div>
<p><b>Shock-Inducing AS3 God</b> &#8211; Man almighty &#8211; packing an entire project&#8217;s code into one .as Class file makes it an enormous pain to manage and update.  i&#8217;m going to identify little modules of code and split them off into their own .as Class files.  Hey &#8230; now i&#8217;m starting to use Classes like a superstar!  </p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/god.jpg" alt="God"></p>
<p>Not *THE* God &#8211; just *A* god
</p></div>
<h2>The Road to Pro</h2>
<p>In this tutorial, we&#8217;ll help you move from <b>Intermediate</b> to <b>Pro</b> by moving your first-frame code to its own Class in a separate .as file.  In Part 2, we&#8217;ll take you from <b>Pro</b> to <b>God</b> by discussing ways in which you can split that enormous file up into smaller Classes, and how to figure out which pieces would make good Classes.</p>
<p>Many of the AS3 tutorials you see on the Internatz are written so that you can just paste the code into the first frame and compile the project.  This is because it&#8217;s easier on the tutorial writers &#8211; they assume that you know how to re-write that code to put it in its own Class.  But what if you don&#8217;t know?</p>
<p>Let&#8217;s take a piece of frame code, and bring it outside our Flash .fla file into a separate .as Class file.  Here&#8217;s the frame code we&#8217;ll be using:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> speed:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span>;
<span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>;
&nbsp;
addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  ball.<span style="color: #006600;">x</span> += speed;
  ball.<span style="color: #006600;">y</span> += speed;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This code grabs a MovieClip called &#8220;Ball&#8221; from the library and places it on the stage.  On every frame, the ball moves to the right 5 pixels and down 5 pixels.  We&#8217;re going to pretend that this code exists on Frame 1 of our Flash movie.  (<em>Note: you don&#8217;t need to copy/paste this code and try it, but go ahead.  If you&#8217;re not sure how to create a ball MovieClip and link it in the library, you might be in over your head already.  Read up a bit on super-basic Flash stuff, and we&#8217;ll see you again shortly.</em>)</p>
<p>Now let&#8217;s create an empty, external .as Class file to hold all this code.  So your project will consist of two files: the Flash .fla file, which has a Ball Movieclip in the library, and a Class file with an .as extension.  This isn&#8217;t anything fancy &#8211; an .as file is just a plaintext (.txt) file that you save with a .as extension.  So yes, you can even write this file using Notepad or your favourite text editor.</p>
<p>The .fla and the .as file should exist side-by-side in the same directory.</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/sideBySide.jpg" alt="Side by side"></p>
<p>Same directory.  We can get fancy later.
</p></div>
<h2>The Care and Feeding of Classes</h2>
<p>Classes demand a particular structure:</p>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructure.jpg" alt="AS3 Class Structure Diagram"></p>
</div>
<p>Notice a few things:</p>
<ol>
<li><font color="#009900">Package</font> (whatever that is) wraps the whole thing
<li>the <font color="#003399">Import statements</font> (whatever <em>they</em> are) come next
<li>the <font color="#FF3366">Class Definition</font> (huh?) wraps everything else
<li>the <font color="#CC6600">Instance Variables</font>, <font color="#660066">Constructor Function</font> and other methods/functions are tucked inside the <font color="#FF3366">Class</font> wrapper.  You can put these three things in any order, but this is how you&#8217;ll usually see them in the wild.
</ol>
<h2>Package</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructurePackage.jpg" alt="AS3 Class Structure Package Diagram"></p>
</div>
<p>Let&#8217;s deal with that Package thing first.  The Package declaration is a way of grouping all your Classes together. Let&#8217;s say you&#8217;re working on a project with Bob. You have a class called &#8220;Stuff&#8221;, and Bob has a class called &#8220;Stuff&#8221;.  By uniquely naming your Package, your classes won&#8217;t conflict.</p>
<p>But are you working with Bob right now?  Are you on a big project with multiple programmers?  No.  So don&#8217;t worry about it.  Forget Bob &#8211; just start with a generic Package declaration, which looks like this:</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;">package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #808080; font-style: italic;">// All the other stuff goes in here, between those curly brackets</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>(note that &#8220;package&#8221; has a lower-case &#8220;p&#8221; &#8230; i&#8217;ve used an upper-case &#8220;P&#8221; in the article just to make the word pop out at you)</p>
<h2>Import Statements</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureImportStatements.jpg" alt="AS3 Class Structure Import Statements Diagram"></p>
</div>
<p>Different parts of the ActionScript 3 language are already split up into packages.  When you put your code on a frame in Flash, all of those packages are automatically accessible.  You don&#8217;t have to do any extra work.</p>
<p>But when you work outside Flash, you have to tell the compiler to go grab portions of the AS3 language before you can use them.  So if you say this in an .as Class file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myclip:<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>;</pre></td></tr></table></div>

<p>the compiler will throw you an error, effectively saying &#8220;What&#8217;s a MovieClip??&#8221;</p>
<p>Use an Import Statement to explicitely tell the compiler that you are using a certain part of the AS3 language.  This is a giant pain in the ass, and a real roadblock when you&#8217;re starting out. As a beginner, you don&#8217;t know the package names you&#8217;ll need.  This is just a painstaking trial-and-error period of looking in the documentation, or referring to other people&#8217;s code, and eventually you&#8217;ll memorize this stuff.  (Or you can use a program like <a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">Flash Develop</a>, which automagically figures some of it out for you.)</p>
<p>So if you use a line like this later on in your code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myclip:<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>;</pre></td></tr></table></div>

<p>you have to include an Import Statement like this at the top of the file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;</pre></td></tr></table></div>

<p>(note: small &#8220;i&#8221; on &#8220;import&#8221;)</p>
<p>It&#8217;s like you&#8217;re saying &#8220;OK, compiler &#8211; we&#8217;re going to talk about fish today.  Go get that fish book out of the library so that we&#8217;re all on the same page.&#8221;</p>
<h2>Class Definition</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureClassDefinition.jpg" alt="AS3 Class Structure Class Definition Diagram"></p>
</div>
<p>This is where you actually start writing your Class.  Here&#8217;s the format:</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: #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;">// The Instance Variables, the Constructor Function and Methods go here</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break down that line piece by piece:</p>
<p><font color="#FF3366"><b>public</b> Class Main extends MovieClip</font></p>
<p>We don&#8217;t really need to bother with understanding public vs. private vs. protected vs. internal.  Those keywords are all in the same family, but here&#8217;s what you need to know: you gotta use &#8220;public&#8221; here.  Don&#8217;t worry about the other possibilities until you&#8217;ve got a handle on the basics.  If you don&#8217;t use &#8220;public&#8221; in the Class that replaces your first-frame code, you&#8217;ll get an error.</p>
<p><font color="#FF3366">public <b>Class</b> Main extends MovieClip</font></p>
<p>We&#8217;re writing a Class!  i&#8217;m getting all tiingly.</p>
<p><font color="#FF3366">public class <b>Main</b> extends MovieClip</font></p>
<p>This is the name of the Class.  Feel free to use your creativity here &#8211; you can call it whatever you like.  But there are a few important rules:</p>
<ol>
<li>You must use standard naming conventions. Don&#8217;t start the name with a number, and stay away from spaces and special characters.
<li>Use a capital letter to begin your Class name
<li>When you save the file, the file MUST have the same name as your Class.  If your Class is called Main, save the file as Main.as.  If it&#8217;s called MyProject, save the file as MyProject.as.   If you don&#8217;t do this, there will be trouble.
</ol>
<p><font color="#FF3366">public class Main <b>extends</b> MovieClip</font></p>
<p>In earlier attempts to understand Classes and Object Oriented Programming (OOP), you may have heard of the concept of &#8220;inheritance.&#8221;  That&#8217;s what the &#8220;extends&#8221; keyword is all about.  Without exploring too deeply, we need to <em>extend</em> / subclass / inherit from an ActionScript 3 Class called MovieClip.  Why?  Because if we don&#8217;t, we&#8217;ll get yelled at.  So just do it.  Strive to understand it later.</p>
<p><font color="#FF3366">public class Main extends <b>MovieClip</b></font></p>
<p>That&#8217;s the Class we&#8217;re inheriting from.  It&#8217;s like saying &#8220;my Class called Main can do everything a MovieClip can do, and more.&#8221;</p>
<h2>Instance Variables</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureInstanceVariables.jpg" alt="AS3 Class Structure Instance Variables Diagram"></p>
</div>
<p>Instance Variables, sometimes called Fields, are variables that you need to access throughout your Class.  If you&#8217;re familiar with the idea of scope, you&#8217;ll know that when we do this:</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> myFirstFunction<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: #000000; font-weight: bold;">var</span> myVariable:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;someValue&quot;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>we&#8217;ll get an error if we try doing this:</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> mySecondFunction<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>myVariable<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s because myVariable is <em>scoped</em> to myFirstFunction.  It doesn&#8217;t exist outside that function.</p>
<p>If you want variables to be accessible to all the different functions in your Class, you declare them in the orange area on the diagram.  You&#8217;ll also need to decide whether you&#8217;re making these variables public, private, internal, protected, etc.  This is not a decision you need to make when all your code is on the first frame.  OOP purists would absolutely FREAK OUT if they were reading this, but i&#8217;m going to recommend you make all your variables public for now.  It&#8217;s the least error-prone approach when you&#8217;re learning, and when you know more, you can get a little fancier.</p>
<p>So here&#8217;s what a typical Instance Variable would look like at this point in the .as Class file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> myVariable:<span style="color: #0066CC;">String</span>;</pre></td></tr></table></div>

<p>Notice that we&#8217;re not even setting a value yet.  We can, but we don&#8217;t have to.  We&#8217;re just reserving a spot in the computer&#8217;s memory for this variable so that the whole Class can use it.</p>
<h2>The Constructor Function</h2>
<div class="dislplayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/AS3ClassStructureConstructorFunction.jpg" alt="AS3 Class Structure Constructor Function Diagram"></p>
</div>
<p>This Main Class will be what&#8217;s called the &#8220;point of entry&#8221; into our Flash program.  It&#8217;s the first place where code will start being activated.  A Constructor Function is like the point of entry in any Class.  It&#8217;s the first function that&#8217;s fired when a Class is encountered by the interpreter.</p>
<p>Here&#8217;s what our Constructor Function should look like:</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: #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>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As with Classes, you have to follow a few rules with your Constructor Function:</p>
<ol>
<li>The Highlander rule: &#8220;there can be only one&#8221;.  i believe other languages allow you to have multiple Constructor Functions, but AS3 does not.
<li>The name of the Constructor function must be the same as the name of the Class.
<li>Make sure it&#8217;s public.
<li>Don&#8217;t return a value &#8211; not even &#8220;void&#8221;.  Constructor Functions are not allowed to return a value.
</ol>
<h2>Putting It All Together</h2>
<p>Let&#8217;s slap it all together and see what we&#8217;ve got:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td 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;">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;">// instance variables go here</span>
&nbsp;
        <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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;It works!&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>  
&nbsp;
        <span style="color: #808080; font-style: italic;">// other functions can go here</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Super!  We&#8217;ve got the shell of a Class that we can use as our point of entry. (Make sure it&#8217;s saved as &#8220;Main.as&#8221;, or whatever you called your Class.)  We&#8217;re just missing one step.  We have to <em>tell</em> Flash that instead of looking to the first frame of the movie as our point of entry, we want it to look at the &#8220;Main&#8221; Class.</p>
<p>Open up the Properties panel in Flash and look for an empty field labelled &#8220;Document class:&#8221; (CS3) or &#8220;Class:&#8221; (CS4).</p>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/documentClassCS3.jpg" alt="Document Class Flash CS3"></p>
<p>This is what it looks like in Flash CS3
</p></div>
<div class="displayed">
<p><img src="http://www.untoldentertainment.com/blog/img/2009_08_25/documentClassCS4.jpg" alt="Document Class Flash CS4"></p>
<p>&#8230; and in CS4
</p></div>
<p>Note: you won&#8217;t be able to use this feature unless you&#8217;re targeting Flash Player 9 or above, with ActionScript 3 or above.</p>
<p>Type &#8220;Main&#8221; (without the quotes) into this field.  Then save and compile.  You should see the trace statement &#8220;It works!&#8221; in the Ouput panel.</p>
<h2>All the Rest</h2>
<p>Now that we have Flash looking to an external .as file to get the party started, let&#8217;s port over some of that first-frame code into our class.  i&#8217;ll paste the clean version first, and then an identical copy with extensive comments so that you can follow along at home:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td 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: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
        <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>
             ball = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
             addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>;
             ball.<span style="color: #006600;">speed</span> = <span style="color: #cc66cc;">5</span>;
&nbsp;
             addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<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> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
                ball.<span style="color: #006600;">x</span> += ball.<span style="color: #006600;">speed</span>;
                ball.<span style="color: #006600;">y</span> += ball.<span style="color: #006600;">speed</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// This is the commented version.</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;">// since both Ball and Main are MovieClips,</span>
              <span style="color: #808080; font-style: italic;">// we have to import the MovieClip Class </span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>; <span style="color: #808080; font-style: italic;">// Later on, we use the Event Class, so</span>
             <span style="color: #808080; font-style: italic;">// we need to import it here.</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;">// See how the Class definition and import statements</span>
        <span style="color: #808080; font-style: italic;">// are inside the package curly brackets?</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ball:<span style="color: #0066CC;">MovieClip</span>; <span style="color: #808080; font-style: italic;">// We declare &quot;ball&quot; up here</span>
             <span style="color: #808080; font-style: italic;">// so that the whole Class can refer to it</span>
&nbsp;
        <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;">// This is the constructor function!</span>
&nbsp;
             ball = <span style="color: #000000; font-weight: bold;">new</span> Ball<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Since we already declared &quot;ball&quot;</span>
               <span style="color: #808080; font-style: italic;">// as an instance variable, we can just refer to it</span>
               <span style="color: #808080; font-style: italic;">// directly without redefining it</span>
             addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Where does ball get added?  Main</span>
               <span style="color: #808080; font-style: italic;">// is representing our Flash movie's main timeline,</span>
               <span style="color: #808080; font-style: italic;">// so that's where the ball appears</span>
             ball.<span style="color: #006600;">speed</span> = <span style="color: #cc66cc;">5</span>;
&nbsp;
             addEventListener<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, moveBall<span style="color: #66cc66;">&#41;</span>;
             <span style="color: #808080; font-style: italic;">// note: no need for the word &quot;this&quot;, in case you</span>
             <span style="color: #808080; font-style: italic;">// were wondering</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moveBall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
        <span style="color: #66cc66;">&#123;</span>
                ball.<span style="color: #006600;">x</span> += ball.<span style="color: #006600;">speed</span>;
                ball.<span style="color: #006600;">y</span> += ball.<span style="color: #006600;">speed</span>;
                <span style="color: #808080; font-style: italic;">// Again, because &quot;ball&quot; was defined up top, </span>
                <span style="color: #808080; font-style: italic;">// the moveBall method can refer to it</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h2>Are You Smart Yet?</h2>
<p>Hopefully, you know enough now to port your whole wad of first-frame code to a separate .as Class file.  That&#8217;s a baby step towards actually using the power of Classes to save yourself time in the future, and to keep your code organized, updatable, and a pleasure to re-visit.</p>
<p>In the next part of this tutorial, we&#8217;ll look at ways you can pick through your Wall of Code and identify which pieces you can could out into their own Class files.</p>
<p>For more Flash AS3 Tutorials and a pile of other useful stuff, check out our <a href="http://www.untoldentertainment.com/blog/flash-and-actionscript-911/">Flash and Actionscript 911</a> feature.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F25%2Ftutorial-understanding-classes-in-as3-part-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2009%2F08%2F25%2Ftutorial-understanding-classes-in-as3-part-1%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=1527&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/feed/</wfw:commentRss>
		<slash:comments>69</slash:comments>
		</item>
		<item>
		<title>Flash CS4 3D &#8211; Depth Management</title>
		<link>http://www.untoldentertainment.com/blog/2008/11/26/flash-cs4-3d-depth-management/</link>
		<comments>http://www.untoldentertainment.com/blog/2008/11/26/flash-cs4-3d-depth-management/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 17:00:57 +0000</pubDate>
		<dc:creator>Ryan Henson Creighton</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[CS4]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.untoldentertainment.com/blog/?p=573</guid>
		<description><![CDATA[i&#8217;ve been playing around with the 3D tools in Flash 10/CS4, with half a mind to completely redo our cube-mapped memory game Two By Two with an actual 3D cube. (Currently, the game uses a series of skewed planes to fake the 3D effect.) The good news is that CS4 does support 3D, although some [...]]]></description>
			<content:encoded><![CDATA[<p>i&#8217;ve been playing around with the 3D tools in Flash 10/CS4, with half a mind to completely redo our cube-mapped memory game <a href="http://www.untoldentertainment.com/blog/2007/04/26/two-by-two/">Two By Two</a> with an actual 3D cube.  (Currently, the game uses a series of skewed planes to fake the 3D effect.)</p>
<p>The good news is that CS4 does support 3D, although some people on the web are calling it 2.5D for one particular reason:  the DisplayObjects that you transform and translate are unaware of each other, and do not exist in the same 3D space.</p>
<p>Here&#8217;s what that means: you can have two planes sitting next to each other.  Depth-wise, PlaneA is in front of PlaneB.  You move PlaneA backwards along the z axis, and it <em>never swaps depths behind PlaneB</em>.  It&#8217;s as if the two clips are entirely autonomous and exist in their own little 3D worlds.  Which they do.</p>
<p>note: you&#8217;ll need to be running the Flash Player 10 plugin to view these examples.  Get it <a href="http://get.adobe.com/flashplayer/">HERE</a>.</p>
<p> <br />

<object width="550" height="400">
<param name="movie" value="http://www.untoldentertainment.com/blog/labs/3d/depthProblem/depthProblem.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></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="550" height="400" src="http://www.untoldentertainment.com/blog/labs/3d/depthProblem/depthProblem.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
 </p>
<p>This becomes a big problem when you need things to look natural.  Check out this Flash coder&#8217;s experiment with a 3D world and a camera:</p>
<div class="displayed">
<p><a href="http://www.btinternet.com/~m.j_hall/check_cs4_3d.html"><img src="http://www.untoldentertainment.com/blog/img/2008_11_26/depthProblem.jpg" alt="Flash 3D World with Messed Up Depth" /></a></p>
<p>This is fine if all of your game characters have X-Ray vision, i suppose.</p></div>
<p>The floors and the walls are placed correctly, but they all show through each other. i had the exact same problem when i tried building a cube from MovieClips.  Placing the clips was very easy, but the depth problem baffled me.  i knew that i had to loop through all the cube faces and re-order them based on their z properties, but i didn&#8217;t know how to do that, since localToGlobal only supports x/y coordinates.</p>
<p>It turns out you have to actually write your own depth manager if you want to build any kind of 3D object and have it display correctly.  i found just the code i needed here on Drew Cummins&#8217;s blog:</p>
<p><a href="http://blog.generalrelativity.org/?p=28">http://blog.generalrelativity.org/?p=28</a></p>
<p>i&#8217;m a big enough man to admit that at one o&#8217;clock in the morning last night, i didn&#8217;t fully grasp how this code did what it did.  Happily, it worked.  You can see the difference in cube rendering below.  The cube on the left applies Drew&#8217;s depth management routine.</p>
<p>Thanks, Drew!</p>
<p> <br />

<object width="550" height="400">
<param name="movie" value="http://www.untoldentertainment.com/blog/labs/3d/cube_depthManagement/cube_comparison.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="transparent"></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="550" height="400" src="http://www.untoldentertainment.com/blog/labs/3d/cube_depthManagement/cube_comparison.swf" quality="high" bgcolor="#FFFFFF" wmode="transparent" menu="false" ></embed>
</object>
<br />
 </p>
<p>Additional notes: there is a (possibly deliberate?) type-o in Drew&#8217;s code.  One of the loops is missing a minus operator on its iteration decrementor (i-), so watch out for that.</p>
<p>You may also have to rely on the ADDED_TO_STAGE event, as Drew&#8217;s code threw me an error when doc.stage was <strong>null</strong>.  This can happen when you try to fire code before an object actually exists on the stage.
<div class="tweetmeme_button" style="margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F11%2F26%2Fflash-cs4-3d-depth-management%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.untoldentertainment.com%2Fblog%2F2008%2F11%2F26%2Fflash-cs4-3d-depth-management%2F&amp;source=untoldent&amp;style=normal&amp;service_api=R_44463fc40e5eda8ec585b4088e695066&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p class="fbconnect_share"><fb:share-button class="url" href="http://www.untoldentertainment.com/blog/2008/11/26/flash-cs4-3d-depth-management/" /></p><img src="http://www.untoldentertainment.com/blog/?ak_action=api_record_view&id=573&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.untoldentertainment.com/blog/2008/11/26/flash-cs4-3d-depth-management/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

