<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Case Statements</title>
	<atom:link href="http://jacksondunstan.com/articles/335/feed" rel="self" type="application/rss+xml" />
	<link>http://jacksondunstan.com/articles/335</link>
	<description>Mastering AS3</description>
	<lastBuildDate>Mon, 14 May 2012 17:02:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Roger Clark</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-131</link>
		<dc:creator>Roger Clark</dc:creator>
		<pubDate>Tue, 06 Oct 2009 05:24:48 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-131</guid>
		<description>Hi Jackson,
Thanks for clearing that up. I&#039;d have never spotted the weak reference (as I didn&#039;t write the code in the first place).
But it just goes to show how important the strength of the linkage is.

Cheers

Roger Clark</description>
		<content:encoded><![CDATA[<p>Hi Jackson,<br />
Thanks for clearing that up. I&#8217;d have never spotted the weak reference (as I didn&#8217;t write the code in the first place).<br />
But it just goes to show how important the strength of the linkage is.</p>
<p>Cheers</p>
<p>Roger Clark</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-127</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Tue, 06 Oct 2009 02:17:22 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-127</guid>
		<description>The issue here is with your event listener. When you add it, you add it as a weak listener by explicitly passing &lt;tt&gt;true&lt;/tt&gt; for  the last parameter. Consider how the references are working here:

&lt;ol&gt;
&lt;li&gt;Local variables of particle() referenced by...&lt;/li&gt;
&lt;li&gt;The eFrame function in particle()...&lt;/li&gt;
&lt;li&gt;The ball sprite created in particle()...&lt;/li&gt;
&lt;li&gt;The instance of your Main class...&lt;/li&gt;
&lt;li&gt;The stage&lt;/li&gt;
&lt;/ol&gt;

But the reference of the eFrame function by the ball is weak, so it requires another reference in order to not get removed by the garbage collector on its next pass. You see this as 1 second, but it could be whenever the GC wants to do the collection. Since you have no other references to the eFrame function, it is &lt;a href=&quot;http://en.wikipedia.org/wiki/Weakly_reachable&quot; rel=&quot;nofollow&quot;&gt;weakly reachable&lt;/a&gt; and is therefore eligible for collection. When this happens, the eFrame function stops getting called. This causes the position to never get updated.

To avoid this problem in the future, do not add dynamic event listener functions as a weak reference unless you are sure you have another reference to them. Since you usually don&#039;t, just make them a strong reference and call &lt;tt&gt;removeEventListener&lt;/tt&gt; later.

Hope this helps clear things up.</description>
		<content:encoded><![CDATA[<p>The issue here is with your event listener. When you add it, you add it as a weak listener by explicitly passing <tt>true</tt> for  the last parameter. Consider how the references are working here:</p>
<ol>
<li>Local variables of particle() referenced by&#8230;</li>
<li>The eFrame function in particle()&#8230;</li>
<li>The ball sprite created in particle()&#8230;</li>
<li>The instance of your Main class&#8230;</li>
<li>The stage</li>
</ol>
<p>But the reference of the eFrame function by the ball is weak, so it requires another reference in order to not get removed by the garbage collector on its next pass. You see this as 1 second, but it could be whenever the GC wants to do the collection. Since you have no other references to the eFrame function, it is <a href="http://en.wikipedia.org/wiki/Weakly_reachable" rel="nofollow">weakly reachable</a> and is therefore eligible for collection. When this happens, the eFrame function stops getting called. This causes the position to never get updated.</p>
<p>To avoid this problem in the future, do not add dynamic event listener functions as a weak reference unless you are sure you have another reference to them. Since you usually don&#8217;t, just make them a strong reference and call <tt>removeEventListener</tt> later.</p>
<p>Hope this helps clear things up.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Clark</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-126</link>
		<dc:creator>Roger Clark</dc:creator>
		<pubDate>Tue, 06 Oct 2009 01:47:55 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-126</guid>
		<description>Hi Jackson. 
Thanks for the link to closures.

The interesting thing about the code is that the closures do not fully work.

If you change the number of &quot;particles&quot; that are created e.g.

&lt;pre lang=&quot;actionscript3&quot;&gt;
		private function mClick (event:MouseEvent):void
		{
			for (var i:uint=0; i&lt;1000; i++)
			{
				particle ();
			}
		}
&lt;/pre&gt;

and also slow their possible movement

&lt;pre lang=&quot;actionscript3&quot;&gt;
xVel = (Math.random() * 2)-1;
yVel = (Math.random() * 2)-1;
&lt;/pre&gt;

it doesn&#039;t work at all.

If you create 200 (and have slow movement) the particles start comming out from the mouse click position, but usually stop after about 1 second.

I suspect that although the vars ball, xVel and yVel are initially maintained because of closure, that the Flash garbage collector is eventually deleting them.</description>
		<content:encoded><![CDATA[<p>Hi Jackson.<br />
Thanks for the link to closures.</p>
<p>The interesting thing about the code is that the closures do not fully work.</p>
<p>If you change the number of &#8220;particles&#8221; that are created e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> mClick <span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                particle <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>and also slow their possible movement</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">xVel = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
yVel = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>it doesn&#8217;t work at all.</p>
<p>If you create 200 (and have slow movement) the particles start comming out from the mouse click position, but usually stop after about 1 second.</p>
<p>I suspect that although the vars ball, xVel and yVel are initially maintained because of closure, that the Flash garbage collector is eventually deleting them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-125</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Mon, 05 Oct 2009 23:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-125</guid>
		<description>I cleaned up your post a bit by wrapping it in code formatting:

&lt;pre lang=&quot;actionscript3&quot;&gt;
// code
&lt;/pre&gt;

You&#039;ve stumbled upon the wonderful world of &lt;a href=&quot;http://en.wikipedia.org/wiki/Closure_%28computer_science%29&quot; rel=&quot;nofollow&quot;&gt;closures&lt;/a&gt;. The &lt;tt&gt;eFrame&lt;/tt&gt; function has the local variables in the &lt;tt&gt;particle()&lt;/tt&gt; function &quot;closed&quot;, meaning they are bound to it like members of a class are bound to the object by &lt;tt&gt;this&lt;/tt&gt;. You may even notice that if you use &lt;tt&gt;this&lt;/tt&gt; explicitly within the &lt;tt&gt;eFrame&lt;/tt&gt; function that you&#039;ll be referring to the &lt;tt&gt;eFrame&lt;/tt&gt; function itself, not the instance of the &lt;tt&gt;Main&lt;/tt&gt; class that you would be referring to if you used &lt;tt&gt;this&lt;/tt&gt; from the &lt;tt&gt;particle()&lt;/tt&gt; function.

Closures can be tough to wrap your head around, but they&#039;re really powerful once you manage it. See these articles for some of that power:
&lt;a href=&quot;/articles/338&quot; rel=&quot;nofollow&quot;&gt;Curry Functions&lt;/a&gt;
&lt;a href=&quot;/articles/183&quot; rel=&quot;nofollow&quot;&gt;Fun With Dynamic Functions&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>I cleaned up your post a bit by wrapping it in code formatting:</p>
<p>&lt;pre lang=&quot;actionscript3&quot;&gt;<br />
// code<br />
&lt;/pre&gt;</p>
<p>You&#8217;ve stumbled upon the wonderful world of <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29" rel="nofollow">closures</a>. The <tt>eFrame</tt> function has the local variables in the <tt>particle()</tt> function &#8220;closed&#8221;, meaning they are bound to it like members of a class are bound to the object by <tt>this</tt>. You may even notice that if you use <tt>this</tt> explicitly within the <tt>eFrame</tt> function that you&#8217;ll be referring to the <tt>eFrame</tt> function itself, not the instance of the <tt>Main</tt> class that you would be referring to if you used <tt>this</tt> from the <tt>particle()</tt> function.</p>
<p>Closures can be tough to wrap your head around, but they&#8217;re really powerful once you manage it. See these articles for some of that power:<br />
<a href="/articles/338" rel="nofollow">Curry Functions</a><br />
<a href="/articles/183" rel="nofollow">Fun With Dynamic Functions</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Clark</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-124</link>
		<dc:creator>Roger Clark</dc:creator>
		<pubDate>Mon, 05 Oct 2009 22:47:47 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-124</guid>
		<description>As a followup to the switch / case scoping I&#039;ve just come across another rather interesting scoping example that someone posted to ActionScript.org with a question about why the code works for some of the time.
(I&#039;ve repackaged it, but its bascially the same code)

&lt;pre lang=&quot;actionscript3&quot;&gt;
package 
{
	import flash.display.Sprite;
	import flash.events.*;
	
	/**
	 * ...
	 * @author Roger Clark
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.addEventListener (MouseEvent.CLICK,mClick,false,0,true);
			// entry point
		}
		
		
				
		private function mClick (event:MouseEvent):void
		{
			for (var i:uint=0; i&lt;20; i++)
			{
				particle ();
			}
		}

		private function particle ():void
		{
			var xPos:Number;
			var yPos:Number;

			var xVel:Number;
			var yVel:Number;

			xPos = mouseX;
			yPos = mouseY;

			xVel = (Math.random() * 20)-10;
			yVel = (Math.random() * 20)-10;

			var ball:Sprite=new Sprite();
			ball.graphics.beginFill (0xff0000);
			ball.graphics.drawCircle (0,0,10);
			addChild(ball);

			ball.x = xPos;
			ball.y = yPos;

			ball.scaleX=ball.scaleY=(Math.random()*.5)+.5;

			ball.addEventListener (Event.ENTER_FRAME, eFrame, false, 0, true);
			
			function eFrame (event:Event):void
			{
				ball.x += xVel;
				ball.y += yVel;

				if (xPos&lt;0&#124;&#124;yPos&lt;0&#124;&#124;xPos&gt;stage.stageWidth&#124;&#124;yPos&gt;stage.stageHeight)
				{
					ball.removeEventListener (Event.ENTER_FRAME,eFrame);
					removeChild (ball);
					ball=null;
				}
			}
		}
	}
}
&lt;/pre&gt;

placing the enterframe handler inside the particle() func seems to almost achieve the particle() func being a class with xPos,yPos,xVel etc as properties.
Keep clicking on the stage, after a while it packs up. But I&#039;m kinda surprised it worked in the first place ;-)</description>
		<content:encoded><![CDATA[<p>As a followup to the switch / case scoping I&#8217;ve just come across another rather interesting scoping example that someone posted to ActionScript.org with a question about why the code works for some of the time.<br />
(I&#8217;ve repackaged it, but its bascially the same code)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.*;</span>
&nbsp;
    <span style="color: #3f5fbf;">/**
     * ...
     * @author Roger Clark
     */</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span> 
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> 
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span><span style="color: #000000;">&#41;</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ADDED_TO_STAGE</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> 
        <span style="color: #000000;">&#123;</span>
            <span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ADDED_TO_STAGE</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>mClick<span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #009900; font-style: italic;">// entry point</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> mClick <span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">20</span><span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                particle <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> particle <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> xPos<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> yPos<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #6699cc; font-weight: bold;">var</span> xVel<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> yVel<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            xPos = <span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span>
            yPos = <span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            xVel = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span>
            yVel = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #6699cc; font-weight: bold;">var</span> ball<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span> <span style="color: #000000;">&#40;</span>0xff0000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawCircle</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>ball<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = xPos<span style="color: #000066; font-weight: bold;">;</span>
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = yPos<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleX</span>=ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">scaleY</span>=<span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">*.</span>5<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">+.</span>5<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> eFrame<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #339966; font-weight: bold;">function</span> eFrame <span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= xVel<span style="color: #000066; font-weight: bold;">;</span>
                ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= yVel<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
                <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>xPos<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">||</span>yPos<span style="color: #000066; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">||</span>xPos<span style="color: #000066; font-weight: bold;">&gt;</span>stage<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageWidth</span><span style="color: #000066; font-weight: bold;">||</span>yPos<span style="color: #000066; font-weight: bold;">&gt;</span>stage<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    ball<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span>eFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                    <span style="color: #004993;">removeChild</span> <span style="color: #000000;">&#40;</span>ball<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                    ball=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>placing the enterframe handler inside the particle() func seems to almost achieve the particle() func being a class with xPos,yPos,xVel etc as properties.<br />
Keep clicking on the stage, after a while it packs up. But I&#8217;m kinda surprised it worked in the first place ;-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-98</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Mon, 05 Oct 2009 04:41:28 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-98</guid>
		<description>I also come from a C/C++ background and have been wary of some of AS3&#039;s fancier features. I endeavor to use them as they do have benefits (eg. the scoping protection you point out), but often do hit potholes and find gotchas. That&#039;s one of the major purposes of this blog: to clear the road for all of you. :) Thanks for commenting!</description>
		<content:encoded><![CDATA[<p>I also come from a C/C++ background and have been wary of some of AS3&#8242;s fancier features. I endeavor to use them as they do have benefits (eg. the scoping protection you point out), but often do hit potholes and find gotchas. That&#8217;s one of the major purposes of this blog: to clear the road for all of you. :) Thanks for commenting!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Clark</title>
		<link>http://jacksondunstan.com/articles/335/comment-page-1#comment-96</link>
		<dc:creator>Roger Clark</dc:creator>
		<pubDate>Sun, 04 Oct 2009 22:47:56 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=335#comment-96</guid>
		<description>This is just a personal opinion, but ...

But comming from a C,C++ background I&#039;ve always been very wary about the ActionScript compiler and its scoping vagueries, in AS2 and AS3.

In AS2 on the timeline, I&#039;ve seen numerous projects where bugs occuered due to vars or functions going out of scope depening on which frame the taget function was on and which frame the current execution was on. 
There did&#039;nt seem to be a pattern to this, as sometimes code would work and in another project, similar code would fail.

I generally don&#039;t use functions inside other functions. I know that putting functions inside other functions etc, does give some scoping protection of the function i.e.  it should not be visible outside the current scope.

But you&#039;ve demonstrated a big downside to this..</description>
		<content:encoded><![CDATA[<p>This is just a personal opinion, but &#8230;</p>
<p>But comming from a C,C++ background I&#8217;ve always been very wary about the ActionScript compiler and its scoping vagueries, in AS2 and AS3.</p>
<p>In AS2 on the timeline, I&#8217;ve seen numerous projects where bugs occuered due to vars or functions going out of scope depening on which frame the taget function was on and which frame the current execution was on.<br />
There did&#8217;nt seem to be a pattern to this, as sometimes code would work and in another project, similar code would fail.</p>
<p>I generally don&#8217;t use functions inside other functions. I know that putting functions inside other functions etc, does give some scoping protection of the function i.e.  it should not be visible outside the current scope.</p>
<p>But you&#8217;ve demonstrated a big downside to this..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

