<?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>Ginger Binger</title>
	<atom:link href="http://gingerbinger.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gingerbinger.com</link>
	<description></description>
	<lastBuildDate>Mon, 07 May 2012 20:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>AS3 Garbage Collection with Closures</title>
		<link>http://gingerbinger.com/2011/09/as3-garbage-collection-with-closures/</link>
		<comments>http://gingerbinger.com/2011/09/as3-garbage-collection-with-closures/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 22:22:54 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=374</guid>
		<description><![CDATA[I was talking with Ido about memory usage in AS3, and we discovered a small weakness in the AVM&#8217;s garbage collector. If you use a functional style of programming with lots of anonymous or nested functions, here&#8217;s something to watch out for: sprite will not be garbage collected, even though it&#8217;s just a local variable [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I was talking with <a href="http://blog.tametick.com/">Ido</a> about memory usage in AS3, and we discovered a small weakness in the AVM&#8217;s garbage collector. If you use a functional style of programming with lots of anonymous or nested functions, here&#8217;s something to watch out for:</p>
<pre class="brush: as3; title: ; notranslate">
import flash.display.Sprite;
import flash.events.Event;

function foo():* {
    var sprite:Sprite = new Sprite();
    sprite.addEventListener(Event.ENTER_FRAME, tick);

    // this closure creates a reference to the outer scope, foo
    return function():void { }
}

function tick(event:Event):void {
    trace(&quot;tick&quot;);
}

var func:* = foo();
System.gc();
// sprite is never garbage collected, and continues to tick
</pre>
<p><code>sprite</code> will not be garbage collected, even though it&#8217;s just a local variable of <code>foo</code> and no other references to it exist!</p>
<p><span id="more-374"></span></p>
<p>The issue is the anonymous function returned by <code>foo</code>. This closure creates a reference to the outer scope, <code>foo</code>. This is necessary so that you can access the outer variables from inside the nested function. For example, the inner function might have done <code>sprite.x = 10</code>. </p>
<p>Unfortunately, <strong>outer variables are still referenced and stay alive, even if you never use them inside the closure!</strong> In our case, the free variable <code>sprite</code> is still referenced by the closure even though it&#8217;s never used in the anonymous function. It remains ineligible for garbage collection. Notice that if we change the return to <code>return 0</code>, then <code>sprite</code> is collected and stops ticking.</p>
<p>Here are some tips to avoid this issue:</p>
<ul>
<li><strong>Ensure that closures will be garbage collected by clearing references to them.</strong> If we eventually do <code>func = null</code>, then our closure is no longer reachable and will be collected, along with <code>sprite</code>.
<li><strong>Avoid using nested functions and anonymous functions.</strong> Instead of using an anonymous inner function, we could move the function outside:
<pre class="brush: as3; title: ; notranslate">
function foo():*
{
    return myFunc;
}

function myFunc():* { }
</pre>
</li>
<li><strong>Null out any local variables at the end of the outer function.</strong> We can set <code>sprite = null</code> at the end of the function since we won&#8217;t be using it any longer.</li>
</ul>
<p>This isn&#8217;t a big issue, but it&#8217;s something to keep in mind if you&#8217;re juggling function references with chunky objects like <code>BitmapData</code>. Is it possible for a VM to be smart enough to reference only the objects that are actually needed, instead of pushing the entire outer scope? I&#8217;m not sure. I wonder if other VMs such as <a href="http://code.google.com/p/v8/">V8</a> handle this any better?</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/09/as3-garbage-collection-with-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Skincraft</title>
		<link>http://gingerbinger.com/2011/06/skincraft/</link>
		<comments>http://gingerbinger.com/2011/06/skincraft/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 21:46:11 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[minecraft]]></category>
		<category><![CDATA[newgrounds]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=352</guid>
		<description><![CDATA[My buddies Shawn Tanner and Michael Swain have released Skincraft, an awesome skin creator for Minecraft. Check it out on Newgrounds: I helped out with the 3D display of the Minecraft character, as well as the file sharing features of the Newgrounds API that allow people to upload their skins. I&#8217;ve been hard at work [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>My buddies <a href="http://www.afro-ninja.com">Shawn Tanner</a> and <a href="http://www.theswain.com">Michael Swain</a> have released <a href="http://www.newgrounds.com/portal/view/571250">Skincraft</a>, an awesome skin creator for <a href="http://www.minecraft.net/">Minecraft</a>. Check it out on Newgrounds:</p>
<div id="attachment_353" class="wp-caption aligncenter" style="width: 350px">
	<a href="http://www.newgrounds.com/portal/view/571250" title="Skincraft Title"><img src="http://gingerbinger.com/wp-content/uploads/2011/06/skincraft.png" alt="Skincraft [newgrounds.com]" title="Skincraft Title" width="350" height="197" class="size-full wp-image-353" /></a>
	<p class="wp-caption-text">Skincraft</p>
</div>
<p>I helped out with the 3D display of the Minecraft character, as well as the file sharing features of the <a href="http://www.newgrounds.com/wiki/creator-resources/flash-api">Newgrounds API</a> that allow people to upload their skins. I&#8217;ve been hard at work on the Newgrounds API for several months, and I think this is one of the best examples of it yet!</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/06/skincraft/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS2 Dithered Transition Effect</title>
		<link>http://gingerbinger.com/2011/03/as2-dithered-transition-effect/</link>
		<comments>http://gingerbinger.com/2011/03/as2-dithered-transition-effect/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 02:19:55 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as2]]></category>
		<category><![CDATA[effects]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=331</guid>
		<description><![CDATA[Tom was looking for some old school effects to use in The Room Tribute game, so I cooked up some pixel-style transitions. By creating a small BitmapData and scaling it to the entire stage, you can create many fun, chunky effects that even AS2 can handle. Now Flash can look like an ancient version of [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><a href="http://tomfulp.newgrounds.com">Tom</a> was looking for some old school effects to use in <a href="http://www.newgrounds.com/portal/view/547307">The Room Tribute</a> game, so I cooked up some pixel-style transitions. By creating a small BitmapData and scaling it to the entire stage, you can create many fun, chunky effects that even AS2 can handle. Now Flash can look like an <a href="http://members.chello.at/theodor.lauppert/windows/colors/">ancient version of Windows&#8230;</a> <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="attachment_333" class="wp-caption aligncenter" style="width: 400px">
	<a rel="shadowbox;width=400;height=400" href="http://gingerbinger.com/projects/flash/pixelfade.swf" title="dither"><img src="http://gingerbinger.com/wp-content/uploads/2011/03/dither.png" alt="AS2 Dither Transition" title="dither" width="400" height="399" class="size-full wp-image-333" /></a>
	<p class="wp-caption-text">Dithered Transitions</p>
</div>
<p><a href="http://www.gingerbinger.com/projects/flash/pixelfade.zip">Click here to download the source (Flash 8+ FLA).</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/03/as2-dithered-transition-effect/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Happy 20th Birthday, Street Fighter II</title>
		<link>http://gingerbinger.com/2011/03/happy-20th-birthday-street-fighter-ii/</link>
		<comments>http://gingerbinger.com/2011/03/happy-20th-birthday-street-fighter-ii/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 10:47:02 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Life]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[memories]]></category>
		<category><![CDATA[street fighter]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=265</guid>
		<description><![CDATA[I don&#8217;t have many memories from when I was very young, but one sticks out clearly in my mind. My mom was shopping at the mall, with my brother and me in tow. As we walked past the Hills department store, a blast of electronic noise caught my ear. An arcade had opened in the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I don&#8217;t have many memories from when I was very young, but one sticks out clearly in my mind. My mom was shopping at the mall, with my brother and me in tow. As we walked past the Hills department store, a blast of electronic noise caught my ear. An arcade had opened in the mall, and like moths to flame, my brother and I stormed into it. To a kid, no experience is quite as visceral as walking into an arcade. And the first thing I saw was this:</p>
<p><span id="more-265"></span></p>
<div class="wp-caption aligncenter" style="width: 425px">
	</p>
<p><a href="http://www.youtube.com/watch?v=X00SZHGBxcU" rel="shadowbox[sbpost-265];player=swf;width=640;height=385;">http://www.youtube.com/watch?v=X00SZHGBxcU</a></p>
<p>
	<p class="wp-caption-text">Street Fighter II: The World Warrior (1991) Attract Mode</p>
</div>
<p>The synthesized guitar, the booming drum samples, the giant showcase cabinet with the huge screen &#8212; Capcom&#8217;s Street Fighter II had an intoxicating attract mode, and immediately, I was mesmerized.</p>
<p>SFII is easily among the most influential video games of all time. Not only did it define the fighting game genre, it also brought on a whole new era of gaming. It signified the death of the <a href="http://en.wikipedia.org/wiki/Golden_age_of_video_arcade_games">&#8220;Golden Age&#8221; of arcade games</a> that marked the &#8217;80s. No longer were games about defeating the computer and getting a high score. Who cares how many dots you could eat? Man vs. machine wasn&#8217;t interesting anymore. The ultimate opponent is man itself.</p>
<p>Games were now a social event. How did you perform the special moves? Before the <a href="http://www.gamefaqs.com">Internet era</a>, word of mouth was the only way for these memes to spread. Quarters would line up on the arcade cabinet, each challenger attempting to dethrone the incumbent. Each player had a personality. You could play with honor. You could play cheap. You could have an ego. Your ego could be crushed.  For one player, there&#8217;s a thrilling victory. For the other, agonizing defeat.</p>
<div class="wp-caption aligncenter" style="width: 425px">
	</p>
<p><a href="http://www.youtube.com/watch?v=v7cW2nMf1gk" rel="shadowbox[sbpost-265];player=swf;width=640;height=385;">http://www.youtube.com/watch?v=v7cW2nMf1gk</a></p>
<p>
	<p class="wp-caption-text">The famous "Daigo Parry" in Street Fighter III: 3rd Strike</p>
</div>
<p>The game was wildly popular, and updates and sequels were plentiful. SF2 was ported to nearly every home console around, and new fighting games flooded the market. Within a decade, there were over a dozen games released in the Street Fighter series alone.</p>
<p>Each new game brought a new system to map out. Strategies would evolve, and an <a href="http://sonichurricane.com/articles/sfterms.html">entire nomenclature</a> was developed to discuss the games. Chess masters talk about discovered checks and knight forks; Street Fighters talk about tick throws and option selects. <a href="http://www.shoryuken.com">Communities grew</a>, and rivalries flared. Eventually, nations would battle.</p>
<div class="wp-caption aligncenter" style="width: 425px">
	</p>
<p><a href="http://www.youtube.com/watch?v=GPzqod70Q4M" rel="shadowbox[sbpost-265];player=swf;width=640;height=385;">http://www.youtube.com/watch?v=GPzqod70Q4M</a></p>
<p>
	<p class="wp-caption-text">Daigo Umehara and Alex Valle battle for the Street Fighter Alpha 3 World Championship, circa 1998</p>
</div>
<p>I&#8217;ve played the Street Fighter series for most of my life, but I was just a Kumbaya guitarist playing a scant few chords. Only in recent years have I gone in depth with the games, jumping deep down the rabbit hole and trying to get better. I regularly get together with a small group of dedicated players to go a few rounds. We&#8217;ll have fun, we&#8217;ll talk tactics, we&#8217;ll talk smack &#8212; all in an effort push the bar higher. Even in the most trivial pursuits, improvement is fulfilling. <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Happy birthday, Street Fighter II.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/03/happy-20th-birthday-street-fighter-ii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS3 Mouse Wheel Trouble</title>
		<link>http://gingerbinger.com/2011/03/as3-mouse-wheel-trouble/</link>
		<comments>http://gingerbinger.com/2011/03/as3-mouse-wheel-trouble/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 23:07:48 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=244</guid>
		<description><![CDATA[If you&#8217;ve ever tried to add mouse wheel support to a Flash web app, it&#8217;s likely you&#8217;ve run into this problem: Scrolling the mouse wheel will scroll the web page, regardless of any mouse wheel listeners or code you have inside the Flash. There seems to be no way to easily prevent the web page [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>If you&#8217;ve ever tried to add mouse wheel support to a Flash web app, it&#8217;s likely you&#8217;ve run into this problem: Scrolling the mouse wheel will scroll the web page, regardless of any mouse wheel listeners or code you have inside the Flash. There seems to be no way to easily prevent the web page from scrolling. <a href='http://gingerbinger.com/wp-content/uploads/2011/03/MouseWheelSimple.swf' rel='lightbox;width=550;height=400'>Here&#8217;s an example</a>.</p>
<p>This is a regression from AS2. In AS2, adding a mouse wheel listener would prevent the web page from scrolling. Not so in AS3. Even <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#preventDefault()">preventDefault</a> doesn&#8217;t seem to help.</p>
<p><span id="more-244"></span></p>
<p>Currently, the best solution is to disable the mouse wheel in the browser using JavaScript such as with Liam O&#8217;Donnell&#8217;s <a href="http://www.spikything.com/blog/index.php/2009/11/27/stop-simultaneous-flash-browser-scrolling/">MouseWheelTrap</a>. Unfortunately, this solution only works if you have script access on the page that contains your Flash. In some cases, you don&#8217;t have control over where your SWF is hosted. It&#8217;d be nice if there was a Flash-side solution.</p>
<p>There is a little workaround that prevents the page from scrolling. If the mouse is over a TextField, and the TextField has more text than it can display, the mouse wheel will scroll the TextField, <em>not</em> scroll the web page! We can use this to our advantage by placing a TextField over the entire stage. Fill it with a ton of blank lines, prevent it from scrolling so it never reaches the top or bottom, and now it prevents the web page from scrolling. <a href='http://gingerbinger.com/wp-content/uploads/2011/03/MouseWheelBroken.swf' rel='lightbox;width=550;height=400'>Here is the workaround in action</a>.</p>
<pre class="brush: as3; title: ; notranslate">
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);

// fill textfield with empty lines
const NUM_LINES:uint = 1000;
var dummyText:String = &quot;&quot;;
for(var i:uint = 0; i &lt; NUM_LINES; i++) {
	dummyText += &quot;\n&quot;;
}
textField.text = dummyText;
// initially scroll to middle of text, so that we can scroll in both directions
textField.scrollV = NUM_LINES/2;

function onMouseWheel(event:MouseEvent):void {
	clip.y -= event.delta;
	// prevent TextField text from scrolling,
	// so that we can scroll indefinitely
	event.preventDefault();
}
</pre>
<p>This solution has a huge problem, though. Try clicking on the blue square in the first example, and then again in the second example with the workaround. Since a TextField is covering the stage, it will steal <strong>all</strong> of your MouseEvents, including clicks and rollovers. All of your buttons and mouse events will stop working! This might be OK in some cases where you don&#8217;t need full mouse functionality, such as a game that only checks mouseX and mouseY. But is there any way to use this workaround while still maintaining other mouse functionality?</p>
<p>The answer is <strong>probably not</strong>. I&#8217;ve tried all sorts of crazy schemes, such as:</p>
<ul>
<li>setting to TextField&#8217;s width and height to 0 and making it follow the mouse,</li>
<li>listening for MouseOver events on the stage and adding the TextField as a child of the object the mouse is over,</li>
<li>fiddling with the TextField.mouseEnabled in ENTER_FRAME or RENDER handlers,</li>
<li>redispatching MouseEvents captured by the TextField to the clips that should have received them (!)</li>
</ul>
<p>Most of these failed to maintain normal mouse control while always preventing the web page from scrolling. The last option seemed to be successful, but that&#8217;s an awful lot of messy work just to use a mouse wheel, and who knows if it causes any other problems!</p>
<p>Ideally, simply calling mouseEvent.preventDefault() should prevent the web page from scrolling. It looks like this is already a <a href="http://bugs.adobe.com/jira/browse/FP-2247">feature request on Adobe&#8217;s bug database</a>, so if this has been bothering you as well, you should vote for it to encourage Adobe to fix this issue.</p>
<p>Cheers to <a href="http://www.glaielgames.com/">Tyler Glaiel</a> for turning me on to this problem.</p>
<h3>Edit &#8211; May 04, 2011</h3>
<p><a href="http://etcs.ru/pre/MouseWheelCatcher/">Dennis Kolyako created a clever workaround</a> that captures the mouse wheel by loading an AS2 movie inside the AS3 movie, allowing us to get the desired AS2 mouse functionality. This seems to be the best solution, so be sure to check it out. <a href="#comment-795">Thanks to Gaen for pointing it out!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/03/as3-mouse-wheel-trouble/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Julia sets on the GPU with Molehill</title>
		<link>http://gingerbinger.com/2011/03/julia-sets-on-the-gpu-with-molehill/</link>
		<comments>http://gingerbinger.com/2011/03/julia-sets-on-the-gpu-with-molehill/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 16:55:18 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Pixel Bender]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[fractals]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[molehill]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=226</guid>
		<description><![CDATA[Everyone has been buzzing about Molehill, the new low-level 3D API that will be available in Flash Player 11. This API finally gives Flash developers the GPU acceleration that they&#8217;ve been waiting for. It&#8217;s a wrapper over OpenGL/DirectX, giving you a nice interface with a OpenGL ES 2.0 featureset. That&#8217;s right, all the Z-buffered triangles [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Everyone has been buzzing about <a href="http://labs.adobe.com/technologies/flashplatformruntimes/incubator/features/molehill.html">Molehill</a>, the new <a href="http://blogs.adobe.com/flashplatform/2011/01/digging-more-into-the-molehill-apis.html">low-level 3D API</a> that will be available in Flash Player 11. This API finally gives Flash developers the GPU acceleration that they&#8217;ve been waiting for. It&#8217;s a wrapper over OpenGL/DirectX, giving you a nice interface with a OpenGL ES 2.0 featureset. That&#8217;s right, all the Z-buffered triangles and shaders your heart desires. <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="wp-caption aligncenter" style="width: 400px">
	<a href="http://www.gingerbinger.com/projects/flash/MolehillJulia/MolehillJulia.html" title="Julia set rendered in Flash Player Incubator"><img alt="Julia set rendered in Flash Player Incubator" src="http://www.gingerbinger.com/projects/flash/MolehillJulia/MolehillJulia.jpg" title="Julia set rendered in Flash Player Incubator" width="400" height="400" /></a>
	<p class="wp-caption-text">Julia set rendered in Flash Player Incubator</p>
</div>
<p>Adobe released a <a href="http://labs.adobe.com/technologies/flashplatformruntimes/incubator/">public beta of Flash Player 11</a>, so I grabbed it and tried to get my <a href="http://gingerbinger.com/2010/06/julia-set-fractal-with-pixel-bender/">Pixel Bender Julia set plotter</a> running in the player. Unfortunately, Adobe hasn&#8217;t yet released the latest Pixel Bender Toolkit, which you would use to compile shaders in Adobe&#8217;s shader format for use with Molehill. But then <a href="http://ncannasse.fr/blog/announcing_hxsl">HxSL</a> appeared, from the brilliant <a href="http://ncannasse.fr/">Nicolas Cannasse</a> &#8212; a shader compiler released <i>before</i> Adobe&#8217;s official tools!</p>
<p><span id="more-226"></span></p>
<p>I did a quick port of the Julia set code to HxSL, and then went through and hand-optimized the assembly output. The Molehill API is still a beta, so there are some hiccups. Branches and loops aren&#8217;t available in shaders yet. HxSL currently unrolls your loops, duplicating your code. Even a small loop can only be unrolled so much before you hit the shader instruction limit &#8212; you can only have a max of 256 instructions per shader. I took a gander at Adobe&#8217;s assembler, AGALMiniAssembler, and noticed there were flow control instructions such as rep, ife, and ifg. Sadly, these aren&#8217;t yet working. You&#8217;ll get an &#8220;unimplemented&#8221; error if you try to use them. Hopefully these are implemented before release.</p>
<p>So right now, I&#8217;m stuck with a Julia set renderer that can only chug for a handful of iterations. If or when the flow control instructions are implemented, this shader can be optimized, and many more cool shaders could be created.</p>
<p>For now, <a href="http://www.gingerbinger.com/projects/flash/MolehillJulia/MolehillJulia.html">check it out action</a> or <a href="http://www.gingerbinger.com/projects/flash/MolehillJulia/MolehillJulia.zip">download the source</a>! This demo requires that you have the <a href="http://labs.adobe.com/technologies/flashplatformruntimes/incubator/">Flash Player Incubator</a> beta installed.</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2011/03/julia-sets-on-the-gpu-with-molehill/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActionScript 3 Version Checking</title>
		<link>http://gingerbinger.com/2010/09/actionscript-3-version-checking/</link>
		<comments>http://gingerbinger.com/2010/09/actionscript-3-version-checking/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 02:49:40 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[swf]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=186</guid>
		<description><![CDATA[The Flash Player makes a decent effort to be forward-compatible. When you run an Flash Player 10 targeted SWF in Flash Player 9, it&#8217;ll run fine until you start using Flash Player 10 specific features. At best, the code will just fail silently or pop up an error dialog. But at worst, the file will fail [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The Flash Player makes a decent effort to be forward-compatible. When you run an Flash Player 10 targeted SWF in Flash Player 9, it&#8217;ll run fine <strong>until</strong> you start using Flash Player 10 specific features. At best, the code will just fail silently or pop up an error dialog. But at worst, the file will fail to play at all! Here are some examples of features that will cause errors if the user is running an old version:</p>
<ul>
<li>3D transformations.</li>
<li>The Vector class.</li>
<li>Dynamic audio using SampleDataEvent.</li>
<li>Global error handling.</li>
<li>Multitouch and accelerometer events.</li>
</ul>
<p>Even seemingly innocuous code can cause problems, such as Event.ADDED_TO_STAGE—it was first added in Flash Player 9 Revision 1! Therefore, <strong>it&#8217;s very important to check that the user has the Flash Player version that we expect</strong>. Otherwise, you may get some nasty reviews stating that your game refuses to work. <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-186"></span></p>
<p>You can check the player version using <a title="ActionScript 3.0 Reference" href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabilities.html?allClasses=1#version">Capabilities.version</a>. Here&#8217;s a helpful function to check that the user has the version we require. For example, calling isFlashVersion(10,1) will return true if the user is running Flash Player version 10.1 or greater.</p>
<pre class="brush: as3; title: ; notranslate">function isFlashVersion(major:uint, minor:uint = 0, build:uint = 0, internalBuild:uint = 0):Boolean
{
    var version:Array = flash.system.Capabilities.version.split(&quot; &quot;)[1].split(&quot;,&quot;);
    var requiredVersion:Array = arguments;

    for (var i:uint = 0; i &lt; requiredVersion.length; i++)
        version[i] = uint(version[i]);

    for (i = 0; i &lt; requiredVersion.length; i++)
    {
        if (version[i] &gt; requiredVersion[i])
            return true;
        if (version[i] &lt; requiredVersion[i])
            return false;
    }
    return true;
}
</pre>
<p>It&#8217;s best to do a version check in your preloader before any offending code can run. Unfortunately, some features like Vector will cause Flash Player 9 to error out immediately if they are referenced anywhere on the main timeline. Therefore, <strong>the preloader should instantiate the application class indirectly</strong>, like so:</p>
<pre class="brush: as3; title: ; notranslate">
// instatiate class Game without directly referencing it
var GameClass:Class = Class( flash.utils.getDefinitionByName(&quot;Game&quot;) );
var game:Sprite = new GameClass();
</pre>
<p>This will allow your preloader to run regardless of the other content in your SWF. Now your preloader can check whether the Flash Player is the version that your SWF requires. If not, you can display a message directing the user to the <a title="Adobe Flash Player" href="http://get.adobe.com/flashplayer/">Flash Player download page</a>. This can help ensure that your Flash content reaches as many people as possible. <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2010/09/actionscript-3-version-checking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Events: The Myth of useWeakReference</title>
		<link>http://gingerbinger.com/2010/07/actionscript-3-0-events-the-myth-of-useweakreference/</link>
		<comments>http://gingerbinger.com/2010/07/actionscript-3-0-events-the-myth-of-useweakreference/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 13:10:16 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[useweakreference]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=109</guid>
		<description><![CDATA[Memory management is tricky when working with a garbage collector. Throw in a complex display list hierarchy with lots of event handlers, and it&#8217;s definitely a challenge! It&#8217;s very easy to overlook some hidden object reference, causing objects to not be marked as garbage. Soon your memory usage is spiraling out of control! This has [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Memory management is tricky when working with a garbage collector. Throw in a complex display list hierarchy with lots of event handlers, and it&#8217;s definitely a challenge! It&#8217;s very easy to overlook some hidden object reference, causing objects to not be marked as garbage. Soon your memory usage is spiraling out of control! This has lead many developers such as <a title="AS3: Weakly Referenced Listeners" href="http://www.gskinner.com/blog/archives/2006/07/as3_weakly_refe.html">Grant Skinner</a> and <a title="useWeakReference:Boolean = false" href="http://ted.onflash.org/2008/09/useweakreferencesboolean-false.php">Ted Patrick</a> to suggest always setting useWeakReference to true when you call addEventListener.</p>
<p>This is indeed a good idea and can be a helpful failsafe, but I think that it fails to emphasize another important action: <strong>Always remove your event listeners.</strong> I fear that many developers may be given a false sense of security by always using weakly referenced listeners, so I&#8217;ve tried to create a clear description of how event listeners create references and how to clean them up. This assumes you have an idea of how Flash&#8217;s mark-sweep garbage collector works. If not, check out Grant&#8217;s excellent <a href="http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html">AS3 Resource Management</a> articles, which are a must read for any Flash developer.</p>
<p><span id="more-109"></span></p>
<h3>Event listeners and references</h3>
<p>Let&#8217;s look at the creation of a simple event listener:</p>
<pre class="brush: as3; light: true; title: ; wrap-lines: false; notranslate">dispatcher.addEventListener(Event.COMPLETE, listener.handlerMethod);</pre>
<p>Here we can see the two objects involved in event handling: the dispatcher and the listener. Note that they could refer to the same object. They could also be omitted, in which case they implicitly refer to &#8220;this&#8221; (i.e., the object running that line of code).</p>
<p><strong>addEventListener creates a reference from the dispatcher to the listener.</strong> The reference is needed to call the handler function when the event fires. This means that listener can not be garbage collected until it is removed from the dispatcher, or until dispatcher is also eligible for garbage collection.</p>
<div id="attachment_171" class="wp-caption aligncenter" style="width: 420px">
	<a href="http://gingerbinger.com/wp-content/uploads/2010/07/rootToDispatcher.gif" rel="shadowbox[sbpost-109];player=img;" title="An example of an event dispatcher preventing garbage collection of listeners"><img class="size-medium wp-image-171  " title="An example of an event dispatcher preventing garbage collection of listeners" src="http://gingerbinger.com/wp-content/uploads/2010/07/rootToDispatcher.gif" alt="" width="380" height="230" /></a>
	<p class="wp-caption-text">(a) The root holds a reference to an event dispatcher. Because the listener is still reachable in the object graph, it can not be garbage collected until (b) the listener is removed using removeEventListener, or (c) the references to the dispatcher itself are cleared, allowing for both items to be collected.</p>
</div>
<p><strong>Listeners do not prevent a dispatcher from being garbage collected.</strong> References are one way, not bidirectional. If we cleared all references to dispatcher, it would have no references pointing to it. It would be eligible for garbage collection, regardless of whether we removed the listener.</p>
<div id="attachment_173" class="wp-caption aligncenter" style="width: 420px">
	<a href="http://gingerbinger.com/wp-content/uploads/2010/07/rootToListener.gif" rel="shadowbox[sbpost-109];player=img;" title="Listeners do not prevent garbage collection of the dispatcher"><img class="size-full wp-image-173 " title="Listeners do not prevent garbage collection of the dispatcher" src="http://gingerbinger.com/wp-content/uploads/2010/07/rootToListener.gif" alt="" width="250" height="230" /></a>
	<p class="wp-caption-text">(a) The root contains an object that is listening to a dispatcher. Even though someone is still listening, (b) the dispatcher is unreachable and is eligible for garbage collection, assuming no other references to the dispatcher exist.</p>
</div>
<h3>useWeakReference</h3>
<p>Let&#8217;s look at how useWeakReference affects things. <strong>Weak references are not traversed during mark-sweep garbage collection</strong>. Therefore, weak references will allow your listener objects to be garbage collected in certain situations where they otherwise would not. Let&#8217;s consider the common example of a Player class that would watch for key presses:</p>
<pre class="brush: as3; light: true; title: ; notranslate">public class Player extends MovieClip {
	public function initPlayer():void {
		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
	}

	public function onKeyDown(e:Event):void { /* ... */ }
}
</pre>
<p>Imagine that our player dies, and we want him to be cleaned up. However, the event listener creates a reference from the stage to the player. The stage is the topmost display object and is always accessible. Therefore, when the mark-sweep process runs, this event listener allows the garbage collector to hop from the stage to our player object, even if we&#8217;ve cleared all other references and removed it from the display list. Our player object will never be collected and will sit around wasting memory! But what if we set useWeakReference to true?</p>
<pre class="brush: as3; first-line: 2; light: true; title: ; notranslate">
		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
</pre>
<p>Now, despite our listener, the garbage collector can not travel from the stage to our player object. The player will be marked as unreachable and eligible for collection. Weak references helped us out here.</p>
<div id="attachment_174" class="wp-caption aligncenter" style="width: 470px">
	<a href="http://gingerbinger.com/wp-content/uploads/2010/07/stageKeyboardListener.gif" rel="shadowbox[sbpost-109];player=img;" title="A player listening to keyboard events from the stage"><img class="size-medium wp-image-174    " title="A player listening to keyboard events from the stage" src="http://gingerbinger.com/wp-content/uploads/2010/07/stageKeyboardListener.gif" alt="" width="470" height="230" /></a>
	<p class="wp-caption-text">(a) The player is visible on the stage, listening for keyboard presses. If the player dies and we remove him from the stage, (b) the event listener will prevent his garbage collection. (c) Using a weakly referenced listener will allow the player to be garbage collected, even if we forget to remove the listener.</p>
</div>
<p>However, let&#8217;s consider a different example. Imagine a shooting game where an enemy is killed when clicked by the user:</p>
<pre class="brush: as3; light: true; title: ; notranslate">
public class Game extends MovieClip {
	private var enemy:Enemy;
	public function Game() {
		enemy = new Enemy();
		addChild(enemy);
		enemy.addEventListener(MouseEvent.MOUSE_DOWN, onEnemyClicked, false, 0, true);
	}

	public function onEnemyClicked(event:Event):void
	{
		removeChild(enemy);	 // kill the enemy
		enemy = null;
	}
}
</pre>
<p>In this example, the enemy is the dispatcher, creating a reference from the enemy to the root game object. It doesn&#8217;t matter if we use a weak reference here. After our enemy dies, when the garbage collector runs, it can&#8217;t hop from the root to the enemy. The reference is in the opposite direction, from enemy to root! Therefore, the enemy doesn&#8217;t get marked and will be eligible for garbage collection, even though someone is still listening to it.</p>
<div id="attachment_172" class="wp-caption aligncenter" style="width: 420px">
	<a href="http://gingerbinger.com/wp-content/uploads/2010/07/rootToEnemyListener.gif" rel="shadowbox[sbpost-109];player=img;" title="A parent object listens to an event from its component"><img class="size-medium wp-image-172  " title="A parent object listens to an event from its component" src="http://gingerbinger.com/wp-content/uploads/2010/07/rootToEnemyListener.gif" alt="" width="340" height="230" /></a>
	<p class="wp-caption-text">(a) The root has many references to an enemy, and is listening for an event from the enemy. When the enemy dies and is removed from the display list, (b) the enemy is unreachable and is eligible for garbage collection, even if we don&#39;t use a weak reference or remove the listener.</p>
</div>
<p><strong>useWeakReference only has an effect when the listener object has a shorter lifetime than the dispatcher.</strong> Usually this happens when a &#8220;child&#8221; object is listening for events from a &#8220;parent&#8221; object. In our first example, the player was a transient child object listening to the long-lived global stage. This is the minority case—more often, an object will listen to events from child objects or itself.</p>
<p>Unfortunately, all of this talk misses an even bigger problem. Regardless of any use of weak references, <strong>an object may continue to dispatch and listen to events until it gets garbage collected</strong>. Since the garbage collector runs at some indeterminate point in the future (possibly never), this could take quite a while! In our first case, the stage will continue to throw KEY_DOWN events, triggering a response inside the player, even though he&#8217;s dead! For events like KEY_DOWN and ENTER_FRAME, this is a source for many subtle bugs and performance problems. <strong>We really need to remove our listeners manually!</strong></p>
<h3>Clean up after yourself</h3>
<p>All of the above pitfalls are avoided if we follow a simple rule: Regardless of whether or not you use weak references, <strong>at the end of an object&#8217;s lifetime, always remove any listeners that it created.</strong> If you clear your listeners when you&#8217;re done with them, then you will both prevent your events from firing when an object is &#8220;dead&#8221;, and you will ensure that listeners are not preventing garbage collection of your objects!</p>
<p>Here&#8217;s a good checklist to follow when you are done with an object:</p>
<ol>
<li>Remove it from the display list.</li>
<li>If it&#8217;s a MovieClip, tell it to stop().</li>
<li>Remove any event listeners that the object has created.</li>
<li>Clear any references in parent objects by setting them to null.</li>
</ol>
<p><strong>Implementing a dispose method</strong> on your objects makes it easy to manage your cleanup duties. Here&#8217;s some sample code illustrating these concepts:</p>
<pre class="brush: as3; collapse: true; light: false; title: ; toolbar: true; notranslate">
package
{
	import flash.display.MovieClip;
	public class Game extends MovieClip
	{
		public var player:Player;

		public function Game()
		{
			player = new Player();	// creates a reference from Game to Player
			addChild(player);		// creates both a reference from Game to Player and vice versa
			player.initPlayer();	// more references created here (see below)

		}

		// all player die someday!
		public function killPlayer():void
		{
			removeChild(player);	// clear the two references created by the display list
			player.dispose();		// tell player to clear its own references and event listeners
			player = null;		// finally, get rid of the direct variable reference
		}
	}
}

package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Player extends MovieClip
	{
		public function initPlayer():void {
			// creates a reference from the stage to player
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

			// creates a self-reference from player to player
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onKeyDown(event:Event):void	{ /* ... */ }
		private function onEnterFrame(event:Event):void	{ /* ... */ }

		public function dispose():void
		{
			// clean up after ourself!
			stop();
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2010/07/actionscript-3-0-events-the-myth-of-useweakreference/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Doom, Heretic, and Hexen in Flash</title>
		<link>http://gingerbinger.com/2010/06/doom-heretic-and-hexen-in-flash/</link>
		<comments>http://gingerbinger.com/2010/06/doom-heretic-and-hexen-in-flash/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 10:39:06 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobe alchemy]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[doom]]></category>
		<category><![CDATA[heretic]]></category>
		<category><![CDATA[hexen]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=90</guid>
		<description><![CDATA[This project was originally posted on my Newgrounds.com blog on December 4, 2008. Near the end of 2008, Adobe released Alchemy, a toolset to cross-compile C++ into ActionScript libraries. Naturally, the first thing that popped into my mind was to try to port Doom! This port is based on the vanilla Doom source code for [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><em>This project was originally posted on my <a href="http://mike.newgrounds.com/news/post/233620">Newgrounds.com blog</a> on December 4, 2008.</em></p>
<p>Near the end of 2008, Adobe released <a href="http://labs.adobe.com/technologies/alchemy/">Alchemy</a>, a toolset to cross-compile C++ into ActionScript libraries. Naturally, the first thing that popped into my mind was to try to port Doom!</p>
<div id="attachment_40" class="wp-caption aligncenter" style="width: 300px">
	<a rel="shadowbox;width=960;height=600" href="http://www.gingerbinger.com/projects/flash/doom.swf" title="Doom"><img src="http://gingerbinger.com/wp-content/uploads/2010/06/doom-300x187.jpg" alt="" title="Doom" width="300" height="187" class="aligncenter size-medium wp-image-100" /></a>
	<p class="wp-caption-text">Doom running in Flash</p>
</div>
<p>This port is based on the vanilla <a href="http://doom.wikia.com/wiki/Doom_source_code">Doom source code</a> for Linux. A few months later, I added Heretic and Hexen support using the original DOS source of Heretic and Hexen. These sources were released by <a href="http://www.idsoftware.com">id Software</a> and <a href="http://www.ravensoftware.com">Raven Software</a> under the GPL. The (messy) code for my port is also released under the GPL, available on <a href="http://github.com/Herschel/flash-doom">GitHub</a>.</p>
<p>These type of reverse-engineering projects are always fun. It&#8217;s a blast to look through the source of an ancient game! And it&#8217;s truly an experience when the game suddenly springs to life after a late-night hacking session. It was such a trip when I first saw &#8220;R_Init: Init DOOM refresh daemon&#8221; in my Flash output window&#8230; <img src='http://gingerbinger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If I were to start again, I&#8217;d probably base it on a more modern source port like <a href="http://prboom.sourceforge.net/">PrBoom</a>. That way I&#8217;d gain many of the bug fixes and cool features that these ports have solved years ago!</p>
<p>This port did manage to win <a href="http://www.fhm.com/online-games/web-games-awards-2009-shortlist">FHM&#8217;s 2009 Web Game Awards</a>! The amount of work I did to port such an awesome game was negligible, so I decided to <a href="http://mike.newgrounds.com/news/post/308833">donate the prize to charity</a>. Go open source!</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2010/06/doom-heretic-and-hexen-in-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Game Architecture</title>
		<link>http://gingerbinger.com/2010/06/actionscript-3-0-game-architecture/</link>
		<comments>http://gingerbinger.com/2010/06/actionscript-3-0-game-architecture/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 07:49:45 +0000</pubDate>
		<dc:creator>Mike Welsh</dc:creator>
				<category><![CDATA[Talks]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game development]]></category>

		<guid isPermaLink="false">http://gingerbinger.com/?p=4</guid>
		<description><![CDATA[I had the pleasure of giving a talk on ActionScript 3.0 game programming at Flash Camp Philadelphia 2009. I spoke about how games are built, the pros and cons of the standard game inheritance hierarchy, and a few alternatives to it. I also talked about some techniques such as: Game loop timing using getTimer() Decoupling game logic [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I had the pleasure of giving a talk on ActionScript 3.0 game programming at <a href="http://flashcamp.pfpaug.org/">Flash Camp Philadelphia 2009</a>. I spoke about how games are built, the pros and cons of the standard game inheritance hierarchy, and a few alternatives to it. I also talked about some techniques such as:</p>
<ul>
<li>Game loop timing using getTimer()</li>
<li>Decoupling game logic and animation from Flash Player&#8217;s frame rate</li>
<li>Rolling your own display list for benefits such as depth sorting</li>
<li>Using the Flex Builder profiler to optimize your game</li>
</ul>
<p>A lot of these techniques are used by the newer game frameworks such as <a href="http://www.flixel.org/">Flixel</a>, <a title="FlashPunk" href="http://flashpunk.net/" target="_blank">FlashPunk</a>, and <a title="PushButtonEngine" href="http://www.pushbuttonengine.com/" target="_blank">PushButton Engine</a>, so it&#8217;s good to have an understanding of what&#8217;s going on behind the scenes.</p>
<p>This talk was given on November 7, 2009 at Philadelphia University. You can <a href="http://www.gingerbinger.com/projects/talks/ActionScript_3_Game_Architecture.zip">download the slides and some sample code here</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://gingerbinger.com/2010/06/actionscript-3-0-game-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

