<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">

    <title type="text">Studio Koi Code Blog</title>
    <subtitle type="text">Studio Koi Code Blog:</subtitle>
    <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/code/index/" />
    <link rel="self" type="application/atom+xml" href="http://codeblog.studiokoi.com/share/site/atom/" />
    <updated>2008-08-26T02:12:23Z</updated>
    <rights>Copyright (c) 2008, Greg Ferrell</rights>
    <generator uri="http://www.pmachine.com/" version="1.6.3">ExpressionEngine</generator>
    <id>tag:codeblog.studiokoi.com,2008:08:26</id>


    <entry>
      <title>Making Anonymous Functions and Closures Work in ActionScript 2.0</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/making_anonymous_functions_and_closures_work_in_actionscript_20/" />
      <id>tag:codeblog.studiokoi.com,2008:share/code/index/1.39</id>
      <published>2008-08-26T01:35:01Z</published>
      <updated>2008-08-26T02:12:23Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>One of the most powerful idioms available in JavaScript is the anonymous function closure: e.g. (function(){})();. If you are unfamiliar with this, it simply creates an anonymous scope bubble that can be used to prevent automatic global variables, or trick JavaScript into allowing private variables. Unfortunately, this idiom isnt available in ActionScript 2.0. It simply doesnt work. (As of ActionScript 3.0, it works perfectly.)</p>
 
<p>This means that, on the surface, the only way to prevent time line variables from being constantly created is an init function of sorts. (ActionScript 2.0 has a pseudo global object which is the main time line of a movie level, and a totally global object named _global.) This also means that private variables are restricted to the creation of object classes. I personally get annoyed at times with classes in ActionScript. One of the things that has always bugged me with ActionScript 2.0 and the introduction of classes is that they are hard to make portable. (I work on no less than 3 computers in a single week, and 4-5 when on business trips, so portability is paramount to my work.) Rather than being able to simply include the classes in the same folder as the flash working file (*.fla), you must specify where on the computer the includes are. This is just annoying if you work on the file on several different computers. There are a couple of ways to avoid naming the class include folder on each computer, but they are as much of a pain as the default method. Fortunately, there is a solution.</p>

<p>To reiterate, while this works in JavaScript and ActionScript 3.0, it will not in ActionScript 2.0:
</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> myFunc = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> hiddenVar = <span style="color: #ff0000;">&quot;hidden&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> hiddenVar;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hiddenVar		<span style="color: #808080; font-style: italic;">//undefined</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myFunc.<span style="color: #006600;">hiddenVar</span>; 	<span style="color: #808080; font-style: italic;">//undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myFunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 		<span style="color: #808080; font-style: italic;">//returns &quot;hidden&quot;</span></div></li></ol></pre>

<p>The good news about ActionScript 2.0 is that it retains the ability to pass anonymous functions as variables to another function. Like so:
</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> myArray = <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> sortedArray = myArray.<span style="color: #0066CC;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> a - b;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>sortedArray<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//returns [1, 5, 6, 15, 23]</span></div></li></ol></pre>

<p>Knowing this, we can emulate anonymous function closures in ActionScipt 2.0. First we will create a global function that returns the function passed to it.</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">_global</span>.<span style="color: #006600;">closure</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> f;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>To use it, you treat it the same as you would the parenteses in the javascript idiom:
</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">closure<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>

<p>Changing the name to something shorter will make this more practical and the code a little cleaner.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">_global</span>.$ = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> f;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>

<p>Given this new ability, we can now apply anonymous closures to our script without littering the time line object with useless variables.</p> 

<p>Lets use a hypothetical situation to illustrate how we might use the anonymous closure. In this script, we want to make 5 buttons that each need to trace their respective number when you click them. We could code each button individually, but that is messy and doesn't promote good code reuse. Instead, lets make a loop that makes all the buttons work, in addition to making it easier to add more buttons.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i = <span style="color: #cc66cc;">1</span>; i &lt;= <span style="color: #cc66cc;">5</span>; i++<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//for each button</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;click&quot;</span> + i + <span style="color: #ff0000;">&quot;_btn&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onPress</span> = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//return a function that has access to n via</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// a closure</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #808080; font-style: italic;">//trace the number passed to the parent closure</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//pass in current loop iteration for caching</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>Now when you press click3_btn, it will trace '3' and click5_btn, '5'. In this way, we avoid creating useless variables inside each button, and we didn't have to create a singular use function.</p>

<p>Earlier I mentioned my slight distaste for how classes were implemented in ActionScript 2.0. Let me first caveat this statement by saying they are extremely useful and are wonderful for individuals or teams that have good version networks and servers. Just for myself and the smaller applications I write, I prefer portable objects. Lets see how we would write a singleton object with private variables and functions:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> bob = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> secret = <span style="color: #ff0000;">&quot;you found me&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private functions</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> a &gt; b;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> Bob = <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//public members/variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		works		:<span style="color: #000000; font-weight: bold;">true</span>,</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		getSecret	:<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> secret;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span>,</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		getCompare	:<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//return object</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> Bob;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//test</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> bob<span style="color: #66cc66;">&#41;</span> 		<span style="color: #808080; font-style: italic;">//returns object</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">works</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns true</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">getSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns the string: you found me</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">secret</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">getCompare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">compare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns undefined function</span></div></li></ol></pre>

<p>As you can see with the tests, we have a singleton object with private functions and variables as well as public methods and members. With a little work, using the prototypal inheritance method, we can make this a reusable pseudo class:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> Bob = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> secret = <span style="color: #ff0000;">&quot;you found me&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private functions</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> a &gt; b;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> Bob = <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//public members/variables</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">works</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//public methods</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Bob.<span style="color: #0066CC;">prototype</span>.<span style="color: #006600;">getSecret</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> secret;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Bob.<span style="color: #0066CC;">prototype</span>.<span style="color: #006600;">getCompare</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//return constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> Bob;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//invoke</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> mybob = <span style="color: #000000; font-weight: bold;">new</span> Bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//test</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> Bob<span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns function</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> mybob<span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns object	</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">works</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns true</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">getSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns the string: you found me</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">secret</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">getCompare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">compare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li></ol></pre>

<p>So, with a single global function, we can bring the magic of anonymous function closures to ActionScript 2.0. Enjoy!</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Parsing a nested or name spaced object from a string in ECMAScript</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/parsing_a_nested_or_name_spaced_object_from_a_string_in_ecmascript/" />
      <id>tag:codeblog.studiokoi.com,2008:share/code/index/1.23</id>
      <published>2008-01-10T01:37:00Z</published>
      <updated>2008-01-10T01:39:03Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>Often times when I am making dynamic scripts in Flash or JavaScript, I want to call an object method using a string. It may be a situation where I am stuck using <a href="http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary372.html" title="ActionScript Dictionary: fscommand">fscommand</a> (due to a customer request of a Flash version &lt;8) or just writing script with script.</p>

<p>The problem that comes with this method is the advent of ActionScript 3 with Flash CS3/Flex 2 and the rising use in namespaces by JavaScript developers (myself included). Both encour a heavy usage of object methods instead of one-off functions. Dynamically calling singleton functions is easy, you can just use subscript notation, e.g. object[&#8217;member/method&#8217;]. What you can&#8217;t do with this method is pass it any dot syntax and expect it to work e.g. object[&#8217;subobject.method&#8217;]. The subscript notation is only meant to work one level. So the previous example won&#8217;t work. In order to fix this issue and to make sending object methods/members in a single string a little easier, I wrote a function that takes a string and a base object (the default is the window object if no object reference is passed) and returns the method/member. (I know at this point that some will simply direct me to ECMAScript&#8217;s built in eval() function, but I personally avoid using it at all costs as it can have unintended, and unsecure results. I&#8217;ll let <a href="http://javascript.about.com/library/bleval.htm" title="About.com: Alternatives to eval">Stephen Chapmen tell you</a> because this article is about something else.)
<br />
</p>

<p>The function is executed somewhat simply by splitting the string at the dots and adding them as subscripts to one another in order. However, there are some other pieces to it that make it a little more friendly to mistakes and general usage.</p>

<p>
<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> parseNameSpace<span style="color: #66cc66;">&#40;</span>s , o<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//default to window if no object sent</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> w = o || window, a;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//reutrn if parsing not necessary or not a method</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> s.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #CC0000;">-1</span> <span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> w<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span> === <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #003366; font-weight: bold;">false</span> : w<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span> ;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make array for looping through</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	a = s.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//run though members / methods</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color: #CC0000;">0</span>,l = a.<span style="color: #006600;">length</span>; i &lt; l; i++<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		w = w<span style="color: #66cc66;">&#91;</span>a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//return object if it is valid, or return false</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> w === <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #003366; font-weight: bold;">false</span> : w ;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
</p>
<p>The function takes two variables, first the string that you want to parse to an object member, and the second optional base object. The first variable w looks for the object &#8220;o&#8221; and if not found, defaults to &#8220;window&#8221; which is the default parent object of browser based JavaScript. (You could change it to &#8220;this&#8221; and it would work in ActionScript as well). Second, we check to see if the passed string has any dots in it. If not, we check to see if it is a regular function. This way you could use this function as a catch all even if you aren&#8217;t always passing nested functions. If it isnt a member of the window object, false is returned. If it does have dots, we split them into an array, and loop through it, making each a member of the previous using the subscript notation. Finally we check to see if the result is valid, and we return false if it isn&#8217;t, or a reference to the object itself.</p>

<p>Let&#8217;s take a look at a simple way to use it. Lets say we have an object literal with a subobject, and a method inside that.</p>

<p>
<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	innerObj: <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		func: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;worked&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
</p>
<p>Then we retrieve it via string with the function.</p>

<p>
<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> objFunc = parseNameSpace<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;obj.innerObj.func&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>objFunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//shows &quot;worked&quot;</span></div></li></ol></pre>
</p>
<p>This should work most places as long as you are mindful of function scope. Enjoy!
</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Create Right Click Menus to Test AIR Files (Win/Mac)</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/create_right_click_menus_to_test_air_files_win_mac/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.21</id>
      <published>2007-08-22T05:18:02Z</published>
      <updated>2007-08-22T11:24:04Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="AIR"
        scheme="http://codeblog.studiokoi.com/share/site/category/air/"
        label="AIR" />
      <content type="html"><![CDATA[
        <p>At the beginning of this year, when I first heard about <a href="http://labs.adobe.com/technologies/air/" title="Adobe Integrated Runtime">A.I.R.</a> I was excited to learn more about it and see what I could do with it. What really jumpstarted my interest was a trip to Cincinnati, OH to the <a href="http://onair.adobe.com/" title="Adobe onAIR Bus Tour">Adobe onAIR Bus Tour</a>. At this point I am full steam excited about working with AIR.</p>

<p>I work on a Windows machine at work and Mac at home, so I am also excited to see cross platform development. One thing that is a little wonky about AIR is the way that you test your applications. Since AIR is built with an SDK instead of a builder application, such as Flash, you have you use the terminal to launch the SDK. So I came up with methods for OS X and Windows XP to use right-click contextual menus to test AIR applications. I will first note that you can use <a href="http://aptana.com" title="Aptana">Aptana</a>, <a href="http://labs.adobe.com/technologies/flex/" title="Flex 3 Beta &quot;Moxie&quot;">Flex 3</a>, <a href="http://labs.adobe.com/wiki/index.php/AIR:Flash_CS3_Professional_Update#Download_and_Install" title="Flash CS3 AIR Plug-in">Flash CS3</a>, and <a href="http://labs.adobe.com/wiki/index.php/AIR:Dreamweaver_CS3_Extension" title="Dreamweaver CS3 AIR Plug-in">Dreamweaver CS3</a> to test your AIR apps, but i thought i would make something for the hardcore coders. Though, I personally use all the aforementioned for AIR.</p>

<p>I will assume you have already installed the <a href="http://labs.adobe.com/downloads/airsdk.html" title="Adobe AIR SDK">SDK</a> on either system.</p>

<p>Windows XP: <a href="#mac">[skip to mac version]</a><hr /></p>

<p>In Windows XP, you can customize your right-click menus for any filetype with the 'folder options' panel. First, navigate to control panel, then folder options. Navigate to XML (It will be close to the bottom.).</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_slct_avd.gif" alt="image" width="386" height="475" /></p>

<p>If the button says 'Restore' where you expect it to say 'Advanced', it is because you have changed the Windows default opening program for the xml file type. Click 'Restore' first then you can click 'Advanced'. (When you are finished, you can change the opening program back when you are finished, the menu will still stick.)</p>

<p>Next, click the 'edit' button and we will create a new contextual command. You will need the full path of the location of your AIR SDK. For this example, I will use mine:</p>

<code>C:\Program Files\Adobe\AIR SDK\bin\adl.exe</code>

<p>This will application field in quotes, followed by "%1". This will notate that you are opening the file right-clicked with this program. (This works on most other windows programs as well.) Then give it a name you can remember. This is how it will show up in the contextual menu. Just replace my AIR bin location with yours.</p>

<code>"C:\Program Files\Adobe\AIR SDK\bin\adl.exe" "%1"</code>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_code.gif" alt="image" width="347" height="361" /></p>

<p>Now click OK and you should see your command in the window:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_fin.gif" alt="image" width="347" height="318" /></p>

<p>Now you can right-click your AIR application.xml and test them right from the Windows file explorer:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_rtclk.gif" alt="image" width="374" height="328" /></p>

<p>The CMD prompt will open, then your air file should open. The CMD prompt will be your error output window.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_adl_run.gif" alt="image" width="550" height="394" /></p>

<p>&nbsp;</p>
<p><a name="mac">OS X:</a><hr /></p>

<p>The Mac version will be somewhat similar, but we will use Automator to do the function. This means you will need at least OS X 10.4 for this tutorial.</p>

<p>Open Automator, and go to 'Finder' in the Library menu, under Applications. Drag 'Get Selected Finder Items' to the work space.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_get.gif" alt="image" width="540" height="104" /></p>

<p>Next, go to 'Automator' in the Library menu, and drag 'Run AppleScript' to the work space under 'Get Selected Finder Items'.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_aplscr.gif" alt="image" width="540" height="377" /></p>

<p>For the Applescript, I had a little help from <a href="http://www.macosxhints.com/article.php?story=20050827164648766" title="MacOSXHints.com | Use Automator to open chosen folder in Terminal">Mac OS X Hints</a>. You will need the directory for your Adobe AIR SDK. Mine is:</p>

<code>/Applications/Adobe AIR SDK/bin/adl</code>

<p>For this one, I will give the code to you. Paste this into the Applescript window and replace my AIR Adl location with yours:</p>

<pre class="applescript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">on</span> <span style="color: #000066;">run</span> <span style="color: #66cc66;">&#123;</span>input, parameters<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">tell</span> application <span style="color: #ff0000;">&quot;Terminal&quot;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		activate</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">the</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">count</span> <span style="color: #b1b100;">of</span> <span style="color: #000000; font-weight: bold;">the</span> window<span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066;">or</span> ¬</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">the</span> busy <span style="color: #b1b100;">of</span> window <span style="color: #cc66cc;">1</span> = <span style="color: #000066;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">tell</span> application <span style="color: #ff0000;">&quot;System Events&quot;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				keystroke <span style="color: #ff0000;">&quot;n&quot;</span> using command down</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">tell</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		do <span style="color: #b1b100;">script</span> <span style="color: #ff0000;">&quot;'/Applications/Adobe AIR SDK/bin/adl' <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> &amp; <span style="color: #66cc66;">&#40;</span>POSIX path <span style="color: #b1b100;">of</span> ¬</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#40;</span>input <span style="color: #000066;">as</span> string<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #b1b100;">in</span> window <span style="color: #cc66cc;">1</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">tell</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> input</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">end</span> <span style="color: #000066;">run</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>

<p>Next, we will save this as a Plug-in. Under the File menu, select 'Save As Plug-in..'. Then select finder as the application and give it a name you can remember:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_plug.gif" alt="image" width="351" height="296" /></p>
<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_save.gif" alt="image" width="406" height="143" /></p>

<p>Now you can just right-click your AIR application.xml, hover over Automater, and choose your new workflow to preview AIR projects!</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_rgtclk.gif" alt="image" width="558" height="500" /></p>

<p>Enjoy!</p>

 
      ]]></content>
    </entry>

    <entry>
      <title>Expression engine 1.6 Preview and Rick Ellis Birthday Contest</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/expression_engine_16_preview_and_rick_ellis_birthday_contest/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.20</id>
      <published>2007-06-19T04:14:00Z</published>
      <updated>2008-07-01T14:05:17Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="ExpressionEngine"
        scheme="http://codeblog.studiokoi.com/share/site/category/ExpressionEngine/"
        label="ExpressionEngine" />
      <content type="html"><![CDATA[
        <p>So Expression Engine is previewing version 1.6 (filled with LOADS of new features) and it is also EE CEO Rick Ellis' Birthday. To celebrate this and some new software, they are having a photoshop contest that ends noon Tuesday June 19th, 2007. Check out the <a href="http://expressionengine.com/forums/viewthread/54250/" title="Expression Engine Forums">forums here</a> for the other submissions. Here is the <a href="http://expressionengine.com/blog/entry/160_preview_and_multiple_rick_manager_birthday_contest/" title="Expression Engine Blog">original blog post</a>.</p>

<p>Without further adieu here are my entries.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/ricky_mouse_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/ricky_mouse.jpg" title="View Larger" class="thickbox">View Larger</a></p>

 <p><img src="http://codeblog.studiokoi.com/images/uploads/rick_oboe_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_oboe.jpg" title="View Larger" class="thickbox">View Larger</a></p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/rick_tricycle_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_tricycle.jpg" title="View Larger" class="thickbox">View Larger</a></p>


<p><img src="http://codeblog.studiokoi.com/images/uploads/rick_tiger_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_tiger.jpg" title="View Larger" class="thickbox">View Larger</a></p> 
      ]]></content>
    </entry>

    <entry>
      <title>Import classes and Multiple ActionScript layers in Flash</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/import_classes_and_multiple_actionscript_layers_in_flash/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.19</id>
      <published>2007-05-24T02:11:00Z</published>
      <updated>2007-05-24T01:15:03Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>At work I am making a new Flash interface for a project, and my goal this time around was to make as many global functions out of repetitive ones as possible so we could save development time.</p>

<p>As I began writing and testing Prototype class extensions and global functions, I noticed something I had not run into before with flash. If Import classes on one ActionScript layer, and call it on another Actions layer, or even the next frame, it produces an ActionScript error.</p>

<p>Normally, I put all actions and functions on one layer and have no keyframes unless they are inside other MovieClips, or I need to animate something to audio cues. With this current project, there was going to be so much ActionScript, I decided to do more than one Actions layer so I could keep everything straight. Never having a reason to do this until now, I was not prepared for some of the problems I ran into. First and foremost being that: If import a class on one Actions layer, and I create a new class object from that group on another layer, I get a syntax error.</p>

<p>Here is a quick example. I have a MovieClip named &#8220;box1&#8221; and I am going to tween it with the Tween class. normally I would just put it all on the same layer like this:</p>

<p>
<img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs1.gif"  alt="script" width="342" height="212" />
</p>
<p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> boxTween:Tween = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>box1, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, box1.<span style="color: #0066CC;">_x</span>, box1._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
</p>
<p>This works perfectly and makes the box move across the screen on load. When I ran into the error was when I decided that I wanted all imports on one actions layer and the object on another. Like so:</p>

<p>
<img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs2.gif"  alt="moved script" width="329" height="211" />
</p>
<p>I expected it to work just the same, as I have My publish settings set to ActionScript 2.0, top down, and Flash 8. Instead, I was presented with a nice error.</p>

<p>
<img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs3.gif"  alt="flash error" width="425" height="209" />
</p>
<p>The problem is replicated also by putting the new Tween object in the following frame. Effectively, any frame that ISN&#8217;T the one I imported the class into. This strikes me as totally odd, as functions endure over frames and from layer to layer. I also tested this with other import classes besides the Tween class. I had the same issues with the Alert class and BitmapData class.</p>

<p>For now, the simple solution I came up with was making a function in the same frame/layer as the import classes that I would simply call elsewhere when I needed the tween to happen. This of course makes no sense to me at all, but it works perfectly.</p>

<p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> tweenIt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> boxTween:Tween = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>box1, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, box1.<span style="color: #0066CC;">_x</span>, box1._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
</p>
<p>Calling this function in other frames and layers has worked so far, and has more than solved my problem. If you are using multiple tweens, or classes, you could use passthrough variables to be able to make the function usable in other situations.</p>

<p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> tweenIt<span style="color: #66cc66;">&#40;</span>mc:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	mc.<span style="color: #006600;">boxTween</span> = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>mc, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, mc.<span style="color: #0066CC;">_x</span>, mc._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">tweenIt<span style="color: #66cc66;">&#40;</span>box1<span style="color: #66cc66;">&#41;</span></div></li></ol></pre>
</p>
<p>If I found out any more info as to why this is a problem, I will post it here. If anyone else has some insight, please leave me a comment.
</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Dreamweaver Mass Find Replace Extension</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/dreamweaver_mass_find_replace_extension/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.18</id>
      <published>2007-05-21T03:46:00Z</published>
      <updated>2007-05-21T15:13:46Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Extensions"
        scheme="http://codeblog.studiokoi.com/share/site/category/Extensions/"
        label="Extensions" />
      <content type="html"><![CDATA[
        <p>At work I work with a lot of local files, and repetitive file groups. Meaning, I might have 30 folders all with the same CSS file or JavaScript file. The trouble I run into with using a lot of the same files is changing them, then having to copy/paste the file a gillion times. It gets really old. (To answer your immediate question as to why I don't just use one file outside of all the folders; I have to send each folder as a separate package to the client because the server they go on handles them as single entities. This however is beside the point of today's post, because you can use this in many situations.)</p>

<p>I work on a corporate network, so i can only install approved programs on my work computer. This keeps me from using my <a href="http://notepad-plus.sourceforge.net/uk/site.htm" title="Notepad++: A free, open-source text editor for Windows">preferred win32 text editor</a>. However, since I work with Flash a lot, I also have DreamWeaver, which is an acceptable text editor in a pinch (as long as you never EVER use the WYSIWYG window).</p>

<p>After using DreamWeaver a while, I've found that it does have a few very useful features. For instance, DreamWeaver has a lot of functions built into the api that can access the file system. These can be utilized within extensions; which brings me to today's goodie. The DreamWeaver Mass Find Replace extension.</p>

<p>I built this because of all the time I was wasting every time I needed to make changes to a file that was used multiple times. The nice part about DreamWeaver's access to the file system, is that it isn't restricted to just DreamWeaver file associations, so you can use this to replace any type of writable file. I use it for Swfs all the time.</p>

<p>Here is an example of how it works:
<ol>
<li>Open the extension and choose a master file (meaning, the file you want to replace all similarly named files with)</li>
<li>Choose the folder (and all sub folders) where you want to Find all files with the same name as the file you chose, and Replace them all with the chosen file.</li>
<li>Click OK.</li>
</ol>
</p>

<p>That's it. You're done! This is a very simple, but powerful tool, so be careful using it. It overwrites all files with the same name as the replace file without warning.</p>

<p><a href="http://codeblog.studiokoi.com/images/uploads/mass_find_replace.zip" class="download"></a></p> 
      ]]></content>
    </entry>

    <entry>
      <title>Moving away from the _root</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/moving_away_from_the_root/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.17</id>
      <published>2007-05-07T06:08:01Z</published>
      <updated>2007-05-07T05:09:26Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>When working with multiple, nested MovieClips, or dynamic variables, sometimes you need your ActionScript to refer out to other MovieClips, or the main timeline of the swf file. When I was first teaching myself ActionScript, I learned to use _root to reference outside of functions or MovieClips.</p>

<p>It is this point in my learning that see how wrong I was to rely on _root for every outside-the-MovieClip function.</p>

<p>First lets look at how _root is <s>wrongly</s> commonly used:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">_root</span>.<span style="color: #006600;">variableValue</span> = <span style="color: #cc66cc;">55</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">_root</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>Let's assume that this button code is on the main timeline. First of all, if 'variableValue' is on the maine timeline, and so is this button function, then you do not need to reference _root. When you call a function or variable inside of a function, Flash assumes already that you are referring to the timeline the parent function is in.</p>

<p>However, the dynamic variable on the second line poses a different problem. Most of the time, if you were going to do a dynamic variable without using _root, you would simply use 'this'. Like so:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li></ol></pre>

<p>Using 'this' should be treated with as much caution as _root. Whenever you say 'this' you mean the entity you are currently in. So, if you are writing a regular function, 'this' refers to the main timeline, because the function is in the main timeline. This principle is implicit.</p>

<p>To illustrate, lets use the previous button example coded on the timeline and replace _root with 'this':</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">this</span>.<span style="color: #006600;">variableValue</span> = <span style="color: #cc66cc;">55</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>First things first, this doesn't work (and this is what turns people to using _root). What confuses many people is why this works on the main timeline, inside regular functions, but not in this instance. When you write a button function like this, you are making a references to an event, <i>onPress</i> , given by the object <i>button1_btn</i>. So 'this' refers to <i>button1_btn</i> and not the main timeline. To clarify, using 'this' inside of a timeline written button function refers to the button, calling a variable or function works as it would inside a normal function.</p>

<p>In many places where you might use _root, you can also use _parent, which is one MovieClip out of where you are (_parent is stackable to go out as many times as you need). For the button code, by itself, it still wont work.</p> 

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">_parent</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This function would refer to the parent of the timeline the function is in. Because as we just learned, if 'this' isn't referred, the button function is treated as a normal timeline function. So to get the function to work without using _root, we need to combine the two.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_parent</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This works because 'this' refers to <i>button1_btn</i>, therefore _parent refers to <i>button1_btn</i>'s parent, the timeline this function is in.</p>

<p>Now for the advanced solution.</p>

<p>Using this._parent could get a little redundant after a while, and would cease to work if you moved the function to a different MovieClip. The best solution i have found is to make a variable that is data-typed as a MovieClip and refer it to the timeline you are wanting to reference.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">home</span>:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">this</span></div></li></ol></pre>

<p>What this says is that anytime you call <i>home</i> you will be referring to the timeline. (You can also refer to other timelines by replacing 'this' with _parent, <i>MovieClip</i> or <i>MovieClip.ChildClip</i>).</p>

<p>Let's see how it would work:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">home</span>:<span style="color: #0066CC;">MovieClip</span> = <span style="color: #0066CC;">this</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">home</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;myDynamicVariable&quot;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>So now you have no excuse what-so-ever to use _root unless you really REALLY mean it!</p>
 
      ]]></content>
    </entry>

    <entry>
      <title>Batch Publish Flash Plug&#45;in</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/batch_publish_flash_plug_in/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.14</id>
      <published>2007-04-26T20:39:01Z</published>
      <updated>2007-05-21T15:14:07Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Extensions"
        scheme="http://codeblog.studiokoi.com/share/site/category/Extensions/"
        label="Extensions" />
      <content type="html"><![CDATA[
        <p>Lately I have been working on a project that has a lot of flash files that use external ActionScript files for similar functions. It helps that they have many of the functions external because you can make mass changes with one file.</p>

<p>For those not in the know, here is how you include an external ActionScript file:</p>

<p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">#include</span> <span style="color: #ff0000;">&quot;filepath/actionscript.as&quot;</span></div></li></ol></pre>
</p>
<p>The downside of using external AS and changing it is that you have to publish every file that includes that script file. This gets to be a little tedious when doing it file by file, so I made a flash extension that will publish all files in a folder.</p>

<p>To use the extension, install the mxp, then restart flash. Under the commands menu, there will be a command called &#8220;BatchPublish&#8221;. <s>Run it, then select the folder that contains your flash files that need to be published. This version does not publish files in sub-folders, but I am working on that for the next version, which will also include a GUI.</s></p>

<p><b>Update 5-16-07:</b> I made version 2 finally. Now it has a GUI and some better options for output. You can publish all FLAs in a directory (and all subdirectories within) to the folder of choice. Force Swf only, and save and compact on close.</p>

<p>
<img src="http://codeblog.studiokoi.com/images/uploads/batchPublish.jpg" alt="Batch Publish Preview" />
</p>
<p>
<a href="http://codeblog.studiokoi.com/images/uploads/batchpublish.zip" class="download"></a>
</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Playing Flash FLVs without the Flash Player</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/playing_flash_flvs_without_the_flash_player/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.13</id>
      <published>2007-04-22T22:12:01Z</published>
      <updated>2008-04-06T18:20:19Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>Due to the rise in internet video, and the need for it to be easily played on different operatiing systems and browers, Flash FLVs have taken off in the last two years. In light of this phenomenon, I find myself more and more downloading FLV videos for later viewng.</p>

<p>The most common places you will see FLV video for the masses is of course <a href="http://youtube.com" title="YouTube">YouTube</a> and <a href="http://video.google.com" title="Google Video">Google Video</a>, but many more sites are catching on. Flash has 98% browser penetration, and works on Mac, PC, and Linux, so it is a no brainer.</p>

<p>So what do you do with all those downloaded FLVs? You could open Flash and make a quick SWF file to play it, but that would be annoying and wouldn't work at all for people without Flash Professional installed. The direct problem with FLVs is that they dont play in Windows Media Player or Linux's Totem player. If you are on a Mac, you can install <a href="http://perian.org/" title="Perian | The swiss army knife for QuickTime">Perian</a> and Quicktime will play the FLVs You could convert them to normal video files with some free video conversion programs such as <a href="http://mediacoder.sourceforge.net/" title="Media Coder | Open Source Win32 Transcoder">Media Coder</a> for Windows or <a href="http://ffmpegx.com" title="FFMpegX Mac Video Transcoder">FFMpegX</a> for Mac. You could also use a free online converter such as <a href="http://media-convert.com/" title="Media Convert | Online Video and Image Converter">Media Convert</a>.</p>

<p>However, all that could add up to time wasted. The solution I found to be the most pleasing and easy was to download the free <a href="http://www.videolan.org/vlc/" title="VLC Media Player | Cross Platform, All Inclusive Video Player">VLC Media Player</a> for Windows/Mac/Linux/Unix. VLC plays FLV files natively and vry clearly. The controlls are quite plain but intuitive, and it supports playlists for viewing multiple FLVs. Best of all, it also plays pretty much any other file format thrown at it. I have used it as my primary video player on Mac and PC for 2 years and have been very satisfied. I don't even open Windows Media Player anymore. VLC plays all Windows Media files out of the box. Plus, if you are on a heavily regulated network at work, or do not have install privelages, there is a <a href="http://portableapps.com/apps/music_video/vlc_portable" title="PortableApps.com | Portable VLC Media Player for Windows">no-install thumbdrive version of VLC</a> with all the same capabilities as the full install version!</p>

 
      ]]></content>
    </entry>

    <entry>
      <title>Disable &#8220;Click&#45;Through&#8221; on MovieClips in Flash</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/disable_click_through_on_movieclips_in_flash/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.12</id>
      <published>2007-04-22T06:48:00Z</published>
      <updated>2007-05-21T15:14:45Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>At work we use a lot of fake pop-ups in Flash to show information. Everything generally works very well, until we discovered that you can click buttons in flash that are underneath a movieclip or a graphic.</p>

<p>The first solution was to hard code disabling of MovieClips/buttons like so:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">button1_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     popUp_mc.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     button1._btn.<span style="color: #0066CC;">enabled</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     button2._btn.<span style="color: #0066CC;">enabled</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">popUpClose_btn.<span style="color: #0066CC;">onPress</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     popUp_mc.<span style="color: #0066CC;">_visible</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     button1._btn.<span style="color: #0066CC;">enabled</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">     button2._btn.<span style="color: #0066CC;">enabled</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>As you can see, this is VERY messy and impractical, as every time you add another button, you have to edit the code for the pop-up buttons to enable/disable it when shown.</p>

<p>One thing we did know up front is that if you put two buttons on top of one another, the one with the highest depth will over take the one below it. Therefore only the top one get's clicked. (The same is not true for rollovers, both items recieve a rollover event, but not click.)</p>

<p>So, we tried using a giant button below the background of the fake pop-up movieclip. It worked in the fact that we could not click buttons below the MovieClip. What was not cool was the hand cursor that showed up over the entire MovieClip because we had a button in the background. Fortunatly, we discovered a built in ActionScript function that disables the hand cursor:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hiddenButton_btn.<span style="color: #0066CC;">useHandCursor</span> = <span style="color: #000000; font-weight: bold;">false</span>;</div></li></ol></pre>

<p>It solved our problem completely! Now we could have pop-ups and not be able to click buttons underneath them while they were open.</p>

<p>Here is an example file.<br/><br/><a href="http://codeblog.studiokoi.com/images/uploads/clickThroughDisable.zip" class="download"></a></p> 
      ]]></content>
    </entry>

    <entry>
      <title>Using The Tag Limit Plug&#45;in with ExpressionEngine</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/using_the_tag_limit_plugin_with_expressionengine/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.11</id>
      <published>2007-04-22T05:33:01Z</published>
      <updated>2007-04-22T05:11:56Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="ExpressionEngine"
        scheme="http://codeblog.studiokoi.com/share/site/category/ExpressionEngine/"
        label="ExpressionEngine" />
      <content type="html"><![CDATA[
        <p>Upon redesigning this website, I decided I wanted to do a little more with the front page. The first thing that I wanted to accomplish besides a new look was removing entire posts from the front page and just having short snippets of each post so that there would be less scrolling and a reason to view the entry/comments page.</p>

<p><a href="http://expressionengine.com" title="Ellis Lab's ExpressionEngine Content Management System">ExpressionEngine</a> has a built in word limiter function that can be used on template tags that produce words. My immediate problem with using the word limiter is that it would cut off paragraphs without closing the paragraph tag. I also show a lot of code within pre tags and those not being closed screwed up the entire page layout.</p>

<p>In my search for a solution that would always work, I came across <a href="http://expressionengine.com/forums/member/31739/" title="the_butcher">the_butcher</a>'s <a href="http://expressionengine.com/forums/viewthread/34079/" title="Tag Limit Plug-in for ExpressionEngine">Tag Limit Plug-in for ExpressionEngine</a>. It is a rather brilliant plug-in that lets you wrap a content output with tags that will limit the amount of a certain tag that will be renderd. Like so:</p>

<pre class="php"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span><a href="http://www.php.net/exp"><span style="color: #000066;">exp</span></a>:tag_limit tag=<span style="color: #ff0000;">&quot;p&quot;</span> <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;foo&quot;</span> num=<span style="color: #ff0000;">&quot;2&quot;</span> offset=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This instance will only show 2 tags with the class "foo" and will start with the third one as the offset is set to two. My only grip so far is that you cannot use autoformatting in your post menu when you make posts. You have to enter all html tags yourself, otherwise the extention renders nothing. My guess is that the extention is run before the tags are automatically placed, but I don't know for sure. The simple solution was for me to just enter the paragraph and break tags myself when posting, which isn't a big deal, just a pain in the butt.</p>

<p>All in all, I love the plug-in, and what I was able to accomplish with it on the site. I could have used the summary tag to do what this does, and only showed summaries on the front page and full articles at the link, but I like this idea better. It lets me post a little faster not having to come up with a summary as well as the content</p> 
      ]]></content>
    </entry>

    <entry>
      <title>A &#8220;Simple&#8221; Tester for Flash Tweens</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/a_simple_tester_for_flash_tweens/" />
      <id>tag:codeblog.studiokoi.com,2007:share/code/index/1.6</id>
      <published>2007-02-03T23:21:00Z</published>
      <updated>2007-05-21T15:11:22Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>Recently, I built a flash website for a customer that required a lot of small, smooth tweens. I have used ActionScript tweening before, but not enough that I knew all of it from memory, so I went about my business experimenting with them. I realized that this situation might come up again, so rather than creating a dummy file every single time, I made a small tween tester app that would give me the code and remind me of how to use it.</p>

<p>I included the full instructions from the flash help file as well as some on how to use the tester. This way, i can simply see how a tween/transition is going to look without even opening flash. It has been a real time saver already.</p>

<p>I will say up front that it is by no means flawless, so I stuck it as an Alpha release. Let me know what you think.</p>

<p><script type="text/javascript">displayFlash("http://codeblog.studiokoi.com/images/uploads/as_tween",500,400);</script></p>

<p><a href="http://codeblog.studiokoi.com/images/uploads/as_tween.fla" class="download"></a></p> 
      ]]></content>
    </entry>

    <entry>
      <title>Exported audio won&#8217;t load from a loaded movie in Flash 8.</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/exported_audio_wont_load_from_a_loaded_movie_in_flash_8/" />
      <id>tag:codeblog.studiokoi.com,2006:share/code/index/1.5</id>
      <published>2006-12-23T01:48:00Z</published>
      <updated>2007-04-22T05:11:20Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>Today, I was working on a flash website for a client when I ran into an interesting problem: My Action-Scripted sounds weren't playing from a internally loaded swf.</p>

<p>The setup was simple:<br/>
Using Flash 8, I made a main movie that loads the interface, and when another section is clicked it loads within the main swf. I was also using the same script that I usually do for sound, which always seems to work (and is subsequently the one Adobe/Macromedia recommends).</p>

<p>Like so:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> buttonSound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	blip = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	blip.<span style="color: #0066CC;">attachSound</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;blipHover&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	blip.<span style="color: #0066CC;">setVolume</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	blip.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>My script was right, and my linkage was right:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/linkage.gif" style="border:thin solid #000;" alt="image" width="450" height="230" /></p>

<p>It was a total mystery to me, so as usuall, I chose not to google the problem, and I experimented. Oddly on the first try I discovered that if you are calling an exported sound in a swf loaded withen a swf, the sound needs to be exported in the main swf and not the loaded swf.</p>

<p>Weird stuff.</p> 
      ]]></content>
    </entry>

    <entry>
      <title>CSS Backgrounds on the body and html tags</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/css_backgrounds_on_the_body_and_html_tags/" />
      <id>tag:codeblog.studiokoi.com,2006:share/code/index/1.4</id>
      <published>2006-12-21T18:28:03Z</published>
      <updated>2007-04-22T05:10:18Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="CSS"
        scheme="http://codeblog.studiokoi.com/share/site/category/CSS/"
        label="CSS" />
      <content type="html"><![CDATA[
        <p>Today I decided to update the html of <a href="http://studiokoi.com" title="Studio Koi Web Design">my main page</a> to appear more like the code blog.</p>
<p>Mainly it was adding the background gradient to the top and some new JavaScript. The background CSS was a very simple effect using the html tag AND the body tag for displaying background images.</p>

<pre class="css"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">html <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-image</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/back.gif&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-repeat</span>: <span style="color: #993333;">repeat</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-color</span>: #<span style="color: #cc66cc;">666</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">body <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">margin</span>:<span style="color: #cc66cc;">0</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">padding</span>:<span style="color: #cc66cc;">0</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-image</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/back_gradient.png&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-repeat</span>:<span style="color: #993333;">repeat-x</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">text-align</span>:<span style="color: #993333;">center</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>I of course assumed it would go very quickly and I just change, upload, and go. I should have known better.</p>

<p>When I was editing the html page, I noticed that I didn't have a <a href="http://www.w3.org/QA/Tips/Doctype" title="w3.org Doctypes">doctype</a>. Being forced to use IE6 all day by the army, I had forgotten what <a href="http://getfirefox.com" title="Get the Firefox Web Browser!">good browsers</a> do when a proper doctype is added. It broke my always-in-the-middle table hack.</p>

<p>There was a simple JavaScript fix I had planed on using anyway, so I decided to implement it. In order for it to work correctly I had to make the div container for the content absolutely positioned. When I did so, the top gradient for the body disappeared. I thought at first that it was a server side issue until I tried it locally with the same results.</p>

<p>I spent more than two hours looking with <a href="http://ee.koruproductions.com" title="Koru Productions' Expression Engine Blog">a friend</a> for the problem. It didn't come to me until after I implemented his suggestion of running a vanilla page with just the CSS.</p>

<p>When an object is absolutely positioned, it doesn't push is parent container. As it was the only thing on the page, and the body counts as a container, there was nothing pushing the body open so that the background could be seen.</p>

<p>The simple solution is adding a height value to the body tag that is as high or higher than the background image.</p>

<pre class="css"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">body <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">height</span>:400px;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">margin</span>:<span style="color: #cc66cc;">0</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">padding</span>:<span style="color: #cc66cc;">0</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-image</span>: <span style="color: #993333;">url</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/back_gradient.png&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">background-repeat</span>:<span style="color: #993333;">repeat-x</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">text-align</span>:<span style="color: #993333;">center</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>The good thing is that it also <a href="http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fstudiokoi.com%2Fmain.css&warning=1&profile=css21&usermedium=all" title="W3C CSS Validator">validates!</a> Enjoy.</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Actionscript function on FLV finish without quepoints</title>
      <link rel="alternate" type="text/html" href="http://codeblog.studiokoi.com/share/site/actionscript_function_on_flv_finish_without_quepoints/" />
      <id>tag:codeblog.studiokoi.com,2006:share/code/index/1.3</id>
      <published>2006-12-19T20:45:00Z</published>
      <updated>2007-04-22T18:57:01Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>info@studiokoi.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://codeblog.studiokoi.com/share/site/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>At work, a colleague and I were converting a huge Director MX2004 CD lesson to a Flash 8 lesson for web delivery.</p>
<p>Within the Director mx 2004 lesson, there were a TON of Quicktime movies that played within the lesson. We batch changed all these into FLVs when we ran into a problem. We needed the flash time-line to advance when the video was finished.</p>
<p>With Flash MX2004 and Flash 8, you can script play() and stop() functions with just the instance name of the FLV container, however, since an FLV is streaming video, and not a swf file, a method like:</p>
<br />
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>myVideo.<span style="color: #0066CC;">_currentframe</span> == myVideo.<span style="color: #0066CC;">_totalframes</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//function()</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>
<br />
 <p>will not work.</p>
<p>With Flash 8, cue points were introduced, allowing you to set points within the FLV that you can set listeners for and do functions on the event of those cue points. This is mostly what we needed, except that would require reimporting 80 videos and adding a cue point at the end of each video. We really didn&#8217;t have time to do that.</p>
<p>With a little help from the built in flash help, we discovered that if you import the mx.video class,  there is already an event built in called &#8220;complete&#8221;.</p>
<p>So with this, we were able to come up with a generic listener that would run a function upon the completion of a video loaded into the component FLV player.</p>
<br />
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #0066CC;">video</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> the_end<span style="color: #66cc66;">&#40;</span>eventObject:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//end of video functions</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myVideo.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;complete&quot;</span>, the_end<span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>
<br />
<p>Enjoy!
</p> 
      ]]></content>
    </entry>


</feed>