<?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>Cameron McKay &#187; ActionScript</title>
	<atom:link href="http://cdmckay.org/blog/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +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>Trimming in ActionScript jQuery-style</title>
		<link>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/</link>
		<comments>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 18:26:45 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1344</guid>
		<description><![CDATA[Sometimes we want to remove whitespace from the ends of our strings. In fact, this task is so common on the web that the ubiquitous jQuery library includes a utility method for that purpose. What about in ActionScript 3? Well, not so much. The String class in AS3 is, in my opinion, a bit lacking [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we want to remove whitespace from the ends of our strings.  In fact, this task is so common on the web that <a href="http://jquery.com">the ubiquitous jQuery library</a> includes <a href="http://api.jquery.com/jQuery.trim/">a utility method</a> for that purpose.</p>
<p>What about in ActionScript 3?  Well, not so much.  The <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.htm">String class in AS3 is</a>, in my opinion, a bit lacking compared to languages like <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx">C#</a> and <a href="http://www.scala-lang.org/docu/files/api/scala/runtime/RichString.html">Scala</a>.  What&#8217;s worse, you can&#8217;t augment prototypes easily like you can in JavaScript to add missing methods (see update at the end for more information on this).</p>
<p><span id="more-1344"></span></p>
<p>So I needed to trim a string in AS3. A quick <a href="http://www.google.ca/#hl=en&#038;safe=off&#038;q=as3+trim">Google search</a> showed a couple so-so methods.  Then I got to thinking: Hey, doesn&#8217;t jQuery have a trim method?  Aren&#8217;t they both ECMAScript languages?  Shouldn&#8217;t something as basic as this be pretty similar in both languages?</p>
<p>The answer was yes to all those questions.  So I popped open <a href="http://code.jquery.com/jquery-1.4.2.js">the jQuery source</a> and did a quick search and found this a few lines into it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Used for trimming whitespace</span>
rtrim <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^(\s|\u00A0)+|(\s|\u00A0)+$/g</span><span style="color: #339933;">,</span></pre></div></div>

<p>Perfect.  I created a StringHelper AS3 file, plunked it in, and presto, we have an efficient trimmer in AS3:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> final <span style="color: #9900cc; font-weight: bold;">class</span> StringHelper
<span style="color: #000000;">&#123;</span>               
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> StringHelper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trim<span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #009966; font-style: italic;">/^(\s|\u00A0)+|(\s|\u00A0)+$/g</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trimStart<span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #009966; font-style: italic;">/^(\s|\u00A0)+/g</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</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;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> trimEnd<span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">return</span> str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #009966; font-style: italic;">/(\s|\u00A0)+$/g</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</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>

<h3>Update</h3>
<p>After this was <a href="http://www.reddit.com/r/flash/comments/bnrsm/trimming_in_actionscript_jquerystyle/">posted on Reddit</a>, <strong>theillustratedlife</strong> made the following comment regarding the prototype chain in ActionScript:</p>
<blockquote><p>
You are supposed to be able to enhance the ECMAScript prototypes, but you might have to call a compiler flag when you do it. If you look in the docs, you&#8217;ll notice that there are ECMAScript prototypes and interchangeable native code classes for the primitive types.</p></blockquote>
<p>After doing a bit more experimenting, it looks like you can fairly easily augment the prototype chain in ActionScript as well.  However, there are some caveats when running AS3 in strict mode.  For example, consider having the String class augmented like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">prototype</span><span style="color: #000066; font-weight: bold;">.</span>trim = <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> StringHelper<span style="color: #000066; font-weight: bold;">.</span>trim<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If we try to call the <code>trim()</code> method in the usual way</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;foo&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
str<span style="color: #000066; font-weight: bold;">.</span>trim<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>we&#8217;ll get a compile-time error because the AS3 compiler can&#8217;t find the method in the String class definition.  Fortunately, we can get around this by using a dynamic reference</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;foo&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
str<span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;trim&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>It&#8217;s a lot uglier, but it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/07/trimming-in-actionscript-jquery-style/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SpriteFactory &#8212; an AS3 library for creating multiple sprites using the same bitmap</title>
		<link>http://cdmckay.org/blog/2010/04/06/spritefactory-an-as3-library-for-creating-multiple-sprites-using-the-same-bitmap/</link>
		<comments>http://cdmckay.org/blog/2010/04/06/spritefactory-an-as3-library-for-creating-multiple-sprites-using-the-same-bitmap/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 01:18:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1336</guid>
		<description><![CDATA[While working on Flash sidescroller stuff I noticed that there was no easy way (that I could find) to create multiple sprites from a single bitmap (something you&#8217;d do when doing a tile-based graphics layout). Sooo&#8230; I made my own simple library called SpriteFactory. Here&#8217;s some example usage: var factory:SpriteFactory = new SpriteFactory&#40;&#34;assets/sprites&#34;&#41;; factory.loadBitmap&#40;&#34;grass&#34;, &#34;block-grass.png&#34;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>While working on Flash sidescroller stuff I noticed that there was no easy way (that I could find) to create multiple sprites from a single bitmap (something you&#8217;d do when doing a tile-based graphics layout).</p>
<p>Sooo&#8230; <a href="http://cdmckay.org/blog/projects/spritefactory">I made my own simple library called SpriteFactory</a>.</p>
<p>Here&#8217;s some example usage:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> factory<span style="color: #000066; font-weight: bold;">:</span>SpriteFactory = <span style="color: #0033ff; font-weight: bold;">new</span> SpriteFactory<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;assets/sprites&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
factory<span style="color: #000066; font-weight: bold;">.</span>loadBitmap<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;grass&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;block-grass.png&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> grass1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = factory<span style="color: #000066; font-weight: bold;">.</span>newSprite<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;grass&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> grass2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = factory<span style="color: #000066; font-weight: bold;">.</span>newSprite<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;grass&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p><a href="http://cdmckay.org/blog/projects/spritefactory">Check out the project page for more information.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/06/spritefactory-an-as3-library-for-creating-multiple-sprites-using-the-same-bitmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

