<?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>AS3Blog.org</title>
	<atom:link href="http://www.as3blog.org/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.as3blog.org</link>
	<description></description>
	<lastBuildDate>Thu, 15 Jul 2010 04:55:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Advanced Function Work</title>
		<link>http://www.as3blog.org/?p=119</link>
		<comments>http://www.as3blog.org/?p=119#comments</comments>
		<pubDate>Thu, 15 Jul 2010 04:54:54 +0000</pubDate>
		<dc:creator>Black Bullet</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=119</guid>
		<description><![CDATA[In this post I&#8217;m going to show you a few more advanced features of AS3 functions. I think you&#8217;ll find they certainly help, at least when it comes to coding an API for the general public. &#8230;rest Parameter The rest parameter is cool little parameter type in AS3. It allows for varying sizes of parameter [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;m going to show you a few more advanced features of AS3 functions. I think you&#8217;ll find they certainly help, at least when it comes to coding an API for the general public.</p>
<h4>&#8230;rest Parameter</h4>
<p>The rest parameter is cool little parameter type in AS3. It allows for varying sizes of parameter lists to be passed into functions. Let&#8217;s look at a quick example:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function foo(bar:String, foobar:Object, ...rest):void</div></td></tr></tbody></table></div>
<p>In this example, we declare function called foo, which has two defined parameters called bar and foobar, and then as the last parameter we have this thing called &#8230;rest. As you can tell, &#8230;rest is the rest parameter. The rest parameter is declared by three ellipses or dots (&#8230;) and then followed by the parameter&#8217;s name. In this case I&#8217;ve named it rest, but you could name whatever you like. For example:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function foo(bar:String, foobar:Object, ...someBigListOfWhoKnowsWhat):void</div></td></tr></tbody></table></div>
<p>What does this look like for the person who&#8217;s calling the function? Let&#8217;s take a look:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">foo(&quot;bar&quot;, {foobar: null}, &quot;some&quot;, &quot;Big&quot;, &quot;List&quot;, &quot;Of&quot;, &quot;Strings&quot;):void</div></td></tr></tbody></table></div>
<p>As you can see, when we call this function we pass in the two defined parameters, and then comes a big long list of parameters that we didn&#8217;t define; this is how the rest parameter works, the user can put in as many parameters as they like. It&#8217;s important to note, that the user can also pass in parameters of any type, so you&#8217;ll have to do some checking if you want the parameters to be of a certain type.</p>
<p>Now we&#8217;ll get onto how you can actually access these parameters passed in. When the function actually starts executing it&#8217;s statements, the rest parameter has been converted into an array. So we can access the parameters like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function foo(bar:String, foobar:Object, ...otherParams):void {<br />
trace(otherParams.length);<br />
trace(otherParams[0]);<br />
trace(otherParams.shift());<br />
otherParams.unshift(&quot;tom&quot;);<br />
}</div></td></tr></tbody></table></div>
<p>As you can see, we can perform various array methods on it, access the length property, and access the elements in the array. We could also do a for each or for loop on the array, and so on. But what if we need to pass these parameters on to another function, expecting a rest parameter (not an array) itself? That&#8217;s what we&#8217;ll discuss next.</p>
<h4><strong>Function.apply()</strong></h4>
<p>When you define a function, you are creating a Function object. The object itself has a number of handy methods. One of those methods we are going to talk about today, and that it the apply() method.</p>
<p>The apply method allows you to call a function, but pass in the parameters needed as an array. Let&#8217;s write some setup code:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function foo(bar:String, ...otherParams):void {<br />
// bar is a string we need to do some processing on<br />
var bar2:String = bar.replace(/\$\d/, someArray[0]);<br />
}</div></td></tr></tbody></table></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></td></tr></tbody></table></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function bar(foo:String, ...otherParams):void {<br />
// some code to do something with these parameters<br />
}</div></td></tr></tbody></table></div>
<p>Now what we need to do is when someone calls foo, we want to do some processing on the bar string as seen in foo(), and then pass on the processed string <strong>and</strong> all the parameters the &#8230;otherParams rest parameter to bar() and return whatever bar() returns. How do we that when, inside the actual function, otherParams is an array (meaning we can&#8217;t just go</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">return bar(bar2, otherParams);</div></td></tr></tbody></table></div>
<p>). This is where apply() comes in:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function foo(bar:String, ...otherParams):void {<br />
// bar is a string we need to do some processing on<br />
var bar2:String = bar.replace(/\$\d/, someArray[0]);<br />
var applyParams:Array = otherParams;<br />
applyParams.unshift(bar2);<br />
return bar.apply(this, applyParams);<br />
}</div></td></tr></tbody></table></div>
<p>So what we&#8217;re doing that&#8217;s different is, creating an array called applyParams; this is the array of parameters to pass to apply(). Then we are adding the processed string (bar2) to the front of the applyParams array. Finally, we are calling the apply method of the bar object (which is a Function), passing in this (this object) and the applyParams array and returning the returned value. The apply method will use the array as the list of parameters for the function and everything will work smoothly. But why do need to pass in the &#8220;this&#8221; reference? The first parameter of apply, (so far as I understand it) is what object to point to wherever &#8220;this&#8221; is referred to in the function. Therefore, be careful with that one and take into account where the function is located.</p>
<p>Anyway, that wraps up this post, hope you learnt a thing or two about functions. I could say more, but I&#8217;ll leave it at that. Please feel free to post your thoughts below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=119</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problems with Tween and Using TweenLite</title>
		<link>http://www.as3blog.org/?p=113</link>
		<comments>http://www.as3blog.org/?p=113#comments</comments>
		<pubDate>Tue, 06 Jul 2010 06:39:53 +0000</pubDate>
		<dc:creator>Black Bullet</dc:creator>
				<category><![CDATA[Nuts & Bolts]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Tweening]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=113</guid>
		<description><![CDATA[Have you had trouble with Adobe&#8217;s Tween class? (Used for so called &#8220;easy&#8221; ActionScript animation of display objects) I sure have, so I thought I&#8217;d write a post about it. When I was first taught how to animate with ActionScript, I had many a lock up and stutter from the Tween class. For awhile I [...]]]></description>
			<content:encoded><![CDATA[<p>Have you had trouble with Adobe&#8217;s Tween class? (Used for so called &#8220;easy&#8221; ActionScript animation of display objects) I sure have, so I thought I&#8217;d write a post about it. When I was first taught how to animate with ActionScript, I had many a lock up and stutter from the Tween class. For awhile I was quite annoyed, seeing the benefit of animation using ActionScript but thinking that it was totally unreliable. Now there are a number of methods to fix the problems with Tween, using Dictionaries, new objects and so on; all of which seemed tedious and time consuming. Thankfully, I found a better solution, third party tweening engines! I&#8217;m going to tell you about what I think is the best one, TweenLite.</p>
<p>TweenLite (and the Greensock Tweening Platform) is simple, powerful, fast and reliable. Greensock has many awesome classes, generously available for free, all of which are (so far as I understand) maintained by one person. You can get them at <a href="http://www.greensock.com/">http://www.greensock.com/</a></p>
<p>I&#8217;ll just go through a few features with you now. First of all the TweenLite object has two main methods, <strong>to</strong>() and <strong>from</strong>(). With these you can change and object&#8217;s properties, from their current value <strong>to </strong>a new value at a certain rate. <strong>from</strong>() works similarly; it sets the properties of an object to what you specify, and then moves those properties back to what they were. So you can tween object&#8217;s in both directions.</p>
<p>Let&#8217;s experiment with to():</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import com.greensock.TweenLite;<br />
TweenLite.to(object, 1.25, { x: 20, y: 45, alpha: .8, customProperty: 20.36 });</div></td></tr></tbody></table></div>
<p>The first parameter is the object you want to perform a tween on. The second is the amount of time you want the tween to take. And the third is an object literal of properties you want to change. In this case these properties will be moved to the values specified. TweenLite can modify any numerical property of an object, making some really powerful stuff possible.</p>
<p>Next I&#8217;ll talk about a few of the special properties, they are: onStart, onComplete, ease, and delay. Here&#8217;s a definition of them:</p>
<ul>
<li>onStart &#8211; A function that should be called when the tween begins (when its currentTime is at 0 and changes to some other value which can happen more than once if the tween is restarted multiple times).</li>
<li>onComplete &#8211; A function that should be called when the tween has finished</li>
<li>ease &#8211; Use any standard easing equation to control the rate of change. For example, Elastic.easeOut. The Default is Quad.easeOut.</li>
<li>delay &#8211; Amount of delay in seconds (or frames for frames-based tweens) before the tween should begin.</li>
</ul>
<p>Let&#8217;s try and use the ease property first:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import com.greensock.TweenLite;<br />
import com.greensock.easing.*;<br />
<br />
TweenLite.to(object, 1.25, { x: 20, y: 45, alpha: .8, customProperty: 20.36, ease: Sine.easeOut });</div></td></tr></tbody></table></div>
<p>Note the extra import, we must import the easing classes to be used with the tween. You can use all the standard easing equations, and a few extras to my knowledge.</p>
<p>Now for the rest of those properties:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import com.greensock.TweenLite;<br />
<br />
TweenLite.to(object, 1.25, { x: 20, y: 45, alpha: .8, customProperty: 20.36, <br />
&nbsp; &nbsp;ease: Sine.easeOut,<br />
&nbsp; &nbsp;onStart: startFunction,<br />
&nbsp; &nbsp;onComplete: completeFunction,<br />
&nbsp; &nbsp;delay: 3<br />
});</div></td></tr></tbody></table></div>
<p>So this tween will trigger startFunction() when it starts, completeFunction() when it ends, and will wait 3 seconds before starting.</p>
<p>That&#8217;s just the tip of the iceberg, but the engine has great documentation, so go to <a href="http://www.greensock.com/">http://www.greensock.com/</a> and check it out. The purpose of this post was not to rewrite the documentation, but to get your feet wet and show you a better way to tweening.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=113</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the AMFPHPConnection class</title>
		<link>http://www.as3blog.org/?p=111</link>
		<comments>http://www.as3blog.org/?p=111#comments</comments>
		<pubDate>Mon, 05 Jul 2010 06:08:04 +0000</pubDate>
		<dc:creator>Black Bullet</dc:creator>
				<category><![CDATA[Flash & Server Scripts]]></category>
		<category><![CDATA[AMFPHP]]></category>
		<category><![CDATA[AS3 to PHP]]></category>
		<category><![CDATA[Remoting]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=111</guid>
		<description><![CDATA[In this post I&#8217;ll be talking about a class I&#8217;ve made called AMFPHPConnection that extends Flash&#8217;s NetConnection object. This class eases and enhances the process of using AMFPHP for Flash remoting. But before we begin, just a few notes: I&#8217;m not the usual author, and you&#8217;ll need to know how to use AMFPHP the normal [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll be talking about a class I&#8217;ve made called AMFPHPConnection that extends Flash&#8217;s NetConnection object. This class eases and enhances the process of using AMFPHP for Flash remoting. But before we begin, just a few notes: I&#8217;m not the usual author, and you&#8217;ll need to know how to use AMFPHP the normal way, or you may not get some of the terminology. If you want to learn how to use it, I have a series on my YouTube channel for that purpose, go here to watch it: <a rel="nofollow" href="http://www.youtube.com/user/BlackBulletIV#grid/user/CAD2DAE46A04939D">http://www.youtube.com/user/BlackBulletIV#grid/user/CAD2DAE46A04939D</a></p>
<p>First go and download the class from here: <a title="http://www.nova-fusion.com/​cms/​FileFolder/​AMFPHPConnection.zip" href="http://www.nova-fusion.com/%E2%80%8Bcms/%E2%80%8BFileFolder/%E2%80%8BAMFPHPConnection.zip" target="_blank">http://www.nova-fusion.com/cms/FileFolder/AMFPHPConnection.zip</a></p>
<p>This zip file contains the minimized class in it&#8217;s package folders: com/novafusion/net/AMFPHPConnection.as. It also contains readable version of the class, which has documentation, comments and more whitespace; it is: AMFPHPConnectionReadable.as. If you actually want to use this class instead, rename it to AMFPHPConnection.as and place it in the proper package folders. The zip file also contains package details similar to what I&#8217;m talking about now, and instructions similar to what I&#8217;m going to be talking about below. Now let&#8217;s get onto how to use it.</p>
<p><strong>Setup</strong></p>
<p>To get started, import the class:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import com.novafusion.net.AMFPHPConnection;</div></td></tr></tbody></table></div>
<p>Next create a new instance, you can call the variable name whatever you like but I&#8217;ll go with connection:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var connection:AMFPHPConnection = new AMFPHPConnection(&quot;http://localhost/amfphp/gateway.php&quot;);</div></td></tr></tbody></table></div>
<p>When you create a new instance, you are expected to pass in one parameter, the path to your AMFPHP gateway.php file. When you create the instance the class will automatically set up a connection and a responder for your function calls. If you would like to change the gateway location at any point do this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.gateway = &quot;http://www.some-other-server.com/amfphp/gateway.php&quot;;</div></td></tr></tbody></table></div>
<p>This will set the gateway location in the class and reconnect to that server. Access the path to the current gateway this way:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.gateway</div></td></tr></tbody></table></div>
<p><strong>Quick Service Referencing</strong></p>
<p>Quick service referencing helps a lot in speeding things up and reducing the amount of code you have to right. If you have a service you use a lot in your code, the connection class can store it for you and cut down how much you have to type to reference it. Examples of referencing strings are:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;FlashPHPTest&quot;; // just a class name, but this could be a folder name<br />
&quot;UsingAMFPHP.TalkBack&quot;; // folder and a class inside it<br />
&quot;UsingAMFPHP.TalkBack.put_together&quot;; // folder, a class inside it, and a method inside the class<br />
&quot;FlashPHP&quot; // part of a class name (could be folder or method name)</div></td></tr></tbody></table></div>
<p>You can basically store any string in them, and reference it quickly when calling a service. You could use each of these reference strings in calls like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;$1.spitBack&quot;; // just a class name, but this could be a folder name<br />
&quot;$2.put_together&quot;; // folder and a class inside it<br />
&quot;$3&quot;; // folder, a class inside it, and a method inside the class<br />
&quot;$4Test.spitBack&quot; // part of a class name (could be folder or method name)</div></td></tr></tbody></table></div>
<p>You use your references with a dollar sign and then it&#8217;s number (which is assigned to it in the order you add them to the class). As you can see this will greatly reduce what you have to write in your code.</p>
<p>You can add service references in two ways. The first is to add them in when constructing the class:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var connection:AMFPHPConnection = new AMFPHPConnection(&quot;http://localhost/amfphp/gateway.php&quot;, &quot;FlashPHPTest&quot;, &quot;UsingAMFPHP.TalkBack&quot;);</div></td></tr></tbody></table></div>
<p>The second way is to add them in with a method provided:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.addServices(&quot;FlashPHPTest&quot;, &quot;UsingAMFPHP.TalkBack&quot;);</div></td></tr></tbody></table></div>
<p>You can add as many as you like at one time, and use the method as many times as you like. You can also remove service references like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.removeServices(&quot;FlashPHPTest&quot;, &quot;UsingAMFPHP.TalkBack&quot;);</div></td></tr></tbody></table></div>
<p>It&#8217;s not recommended that you remove service references, as it might generate confusion as to the order in which to reference them afterwards (just more for you to keep track of).</p>
<p><strong>Calling Services</strong></p>
<p>To call a service, use this method and pass in your service and parameters (not the responder! That&#8217;s taken care of for you):</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.talk(&quot;SomeService.someMethod&quot;, &quot;Some parameter&quot;, &quot;Another parameter&quot;);</div></td></tr></tbody></table></div>
<p>The function is called talk because of confliction with the NetConnection object&#8217;s call method. However you can still use that method, like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.call(&quot;SomeService.someMethod&quot;, null, &quot;Some parameter&quot;, &quot;Another parameter&quot;);</div></td></tr></tbody></table></div>
<p>You can pass in the responder either as null (the object&#8217;s responder object will be used instead, which is what you want), use connection.responder (replace connection with your instance name), or pass in your own custom responder (note that result and fault listeners won&#8217;t work).</p>
<p>You can use quick service referencing in your calls like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.talk(&quot;$1.spitBack&quot;, &quot;Hello world!&quot;);</div></td></tr></tbody></table></div>
<p>Just use strings in the style shown in the quick service referencing section and all will be well.</p>
<p><strong>Result and Fault Listeners/Handlers</strong></p>
<p>We need to see up some listener/handler functions to respond to results and faults from AMFPHP:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">function onResult(event:Event):void {<br />
// do whatever<br />
trace(connection.result);<br />
}<br />
function onFault(event:Event):void {<br />
// do whatever<br />
trace(connection.fault);<br />
}</div></td></tr></tbody></table></div>
<p>Notice that instead of receiving objects they actually receive plain event objects; that&#8217;s because they&#8217;re triggered by custom event listeners. You access the last result object with connection.result (replace connection with your instance name) and the last fault with connection.fault (replace connection with your instance name). Now how do we add the listeners for these functions? Here&#8217;s how:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.setListeners(onResult, onFault);</div></td></tr></tbody></table></div>
<p>Just pass in the result function and the fault function and they&#8217;ll be triggered for their respective events. Now you don&#8217;t have to set either one, if you want to set the fault handler but leave the result handler as it is just leave the onResult function as null. Now be careful about these handlers, when you set them like this they become the default handlers for all calls. One way to get around this is to remove the listeners in the on result or on fault functions, using this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.removeListeners();</div></td></tr></tbody></table></div>
<p>You could do that, but there&#8217;s a better way to do it. Set the third parameter of setListeners() to true; this will make these listeners only last for one call, and on the next result or fault they will be removed. Here&#8217;s how:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.setListeners(onResult, onFault, true);</div></td></tr></tbody></table></div>
<p>You could also add listeners the harder way like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">connection.addEventListener(AMFPHPConnection.RESULT, onResult); // For results<br />
connection.addEventListener(AMFPHPConnection.FAULT, onFault); // For faults</div></td></tr></tbody></table></div>
<p>And of course to remove them you just use them same code, just change the function to removeEventListener().</p>
<p><strong>Conclusion</strong></p>
<p>Hope that helps! Here&#8217;s a sample application to give you some perspective on how this class lessens your code when using AMFPHP:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import com.novafusion.net.AMFPHPConnection;<br />
<br />
var connection:AMFPHPConnection = new AMFPHPConnection(&quot;http://localhost/amfphp/gateway.php&quot;, &quot;UsingAMFPHP.TalkBack&quot;, &quot;FlashPHPTest&quot;);<br />
<br />
function result(event:Event):void {<br />
trace(connection.result);<br />
}<br />
<br />
function fault(event:Event):void {<br />
trace(connection.fault);<br />
}<br />
<br />
connection.setListeners(result, fault);<br />
connection.talk(&quot;$1.put_together&quot;, &quot;tom&quot;, &quot;bob&quot;);<br />
connection.talk(&quot;$2.spitBack&quot;, &quot;tom&quot;);</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=111</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Pad a Number with Zeros + Simple Counter</title>
		<link>http://www.as3blog.org/?p=104</link>
		<comments>http://www.as3blog.org/?p=104#comments</comments>
		<pubDate>Tue, 25 May 2010 12:48:01 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Nuts & Bolts]]></category>
		<category><![CDATA[Counter]]></category>
		<category><![CDATA[PadZero]]></category>
		<category><![CDATA[Timer]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=104</guid>
		<description><![CDATA[At some point you may need to display a number that must always occupy a predetermined number of character spaces. An example would be a rolling game timer with a 5 digit display. For example, when the timer hits 33 seconds, the digits would display like so: 0 0 0 3 3 It is very [...]]]></description>
			<content:encoded><![CDATA[<p>At some point you may need to display a number that must always occupy a predetermined number of character spaces. An example would be a rolling game timer with a 5 digit display. For example, when the timer hits 33 seconds, the digits would display like so:  0 0 0 3 3</p>
<p>It is very simple to pad your number to whatever length of characters you would like.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">frigidfish</span>.<span style="color: #006600;">utils</span><span style="color: #66cc66;">&#123;</span><br />
<br />
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PadZero <span style="color: #66cc66;">&#123;</span><br />
<br />
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> convert<span style="color: #66cc66;">&#40;</span>inputNumber:<span style="color: #0066CC;">Number</span>,numberOfDigits:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> paddedString:<span style="color: #0066CC;">String</span>=inputNumber.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>paddedString.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&amp;</span>lt; numberOfDigits<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
paddedString = <span style="color: #ff0000;">&quot;0&quot;</span> + paddedString;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #b1b100;">return</span> paddedString;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Save this class to a folder relative to your flash document /com/frigidfish/utils/PadZero.as</p>
<p>Here is an example usage:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">frigidfish</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">PadZero</span>;<br />
<br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>PadZero.<span style="color: #006600;">convert</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #808080; font-style: italic;">// traces 00033</span></div></td></tr></tbody></table></div>
<p>I use this little converter all the time with customized timers and counters.</p>
<p>If you would like a simple example of a timer, you could do something like this:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:460px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// Simple Counter</span><br />
<span style="color: #808080; font-style: italic;">// Add 4 text boxes to the stage.</span><br />
<span style="color: #808080; font-style: italic;">// Name their instances from left to right:</span><br />
<span style="color: #808080; font-style: italic;">// digit0, digit1, digit2, digit3</span><br />
<br />
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">frigidfish</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">PadZero</span><br />
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Timer</span>;<br />
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TimerEvent</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> counter:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> timer:Timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>;<br />
timer.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">TIMER</span>, addOne<span style="color: #66cc66;">&#41;</span>;<br />
timer.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> addOne<span style="color: #66cc66;">&#40;</span>event:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
<br />
counter ++;<br />
<br />
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>counter <span style="color: #66cc66;">&amp;</span>gt; <span style="color: #cc66cc;">9999</span><span style="color: #66cc66;">&#41;</span> counter = <span style="color: #cc66cc;">0</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> t:<span style="color: #0066CC;">String</span> = PadZero.<span style="color: #006600;">convert</span><span style="color: #66cc66;">&#40;</span>counter,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
digit0.<span style="color: #0066CC;">text</span> = t.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
digit1.<span style="color: #0066CC;">text</span> = t.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
digit2.<span style="color: #0066CC;">text</span> = t.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;<br />
digit3.<span style="color: #0066CC;">text</span> = t.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>You could make custom digits with your own artwork to spice up your timer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=104</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Freeing up memory for Garbage Collection</title>
		<link>http://www.as3blog.org/?p=100</link>
		<comments>http://www.as3blog.org/?p=100#comments</comments>
		<pubDate>Sun, 02 May 2010 10:44:54 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Flash & Server Scripts]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=100</guid>
		<description><![CDATA[Something that I&#8217;ve been noticing lately is the need to pay close attention to how you are cleaning up your objects after you are done with them. I&#8217;ve been working with a great as3 debugging tool called &#8220;De MonsterDebugger&#8221;. Strange name but great tool. Check it out here: http://www.demonsterdebugger.com/ Basically you just download the debugger [...]]]></description>
			<content:encoded><![CDATA[<p>Something that I&#8217;ve been noticing lately is the need to pay close attention to how you are cleaning up your objects after you are done with them.</p>
<p>I&#8217;ve been working with a great as3 debugging tool called &#8220;De MonsterDebugger&#8221;. Strange name but great tool. Check it out here: <a href="http://www.demonsterdebugger.com/">http://www.demonsterdebugger.com/</a></p>
<p>Basically you just download the debugger and import the class:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> nl.<span style="color: #006600;">demonsters</span>.<span style="color: #006600;">debugger</span>.<span style="color: #006600;">MonsterDebugger</span>;</div></td></tr></tbody></table></div>
<p>then as the first line in your constructor function:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">new</span> MonsterDebugger<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Now you just open the air application that came with the download and it will track everything that happens in your class in the interface. You can run functions from it as well. It rocks!</p>
<p>Anyway, my point being that as I was running it on some of my programs I noticed a significant amount of memory leakage on some of the classes. Here are a few tips to help flash along with garbage collecting your old used up objects.</p>
<p>One thing I like to do is to create a destroy() function at the end of my classfiles. The job of this function is to reset all variables possible in the script. I&#8217;ll explain more in a moment.</p>
<p>The &#8216;delete&#8217; keyword:</p>
<p>This is a somewhat misunderstood keyword because it only works on dynamically created variables. let me explain. take the following code for example:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> fluffy:Cat = <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;happy&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> sally:Cat = <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;angry&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// now somewhere along the way we want to remove fluffy</span><br />
<span style="color: #0066CC;">delete</span> fluffy;<br />
<span style="color: #808080; font-style: italic;">// oops! This generates an error. This is because the delete </span><br />
<span style="color: #808080; font-style: italic;">// keyword only works on dynamic properties.</span><br />
fluffy = <span style="color: #000000; font-weight: bold;">null</span>;<br />
<span style="color: #808080; font-style: italic;">// Ok, so this sets fluffy to null but references to fluffy elsewhere</span><br />
<span style="color: #808080; font-style: italic;">// in the code may still cause Garbage Collection to not be able to </span><br />
<span style="color: #808080; font-style: italic;">// clean it from memory.</span></div></td></tr></tbody></table></div>
<p>So I have discovered that the only way to be sure that your variables will be garbage collected and the memory will be released is to make the variables in your classes dynamic by using arrays or objects to store them. </p>
<p>You could do this like so:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> cat:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
cat<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;fluffy&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;angry&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
cat<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;sally&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> Cat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;happy&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
addChild<span style="color: #66cc66;">&#40;</span>cat.<span style="color: #006600;">sally</span><span style="color: #66cc66;">&#41;</span>;<br />
addChild<span style="color: #66cc66;">&#40;</span>cat.<span style="color: #006600;">fluffy</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// ok, now later we can clean up with a destroy function</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> cat<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeChild<span style="color: #66cc66;">&#40;</span>cat<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">delete</span> cat<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The destroy function iterates through all objects inside of the cat object and removes them from the display list.<br />
Next, it deletes the property itself freeing it up for garbage collection.</p>
<p>Keep in mind that the Cat class should also have a destroy function that cleans itself out as well!<br />
So the function would be modified to look like this:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> cat<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeChild<span style="color: #66cc66;">&#40;</span>cat<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cat<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">delete</span> cat<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>You can use the De MonsterDebugger to magically watch your memory performance increase dramatically by using this technique. Hope it helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=100</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hidden Games in as3blog.org</title>
		<link>http://www.as3blog.org/?p=93</link>
		<comments>http://www.as3blog.org/?p=93#comments</comments>
		<pubDate>Fri, 02 Apr 2010 22:25:44 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[game grid]]></category>
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=93</guid>
		<description><![CDATA[I just realized that there are some hidden games in my banner. This may seem like a strange statement since I made the banner and all the artwork. Well, originally the banner was a concept design for a company that we do projects for called &#8220;ThinkFun&#8221;. I stripped down the banner but apparently left some [...]]]></description>
			<content:encoded><![CDATA[<p>I just realized that there are some hidden games in my banner. This may seem like a strange statement since I made the banner and all the artwork. Well, originally the banner was a concept design for a company that we do projects for called &#8220;ThinkFun&#8221;. I stripped down the banner but apparently left some &#8216;hidden&#8217; games.. See if you can find the games hidden in the header banner of this site! I believe there are anywhere from 3 to 6 or so. </p>
<p>By the way, for anyone that has read my Game Grid article, these games all use the concept, so you can see it in action.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=93</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to use Classes in your Project</title>
		<link>http://www.as3blog.org/?p=87</link>
		<comments>http://www.as3blog.org/?p=87#comments</comments>
		<pubDate>Fri, 02 Apr 2010 01:32:02 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Nuts & Bolts]]></category>
		<category><![CDATA[fundamentals]]></category>
		<category><![CDATA[getter & setter]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=87</guid>
		<description><![CDATA[Many of the examples that I use in my blog come in the form of a &#8220;Class&#8221; file. Without going into too deep an explanation, I will attempt to explain how to implement a class into your project. The following is a class for our example: 12345678910111213141516171819202122232425262728293031package com.frigidfish&#123; &#160; &#160; &#160; &#160; import flash.display.Sprite; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Many of the examples that I use in my blog come in the form of a &#8220;Class&#8221; file. Without going into too deep an explanation, I will attempt to explain how to implement a class into your project. The following is a class for our example:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:460px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">frigidfish</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Monkey <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentState:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Monkey<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'New Monkey Instance Created'</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> jumping<span style="color: #66cc66;">&#40;</span>bool:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>bool<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Monkey is jumping around!'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentState = <span style="color: #ff0000;">&quot;jumping&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> jumping<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentState == <span style="color: #ff0000;">&quot;jumping&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The first line is the location of the package. Generally developers will save their classes under a hierarchy that is their website name backwards.. in my case, my game projects are under my business name frigidfish.com so all of my classes are stored under com.frigidfish. </p>
<p>To clarify, lets start by opening flash and selecting File|New, Flash File (Actionscript 3) and click OK. Save the new file to your hard drive somewhere in a folder called &#8220;Monkey&#8221;.</p>
<p>Now, navigate to the Monkey folder and create a subfolder called &#8220;com&#8221;. Navigate to the com folder and create another subfolder called &#8220;frigidfish&#8221;. </p>
<p>Go back to Flash and click File|New. Select &#8220;Actionscript File&#8221; and choose OK.<br />
You will get a blank page that you can enter code into. Paste the code from above into the blank page and save it in the newly created Monkey/com/frigidfish as Monkey.as</p>
<p>Now go back to the flash file we created first and open the &#8220;actions&#8221; panel by hitting the F9 key on a PC and on a mac I&#8217;m not sure, just click Window|Actions.</p>
<p>In the Actions panel you can start typing in code. The first thing you&#8217;ll want to do is import the class that we saved so that you can use it. Enter the following line:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">frigidfish</span>.<span style="color: #006600;">Monkey</span></div></td></tr></tbody></table></div>
<p>Now that the class is available, we can create a new variable instance of it. You can think of a class file as a &#8216;prototype&#8217;. In this case it is a prototype of a Monkey&#8230; The cool thing about a class file is that you can make as many instances of it as you&#8217;d like. </p>
<p>Ok, so now enter the following line:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> silly:Monkey = <span style="color: #000000; font-weight: bold;">new</span> Monkey<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>This line creates an instance of Monkey called silly. The Monkey class does not accept any arguments so we just leave the () empty. All classes have what is called a &#8216;constructor&#8217; function. This is a public function that bears the same name as the class. Anything you put in the constructor function will be executed immediately when the class is instantiated. If the constructor does nothing, it is perfectly legal to leave it out of the class completely, but it is considered bad practice to do so.</p>
<p> In our case, the Monkey class constructor only has one line of code which just traces &#8216;New Monkey Instance Created&#8217;. </p>
<p>Go ahead and test the movie. </p>
<p>If you look in the output panel you will see that not much happens except that you receive the message about the new instance being created. Go ahead and close the test window.</p>
<p>add this line of code next:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>silly.<span style="color: #006600;">jumping</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>If you test the movie now, you will see that in addition to the new instance message, a new line appears that says &#8216;false&#8217;. The default jumping state of the monkey is false.</p>
<p>go ahead and add the next couple of lines:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">silly.<span style="color: #006600;">jumping</span> = <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>silly.<span style="color: #006600;">jumping</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>If you test the movie now, you should see the following:</p>
<p>New Monkey Instance Created<br />
false<br />
Monkey is jumping around!<br />
true</p>
<p>Let&#8217;s go through this. </p>
<p>First we created a new instance of Monkey called silly.<br />
Next we asked flash to display the jumping state of silly, which returned false.<br />
Next we set silly&#8217;s jumping property to true, and he started jumping.<br />
Last we ask flash to display the current jumping state of silly, which returned true.</p>
<p>So you should be able to see how to get started using classes with Actionscript 3. I also used a couple of  getter and setter functions in the class. I will write an tutorial on this but for now a brief overview:</p>
<p>In Actionscript, you can set or get properties using functions. To the person setting the property, they are none the wiser that they are actually using a function. </p>
<p>In our example we simply set the monkey jumping with the statement: </p>
<p>silly.jumping = true;</p>
<p>in your class, if you create a function using the &#8216;set&#8217; keyword, the function name will be treated as if it were a property. Likewise, the &#8216;get&#8217; keyword can be used if a developer would like to retrieve the current value or state of a classes &#8216;property&#8217;. </p>
<p>It is important to note that the value passed to the setter function must be of the same data type that is returned from the getter function. Anyhow, I am getting off track but it&#8217;s all good information. </p>
<p>Below I have attached a link to the files associated with this tutorial.</p>
<p>Hope you find it useful! </p>
<p><a href='http://www.as3blog.org/wp-content/uploads/2010/04/monkey.zip'>monkey</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=87</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to dispatch a custom event</title>
		<link>http://www.as3blog.org/?p=75</link>
		<comments>http://www.as3blog.org/?p=75#comments</comments>
		<pubDate>Fri, 12 Mar 2010 01:46:28 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Nuts & Bolts]]></category>
		<category><![CDATA[Dispatching Events]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=75</guid>
		<description><![CDATA[This is going to be a very simple example of how to dispatch an event. In actionscript 3, you dispatch an event by extending the EventDispatcher class. I&#8217;m not going to get into a big complicated explanation right now, but I will show you a couple of simple examples. One good thing about the Sprite [...]]]></description>
			<content:encoded><![CDATA[<p>This is going to be a very simple example of how to dispatch an event. In actionscript 3, you dispatch an event by extending the EventDispatcher class. I&#8217;m not going to get into a big complicated explanation right now, but I will show you a couple of simple examples. </p>
<p>One good thing about the Sprite class is that it already extends the EventDispatcher class, so if you are working from your document class, or your class extends Sprite/Movieclip, you can use this anywhere in your class:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// Declare at the top...</span><br />
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const customEvent:<span style="color: #0066CC;">String</span>&nbsp; = <span style="color: #ff0000;">&quot;customEvent&quot;</span>;<br />
<span style="color: #808080; font-style: italic;">// ... Somewhere in your class...</span><br />
dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>customEvent<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Let&#8217;s say for example you have the following class:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cat <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const MEOW:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;meow&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Cat<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> meow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>MEOW<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now create an instance from your timeline:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> cat:Cat = <span style="color: #000000; font-weight: bold;">new</span> Cat</div></td></tr></tbody></table></div>
<p>next:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cat.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Cat.<span style="color: #006600;">MEOW</span>, doSomething<span style="color: #66cc66;">&#41;</span>;<br />
cat.<span style="color: #006600;">meow</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> doSomething<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The cat is hungry!&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Ok, so this really doesn&#8217;t do anything useful, but you can see how you could use this idea to broadcast events from your objects. This is by no means comprehensive, but I have noticed that this simple concept has eluded a lot of people. Later I will write a more robust tutorial showing how to extend the EventDispatcher class and do some cool stuff. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=75</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to create an AS3 game grid</title>
		<link>http://www.as3blog.org/?p=50</link>
		<comments>http://www.as3blog.org/?p=50#comments</comments>
		<pubDate>Tue, 02 Mar 2010 03:37:44 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Game Stuff]]></category>
		<category><![CDATA[iterators]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=50</guid>
		<description><![CDATA[One things that you have to do a lot when developing grid based games is obviously lay out the grid. This is confusing to a lot of people but is really quite simple. I created a simple class just for this blog to demonstrate how this can be achieved. Here is the class, and then [...]]]></description>
			<content:encoded><![CDATA[<p>One things that you have to do a lot when developing grid based games is obviously lay out the grid. This is confusing to a lot of people but is really quite simple. I created a simple class just for this blog to demonstrate how this can be achieved. </p>
<p>Here is the class, and then an explanation:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:460px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">frigidfish</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Grid <span style="color: #0066CC;">extends</span> Sprite<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> rows:uint;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> cols:uint;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> cell:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> plot:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Grid<span style="color: #66cc66;">&#40;</span>displayObject:<span style="color: #66cc66;">*</span>,rows,cols, spacer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">rows</span> &nbsp; = rows;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">cols</span> &nbsp; = cols;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; populateCells<span style="color: #66cc66;">&#40;</span>displayObject,spacer<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plotCells<span style="color: #66cc66;">&#40;</span>cell<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> populateCells<span style="color: #66cc66;">&#40;</span>displayObject:<span style="color: #66cc66;">*</span>,spacer:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> iterator:uint = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:uint = <span style="color: #cc66cc;">0</span>;c<span style="color: #66cc66;">&lt;</span>cols;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> r:uint = <span style="color: #cc66cc;">0</span>;r<span style="color: #66cc66;">&lt;</span>rows;r++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> displayObject;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">y</span> = cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">height</span> &nbsp;<span style="color: #66cc66;">*</span> r + <span style="color: #66cc66;">&#40;</span>spacer<span style="color: #66cc66;">*</span>r<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">x</span> = cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">*</span> c + <span style="color: #66cc66;">&#40;</span>spacer<span style="color: #66cc66;">*</span>c<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span style="color: #66cc66;">&#40;</span>cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterator++<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> plotCells<span style="color: #66cc66;">&#40;</span>cell:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> iterator:uint = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:uint = <span style="color: #cc66cc;">0</span>;c<span style="color: #66cc66;">&lt;</span>cols;c++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plot<span style="color: #66cc66;">&#91;</span>c<span style="color: #66cc66;">&#93;</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> r:uint = <span style="color: #cc66cc;">0</span>;r<span style="color: #66cc66;">&lt;</span>rows;r++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plot<span style="color: #66cc66;">&#91;</span>c<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>r<span style="color: #66cc66;">&#93;</span> = cell<span style="color: #66cc66;">&#91;</span>iterator<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iterator++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Example usage:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">import</span> com.<span style="color: #006600;">frigidfish</span>.<span style="color: #006600;">Grid</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> gameGrid:Grid = <span style="color: #000000; font-weight: bold;">new</span> Grid<span style="color: #66cc66;">&#40;</span>Token,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>;<br />
addChild<span style="color: #66cc66;">&#40;</span>gameGrid<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<br />
<span style="color: #808080; font-style: italic;">// There are 2 ways you can access the grid. </span><br />
<span style="color: #808080; font-style: italic;">// by cell number (count 20 cells col by col):</span><br />
gameGrid.<span style="color: #006600;">cell</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// or by plot info. if you count over 2 cols, and one</span><br />
<span style="color: #808080; font-style: italic;">// row down, you'll see that this line makes the grid</span><br />
<span style="color: #808080; font-style: italic;">// item invisible.</span><br />
gameGrid.<span style="color: #006600;">plot</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></td></tr></tbody></table></div>
<p>Ok, so this is a very simple class that can be built upon to do a lot of things. For example you could add a dynamic text field to your display object, and use the iterator in grid.as to add numbers to each cell. </p>
<p>You could use this as a starting point for a match type game or an array of other puzzle ideas. The point of this is just to help you understand how the grid is generated.</p>
<p>Let&#8217;s quickly go through the grid class:</p>
<p>The first thing we do is create 4 variables (lines 8-11).</p>
<p>rows and cols are passed to the constructor along with the class/display object that you want to instantiate and a spacer number. Note that in this class it is assumed that whatever you pass it as your display class must be displayable. </p>
<p>For this example, I just made a quick movieclip and called it Token, and selected &#8216;export for actionscript&#8217; in it&#8217;s properties. </p>
<p>Next is the constructor. As I said before, this requires 4 parameters:</p>
<p> your library item or class to display, rows (how many rows do you want) cols (how many columns do you want), space in pixels between display objects</p>
<p>the populateCells function does the meat of the work.</p>
<p>we create an iterator and start it at 0.<br />
next we loop through each column.<br />
for each column we add all of the rows:<br />
1. create a new instance of the supplied display object and add it to the cell array<br />
2. set the y position of the display object to it&#8217;s height x the number of the current row, + spacer<br />
3. set the x position of the display object to it&#8217;s width x the number of the current column + spacer<br />
4. Since this class extends Sprite, it is instantiated as a Sprite so we call just add the cell to it using the addChild() method<br />
5. add 1 to the iterator and continue the loop.</p>
<p>notice that a key to making the spacer work properly is multiplying it by the row or the column. If you were to just &#8216;add&#8217; spacer without multiplying by the row/col you would not achieve the desired result. </p>
<p>Next we execute the plotCells method. This creates an extra way to find the cell that you want. It sets up a 2 dimensional array and adds the cells to it so that you can access them by plotting coordinates.</p>
<p>first it creates an iterator and sets it to 0.<br />
next we look through the length of the cols variable<br />
now we create a new array and assign it to the plot array at the current column<br />
for each number in cols, we iterate through all rows.<br />
finally we assign each cell from our cell array to the proper plot coordinates.<br />
add 1 to the iterator and process the next column.</p>
<p>So basically:<br />
plot[0][0] = cell[0]<br />
plot[0][1] = cell[1]<br />
plot[0][1] = cell[2]<br />
&#8230;<br />
&#8230;<br />
and so on. if you made a 5&#215;5 grid, column 2, row 0 would be:<br />
plot[2][0] = cell[10]</p>
<p>well, that&#8217;s it!<br />
Hope you find this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=50</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Load AS2 SWF into your AS3 Project</title>
		<link>http://www.as3blog.org/?p=45</link>
		<comments>http://www.as3blog.org/?p=45#comments</comments>
		<pubDate>Tue, 16 Feb 2010 09:26:13 +0000</pubDate>
		<dc:creator>nuthman</dc:creator>
				<category><![CDATA[Flash & Server Scripts]]></category>
		<category><![CDATA[Dispatching Events]]></category>

		<guid isPermaLink="false">http://www.as3blog.org/?p=45</guid>
		<description><![CDATA[There is a game that I have been working on that I&#8217;ve had to load into an as3 interface and I noticed that my previous class will not work to do this. There were a few modifications that I had to make&#8230; Here is the class: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849// SWFLoader Class by Rick Nuthman // 2.16.10 // [...]]]></description>
			<content:encoded><![CDATA[<p>There is a game that I have been working on that I&#8217;ve had to load into an as3 interface and I noticed that my previous class will not work to do this.<br />
There were a few modifications that I had to make&#8230; Here is the class:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:460px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// SWFLoader Class by Rick Nuthman</span><br />
<span style="color: #808080; font-style: italic;">// 2.16.10</span><br />
<span style="color: #808080; font-style: italic;">//</span><br />
<span style="color: #808080; font-style: italic;">// Usage: </span><br />
<span style="color: #808080; font-style: italic;">/*<br />
var game:SWFLoaderAs2 = new SWFLoaderAs2(&quot;yourSwf.swf&quot;,20,0)<br />
game.dispatcher.addEventListener(&quot;ready&quot;, gameLoaded);<br />
function gameLoaded(event:Event){<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; addChild(game);<br />
}<br />
*/</span><br />
<br />
package com.<span style="color: #006600;">frigidfish</span><span style="color: #66cc66;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SWFLoaderAs2 <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> dispatcher:EventDispatcher &nbsp; = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> contentLoader:Loader; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SWFLoaderAs2<span style="color: #66cc66;">&#40;</span>pathToTheContent,x,y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> = x;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> = y;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadSWF<span style="color: #66cc66;">&#40;</span>pathToTheContent<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loadSWF<span style="color: #66cc66;">&#40;</span>pathToTheContent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest &nbsp;= <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>pathToTheContent<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentLoader &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentLoader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,swfLoaded<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> swfLoaded<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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span style="color: #66cc66;">&#40;</span>contentLoader<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatcher.<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><span style="color: #ff0000;">&quot;ready&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>usage:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> game:SWFLoaderAs2 = <span style="color: #000000; font-weight: bold;">new</span> SWFLoaderAs2<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yourSwf.swf&quot;</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />
game.<span style="color: #006600;">dispatcher</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ready&quot;</span>, gameLoaded<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">function</span> gameLoaded<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; addChild<span style="color: #66cc66;">&#40;</span>game<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>This will certainly work to load an as2 swf into your as3 project,<br />
but keep in mind that you will not be able to access methods and properties of your as2 swf.<br />
I will be updating this class or adding another to make this possible. In the meantime,<br />
Grant Skinner has created <a href="http://www.gskinner.com/blog/archives/2007/07/swfbridge_easie.html">this</a> to make the job a snap!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3blog.org/?feed=rss2&amp;p=45</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 3.174 seconds -->
