<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>H1DD3N.R350URC3 &#187; Source Code</title>
	<atom:link href="http://blog.hexagonstar.com/tag/source-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hexagonstar.com</link>
	<description>turn-based glory and pixel pleasure</description>
	<lastBuildDate>Sat, 10 Dec 2011 02:45:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Customizing toString()</title>
		<link>http://blog.hexagonstar.com/customizing-tostring/</link>
		<comments>http://blog.hexagonstar.com/customizing-tostring/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 17:15:56 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/?p=640</guid>
		<description><![CDATA[When writing classes for a framework I usually put a custom toString method into important classes so that debugging becomes easier. Normally they would go like something along the lines of: override public function toString():String { return "[ClassName]"; } &#8230; Sometimes adding properties to the returned String that give back information about the class, e.g. [...]]]></description>
			<content:encoded><![CDATA[<p>When writing classes for a framework I usually put a custom toString method into important classes so that debugging becomes easier. Normally they would go like something along the lines of:</p>
<pre lang="actionscript3">override public function toString():String {
	return "[ClassName]";
}</pre>
<p>&#8230; Sometimes adding properties to the returned String that give back information about the class, e.g.</p>
<pre lang="actionscript3">override public function toString():String {
	return "[ImageClass, size=" + _size + "]";
}</pre>
<p>But writing them rigid like that is a disadvantage when you decide later to refactor class names. Admittedly it&#8217;s also not a very elegant way so I got the idea to take the class name that is returned by <em>getQualifiedClassName()</em>. The only problem is that getQualifiedClassName not only provides the type name but also the whole package String of the class. Regular Expressions to the rescue! After twiddling around with them for a while (I&#8217;m by no means a RegExp expert) I got my toString method into the shape that I desired:</p>
<pre lang="actionscript3">override public function toString():String {
	return "[" + getQualifiedClassName(this).match("[^:]*$")[0] + ", size=" + _size + "]";
}</pre>
<p>This way it matches the String returned by getQualifiedClassName with the Regular Expression [^:]*$ which checks from the right end for an arbitrary text up to the first occurring colon, but without including the colon. Taking the first element of the Array returned by match() and you got what you need!</p>
<p>A nice way of using this is when writing abstract classes that contain the toString method and any subclass can use that toString method without the need to override it &#8230; that is of course unless you want ot add other output information.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/customizing-tostring/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>ActionScript3 Dice Class</title>
		<link>http://blog.hexagonstar.com/diceclass/</link>
		<comments>http://blog.hexagonstar.com/diceclass/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 11:29:23 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Random Picks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[D20]]></category>
		<category><![CDATA[D6]]></category>
		<category><![CDATA[Dice]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Lab]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[SWF]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/aboutrollingdice/</guid>
		<description><![CDATA[<script type="text/javascript" src="http://blog.hexagonstar.com/wp-content/plugins/pb-embedflash/js/sbadapter/shadowbox-jquery.js"></script><script type="text/javascript" src="http://blog.hexagonstar.com/wp-content/plugins/pb-embedflash/js/shadowbox.js"></script><script type="text/javascript"><!--
window.onload = function() {var options ={assetURL:'',loadingImage:'http://blog.hexagonstar.com/wp-content/plugins/pb-embedflash/css/images/loading.gif',flvPlayer:'http://blog.hexagonstar.com/wp-content/plugins/pb-embedflash/swf/mediaplayer.swf',animate:true,animSequence:'wh',overlayColor:'#000',overlayOpacity:0.85,overlayBgImage:'http://blog.hexagonstar.com/wp-content/plugins/pb-embedflash/css/images/overlay-85.png',listenOverlay:true,autoplayMovies:true,showMovieControls:true,resizeDuration:0.35,fadeDuration:0.35,displayNav:true,continuous:false,displayCounter:true,counterType:'default',viewportPadding:20,handleLgImages:'resize',initialHeight:160,initialWidth:320,enableKeys:true,keysClose:['c', 'q', 27],keysPrev:['p', 37],keysNext:['n', 39],handleUnsupported:'',text: {cancel:'Cancel',loading: 'loading',close:'<span class="shortcut">C</span>lose',next:'<span class="shortcut">N</span>ext',prev:'<span class="shortcut">P</span>revious',errors:{single: 'You must install the <a href="{0}">{1}</a> browser plugin to view this content.',shared: 'You must install both the <a href="{0}">{1}</a> and <a href="{2}">{3}</a> browser plugins to view this content.',either: 'You must install either the <a href="{0}">{1}</a> or the <a href="{2}">{3}</a> browser plugin to view this content.'}}};Shadowbox.init(options);}
--></script>In game development randomness is often necessary for certain tasks, be it the random distribution of graphic tiles, a random factor in NPC AI or random stats in a roleplaying game. Especially for the latter purpose the static Dice class provides a set of methods to roll dice as it is common in a Role-playing [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="alignnone size-full wp-image-217 aligncenter" src="http://blog.hexagonstar.com/wp-content/uploads/dice_collection.jpg" alt="" width="500" height="179" /></p>
<p>In game development randomness is often necessary for certain tasks, be it the random distribution of graphic tiles, a random factor in NPC AI or random stats in a roleplaying game. Especially for the latter purpose the static <em>Dice class</em> provides a set of methods to roll dice as it is common in a Role-playing game, to be exact four-, six-, eight-, ten-, twelve-, twenty-sided and percentile dice.</p>
<p><span id="more-162"></span></p>
<p>The Dice class (and it&#8217;s supporting classes) are rather elaborated, using for example the <a href="http://en.wikipedia.org/wiki/Linear_congruential_generator" target="_blank">Linear Congruential algorithm </a>in the process of generating &#8216;true&#8217; random numbers so it might not be the most speed-optimized method for calculating random numbers. For absolute performance the LCA and rounding routines can be removed to speed up calculations.</p>
<p>Using the Dice class is very simple! For example rolling two ten-sided dice can be done with the following call &#8230;</p>
<pre lang="actionscript3">var result:int = Dice.tenSided(2);</pre>
<p>&#8230; rolling the percentile die is even simpler as it does not need any arguments. It always returns a value between 1 and 100 &#8230;</p>
<pre lang="actionscript3">var result:int = Dice.percentile();</pre>
<p>&#8230; the class also provides the  <em>roll </em>method with that any x-sided die could be rolled, 3 sixteen-sided dice for instance &#8230;</p>
<pre lang="actionscript3">var result:int = Dice.roll(16, 3);</pre>
<p>The following small Flex application uses the Dice class and can be used to test dice throw probability according to the <a href="http://www.darkshire.net/jhkim/rpg/systemdesign/dice-methods.html" target="_blank">bell curve </a>(the more dice are used the lower the probability to roll boundary results).</p>
<p style="text-align: center;"><small>(Please open the article to see the Flash content.)</small></p>
<p><a href="http://blog.hexagonstar.com/download/diceclass.zip" title="Download AS3 Dice Class v1.0.0 (6.89 kB, downloaded 993 times)"><img src="http://blog.hexagonstar.com/wp-content/uploads/downloadbutton.png" alt="Download AS3 Dice Class v1.0.0 (6.89 kB, downloaded 993 times)" /></a></p>
<p>Update: The Dice class is now part of the hexagonLib and can be found <a href="http://code.google.com/p/hexagon/source/browse/trunk/hexagon/src/hexagonlib/flash/com/hexagonstar/util/Dice.as" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/diceclass/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Animated Bitmap Class</title>
		<link>http://blog.hexagonstar.com/animatedbitmapclass/</link>
		<comments>http://blog.hexagonstar.com/animatedbitmapclass/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 17:25:29 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Random Picks]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Lab]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Sprite]]></category>
		<category><![CDATA[SWF]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/animatedbitmapclass/</guid>
		<description><![CDATA[The AnimatedBitmap class provides functionality for Bitmap objects that are animated by using a series of still images. When creating a new AnimatedBitmap you provide a BitmapData object that contains an image that consists of the &#8216;single-frame&#8217; images for the animation. What are the advantages over using a generic MovieClip? When writing games you might [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-1547" title="animbitmap_demo" src="http://blog.hexagonstar.com/wp-content/uploads/animbitmap_demo-150x106.jpg" alt="" width="150" height="106" />The <strong>AnimatedBitmap</strong> class provides functionality for Bitmap objects that are animated by using a series of still images. When creating a new AnimatedBitmap you provide a BitmapData object that contains an image that consists of the &#8216;single-frame&#8217; images for the animation.</p>
<p><span id="more-158"></span></p>
<p><strong>What are the advantages over using a generic MovieClip?</strong> When writing games you might have several animated graphics (also called sprites, but not related to the AS3 Sprite class) that should run with a different framerate than the game&#8217;s  global framerate. Let&#8217;s say your game runs with a global framerate of 99 and you put several animated sprites into your game that were created for playing back with a framerate of 24. With a MovieClip all those sprites would also play with a framerate of 99 which means they play way too fast. However with an AnimatedBitmap you can set every framerate individually. There are a couple of other advantages like that a Bitmap is more lightweight than  a MovieClip and it has a isPlaying() method. Also it changes the way of how to embed assets. Instead of embedding many files for one animation only one image for  a whole animation sequence is embedded which has positive effects on the file size. The ring sequence used in the demo has 21 frames that use 102Kb as single images but only 44Kb when they are combined to one image.</p>
<p>The following demonstration plays with a global framerate of 99 but all AnimatedBitmap instances play at a framerate of 24 &#8230;</p>
<p style="text-align: center;"><small>(Please open the article to see the Flash content.)</small></p>
<p>You can download the class including demo source code and demo image here:</p>
<p><span style="text-decoration: line-through;"><p><a href="http://blog.hexagonstar.com/download/animatedbitmap.zip" title="Download AS3 AnimatedBitmap Class v1.1.0 (118.29 kB, downloaded 6158 times)"><img src="http://blog.hexagonstar.com/wp-content/uploads/downloadbutton.png" alt="Download AS3 AnimatedBitmap Class v1.1.0 (118.29 kB, downloaded 6158 times)" /></a></p><br />
</span></p>
<p><strong>Update</strong>: The class is now part of the <strong>hexagonLib</strong> and can  be found <a href="http://code.google.com/p/hexagon/source/browse/trunk/hexagon/src/hexagonlib/flash/com/hexagonstar/display/bitmaps/AnimatedBitmap.as" target="_blank">here</a>. You want this version because it&#8217;s the one that is being maintained.</p>
<p><strong>Changes in v1.1</strong><br />
The timer object used to time the frame animation has now been made external. For this purpose a custom <em>FrameRateTimer</em> class has been added. This has the advantage that AnimatedBitmap objects don&#8217;t use their own timer objects anymore. Instead you can use one timer for many animated objects if they use the same framerate. This saves memory and CPU cycles. However you are still able to use one <em>FrameRateTimer</em> for every animated object if it is desired.<br />
An <em>IAnimatedDisplayObject</em> interface has been added so that future animated display object types can be integrated more easily.</p>
<p>Also included is a jsx script for Photoshop CS named <strong>HDRS- CreateImageSequence</strong> that can be used to easily create a &#8216;single-frames image&#8217; with Photoshop. Simply run the script in Photoshop and choose the images in the file browser that appears. Photoshop will then generate a single image with all the frames laid out horizontally. All images should have the same size to get a correct animation sequence. Also sometimes you need to fine-tune the position of some frames, e.g. for the ring in the demo (which was rendered to single frame images from a 3D modeler) I had to adjust the horizontal alignment on some frames because otherwise the animation would glitch to left/right by some pixels.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/animatedbitmapclass/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>ActionScript 3 MultiMap Class</title>
		<link>http://blog.hexagonstar.com/as3_multimap_class/</link>
		<comments>http://blog.hexagonstar.com/as3_multimap_class/#comments</comments>
		<pubDate>Sun, 10 Jun 2007 08:28:11 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/actionscript-3-multimap-class/</guid>
		<description><![CDATA[Recently I needed a HashMap for a project to map key/value pairs but in that particular case the Map required to map not just one but several values to a key. I could have used an array or object to store the values in and map that one but in practice it turned out that [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed a HashMap for a project to map key/value pairs but in that particular case the Map required to map not just one but several values to a key. I could have used an array or object to store the values in and map that one but in practice it turned out that accessing the map looked rather messy. It would be much more elegant to have a map to that multiple values can be mapped directly. After some investigation (strangely even Java seems not to have a MultiMap included) I came up with writing my own MultiMap class, so here it is!</p>
<p><span id="more-138"></span><br />
The MultiMap is heavily based on <a href="http://lab.polygonal.de/ds/" target="_blank">Michael Baczynski&#8217;s HashTable class</a> but I modified it to my requirements and added a couple of additional methods for luxury. At first I wrote an even more customized version that would decide automatically which hash function to use but as it turned out some of these changes weighted heavy on the performance, especially not using a strict equality operator (===) and having a HashEntry object with non-numeric keys. In fact Michael&#8217;s class is still a tad faster (he really squeezed out the last bit of performance there) but as long as the MultiMap isn&#8217;t too large/full there is only a minor difference of some milliseconds.<br />
As a trade-off I added checking for existing keys (which can be omitted to improve performance when adding values) and of course there is the multiple values functionality which required a more complex implementation of some methods.</p>
<p><a href="http://blog.hexagonstar.com/download/MultiMap.as" title="Download AS3 MultiMap Class v1.1.0 (12.25 kB, downloaded 1329 times)"><img src="http://blog.hexagonstar.com/wp-content/uploads/downloadbutton.png" alt="Download AS3 MultiMap Class v1.1.0 (12.25 kB, downloaded 1329 times)" /></a></p>
<p>I&#8217;m sure there is still a lot of room for improvement so if you have any suggestions it would be cool to let us know!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/as3_multimap_class/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Waterproof AS 3.0 Singleton</title>
		<link>http://blog.hexagonstar.com/waterproof_as3_singleton/</link>
		<comments>http://blog.hexagonstar.com/waterproof_as3_singleton/#comments</comments>
		<pubDate>Fri, 24 Nov 2006 14:22:48 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/waterproof-as-30-singleton/</guid>
		<description><![CDATA[While reading Advanced ActionScript 3 with Design Patterns I&#8217;ve once again came across the AS 3.0 Singleton solution that has to deal with the absence of private constructors (I&#8217;ll save my rage and cursings about who had the idea to remove them for now ) and while the authors are using the already well known [...]]]></description>
			<content:encoded><![CDATA[<p>While reading <a href="http://hiddenresource.corewatch.net/archives/69">Advanced ActionScript 3 with Design Patterns</a> I&#8217;ve once again came across the AS 3.0 Singleton solution that has to deal with the absence of private constructors (I&#8217;ll save my rage and cursings about who had the idea to remove them for now <img src='http://blog.hexagonstar.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) and while the authors are using the already well known method of using a SingletonEnforcer class outside the Singleton&#8217;s package to verify that the class cannot be  instanciated via the constructor they also note that this can be doublecrossed by giving <strong>null </strong>or <strong>undefined </strong>as a constructor parameter. As this is rather sub optimal I was wondering why they don&#8217;t just check inside the Singleton constructor for a null/undefined argument and throw an exception accordingly?!<br />
I&#8217;m sure somebody else had the same idea already but here&#8217;s my idea of a waterproof Singleton class that cannot be misused from the outside (As always, if I missed any detail that accidentally breaks hell loose feel free to correct me) &#8230;</p>
<pre lang="actionscript3" line="1">package
{
	public class WaterproofSingleton {
		static private var _instance:Singleton;

		public function Singleton(singletonEnforcer:SingletonEnforcer) {
			if ((singletonEnforcer as SingletonEnforcer) == null) {
				throw (new Error("Direct instantiation of a Singleton is not allowed!"));
			}
		}

		public static function getInstance():Singleton {
			if (Singleton._instance == null) {
				Singleton._instance = new Singleton(new SingletonEnforcer());
			}
			return Singleton._instance;
		}
	}
}

class SingletonEnforcer {}</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/waterproof_as3_singleton/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>AS3 Short Variable/Function Names vs Long Ones</title>
		<link>http://blog.hexagonstar.com/as3-short-variablefunction-names-vs-long-ones/</link>
		<comments>http://blog.hexagonstar.com/as3-short-variablefunction-names-vs-long-ones/#comments</comments>
		<pubDate>Tue, 29 Aug 2006 17:07:08 +0000</pubDate>
		<dc:creator>sascha</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://blog.hexagonstar.com/as3-short-variablefunction-names-vs-long-ones/</guid>
		<description><![CDATA[In ActionScript it was common knowledge that shorter Variable and Function names yielded better performance in situations were it depends but does that still hold true for ActionScript 3.0? I&#8217;ve ran a couple of quick tests to find a clear answer about this. I usually tend keep my Variable/Function names to 1-3 characters when performance [...]]]></description>
			<content:encoded><![CDATA[<p>In ActionScript it was common knowledge that shorter Variable and Function names yielded better performance in situations were it depends but does that still hold true for ActionScript 3.0? I&#8217;ve ran a couple of quick tests to find a clear answer about this. I usually tend keep my Variable/Function names to 1-3 characters when performance really mattered but it might actually not be necessary. Consider the following test &#8230;</p>
<pre lang="actionscript3" line="1">package
{
	import com.hexagonstar.util.debug.Debug;
	import flash.utils.getTimer;
	import flash.display.Sprite;

	public class PerformanceTest extends Sprite
	{
		private var aVeryVeryLongVariableNameaVeryVeryLongVariableName:Number;
		private var num:Number;

		public function PerformanceTest()
		{
			var startTime:Number = getTimer();
			for (var i:int = 0; i < 4000000; i++)
			{
				aVeryVeryLongVariableNameaVeryVeryLongVariableName = (i * Math.random()) * 100;
			}
			var timeTaken:String = getTimer() - startTime + " ms";
			Debug.trace("timeTaken (long): " + timeTaken);
		}
	}
}</pre>
<p>I've ran this code ten times with the 50 characters long variable name and then ten times with a 3 characters long one, both in their own SWF's. The results ranged from 757ms to 820ms, more or less the same on both sides. The sum of the long variable version was 7924ms and the one of the three characters long one was 7903ms, not much of a difference.</p>
<p>The next test was similar but this time with a 50 characters long Method name and the other again with a 3 characters long one, the Methods returned a random Number for 4000000 times during this test. The results looked similar but the total sum of each test gave a 12044ms for the long name test and 15570ms for the short named one. A noticable difference of over 3 seconds. This might have been coincidence but I've ran the tests a couple more times and it turned out every time that the result were very close together. This is quite interesting because I was safely believing that short names would still have an advantage. It might be time to rethink this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hexagonstar.com/as3-short-variablefunction-names-vs-long-ones/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

