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

<channel>
	<title>20papercups :: Michael Marner&#039;s Website &#187; www</title>
	<atom:link href="http://www.20papercups.net/tag/www/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.20papercups.net</link>
	<description>The (awesome) website of Michael Marner</description>
	<lastBuildDate>Tue, 15 Nov 2011 01:09:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tutorial: The Age Gauge &amp; the easy way to get data into Flash</title>
		<link>http://www.20papercups.net/tutorials/tutorial-age-gauge/</link>
		<comments>http://www.20papercups.net/tutorials/tutorial-age-gauge/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 11:37:44 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flashvars]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[www]]></category>

		<guid isPermaLink="false">http://www.20papercups.net/?p=203</guid>
		<description><![CDATA[If you work with websites, sooner or later you will find yourself interacting with Flash. While Flash usually gives me the urge to run for the hills, sometimes it really is the correct tool for the job. If you do have to work with Flash, chances are you will need to get data into it. [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_205" class="wp-caption alignleft" style="width: 184px"><img class="size-full wp-image-205" title="Age Gauge" src="http://www.20papercups.net/wp-content/uploads/2009/06/Screenshot-Come-Out-2009-Mozilla-Firefox.png" alt="Age Gauge on Comeout.on.net" width="174" height="59" /><p class="wp-caption-text">Age Gauge on Comeout.on.net</p></div>
<p>If you work with websites, sooner or later you will find yourself interacting with Flash. While Flash usually gives me the urge to run for the hills, sometimes it really is the correct tool for the job. If you do have to work with Flash, chances are you will need to get data into it. This post documents how I created the Age Gauge for the <a href="http://comeout.on.net" target="_blank">Come Out Festival</a>&#8216;s 2009 website.<span id="more-203"></span></p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-9376349336558898";
google_ad_slot = "0815367707";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>Before we begin, a disclaimer: I hate working with Flash. If you are in a position where you are about to do something in Flash, the correct approach is to first make sure there is no other way to do what you want. Since I primarily work on Linux, I especially hate Flash. However, sometimes it is the best way to solve a problem.</p>
<p>Normally, if you&#8217;re using Flash for anything other than simple animation, you will need a way to pass data from your website/database/server to the Flash file. There are many ways to go about this. The Web 2.0 guys will tell you that once the Flash animation has started, it should retrieve an XML file from the server, using Flash&#8217;s built in XML parsing functionality to extract the data. A slightly more old school variation on this approach is to create a server side script that creates output looking just like URL encoded variables ($_GET in PHP). The Flash file loads the URL and the variables are automatically available as global variables. However, sometimes you just need to keep things simple. But first, some background&#8230;</p>
<h3>What is this Age Gauge anyway?</h3>
<p>Come Out is an arts festival for young people. The festival organisers needed the new website to show the target ages for all of the shows, so that 17 year old year 12s didn&#8217;t end up at a show aimed at kindergarten kids. The guys at <a href="http://www.loudwhisper.com.au" target="_blank">Loud Whisper</a> being all awesome and designy, came up with what you see above. They gave this to me and said &#8220;make it work&#8221;.</p>
<p>Now I was left with a predicament, how would I make it work? There are 171 possible combinations of ages, so making some static images is probably not the best way to go.</p>
<p>My immediate reaction was to look at the <a href="http://au.php.net/manual/en/book.image.php" target="_blank">GD image library</a> for PHP and see if I could make something work. I have made bar graphs using GD before. However, the big problem here was that it had to look like the design. The round ends were are a particular problem here. Next I thought about Javascript. To be honest, with enough CSS trickery you could probably make it work. However, I am not quite Web 2.0 enough, and getting it to be cross-browser compatible would be a bit of a pain.</p>
<p>This leaves one extra option: Implement the thing in Flash.</p>
<h3>Setting up the FLA file</h3>
<p>The fact that I don&#8217;t like Flash actually worked in my favour. I wanted to get this over and done with as quickly as possible. So, I have ignored pretty much all the cool ActionScript 3 features, and using external .as files for scripts. For something this quick and simple, it just isn&#8217;t needed.</p>
<p>So we end up with something that looks like what you see below:</p>
<div id="attachment_206" class="wp-caption aligncenter" style="width: 550px"><img class="size-full wp-image-206" title="Age Gauge Layers" src="http://www.20papercups.net/wp-content/uploads/2009/06/flash.png" alt="The Age Gauge in Flash" width="540" height="431" /><p class="wp-caption-text">The Age Gauge in Flash</p></div>
<p style="text-align: left;">All of the layers can be ignored except for <em>red</em>. Red contains the red bar that moves and changes size. Because we need to change the red bar using ActionScript, we&#8217;ve given the red movie clip an instance name called <em>slider</em>. Where the slider is actually positioned doesn&#8217;t matter too much, because we&#8217;re going to be moving it once the movie loads anyway!</p>
<h3 style="text-align: left;">ActionScript me baby!</h3>
<p>Before we can do anything, we need to figure out the correct position for the slider, and the correct increment for a single year. The way I did this was to simply move the slider using the arrow keys on the keyboard until I had something that looked right. We then put these into script:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">var startAge<span style="color: #008080;">;</span>
var endAge<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/**
 * This is the smallest value for the start age
 */</span>
var MIN_POSITION <span style="color: #000080;">=</span> <span style="color:#800080;">2.9</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/**
 * The width of a single year.
 * So, if there were 2 years between the start and end age,
 * The width of the rectangle would be step*2
 */</span>
var STEP <span style="color: #000080;">=</span> <span style="color:#800080;">9.29</span></pre></div></div>

<p>So, with an age of 0, the slider should have it&#8217;s position.x at 2.9. For every year, we had 9.29 onto this position. Sounds easy so far right? Next we have to actually do the moving and resizing based on the starting and ending age:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// set the position of the slider, but first</span>
<span style="color: #666666;">// we don't want things to break with a negative age</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>startAge <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
 startAge <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
 slider._x <span style="color: #000080;">=</span> MIN_POSITION<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
 <span style="color: #666666;">// here we take the position, and add the step value multiplied by the start age</span>
 slider._x <span style="color: #000080;">=</span> MIN_POSITION <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>startAge <span style="color: #000040;">*</span> STEP<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// now we just set the width to be however many years are between the start and end ages</span>
slider._width <span style="color: #000080;">=</span> STEP<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>endAge<span style="color: #000040;">-</span>startAge<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>That is all of the code to make this work. We don&#8217;t even need to be too precise with the starting position and step, because the mask hides it if we are slightly off. This is one of the benefits of using Flash, and made the Age Gauge very easy to implement.</p>
<h3>Getting the data into Flash</h3>
<p>The next step is to actually make this work. The script above uses uninitialised variables. This will cause things to break, which would be bad. We need to get data into the swf file so that startAge and endAge have values. As I mentioned way back at the start, there are a few ways this can be done. The one I didn&#8217;t mention is to simply use flashvars in the HTML of the page. This gives the Flash file data without having to bother with opening remote URLs for use with XML parsing or loadVars(). <strong>This is not the best way for all problems!</strong> However, all we are trying to do here is get two numbers in, so what&#8217;s the big deal?</p>
<p>Creating the HTML is pretty straight forward. I should mention though that I use swfobject for loading Flash through Javascript. It gets around some issues, such as IE7 not playing Flash on page load. Anyway, the HTML looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="js" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot;&gt;
swfobject.embedSWF(&quot;AgeGauge.swf&quot;, 
                   &quot;AgeGauge&quot;,
                   &quot;168&quot;, 
                   &quot;61&quot;, 
                   &quot;9.0.0&quot;,
                   false,
		   {startAge: &quot;6&quot;, endAge: &quot;13&quot;}
                  );
&lt;/script&gt;</pre></div></div>

<p>The code above uses swf object to load the Age Gauge, passing flashvars to the file when it loads. In this example, we are using a start age of 16, and end age of 13. If you aren&#8217;t using flashvars, then you&#8217;d do something more like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;object id=&quot;AgeGauge&quot; data=&quot;AgeGauge.swf&quot; type=&quot;application/x-shockwave-flash&quot;  height=&quot;61&quot; width=&quot;168&quot;&gt;
   &lt;param value=&quot;startAge=6&amp;amp;amp;endAge=13&quot; name=&quot;flashvars&quot;&gt;
&lt;/object&gt;</pre></div></div>

<p>The point is the same. Really, all swfobject does is generate this HTML for you when the page loads. Note that the flashvars param has the actual startAge and endAge variables formatted like $_GET data, in the same format as loadVars() expects.</p>
<h3>Conclusion</h3>
<p>And that&#8217;s all there is to it really. Remember that using flashvars is a very simple way of getting data from your website into Flash. For more complicated things, parsing an XML file would be a much more flexible way to go. The moral of the story is to use the right tool for the job. Whether we are talking about the choice of Flash, GD, or Javascript, or choosing between the methods of getting data into Flash.  Hope this was at least a little useful!</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-9376349336558898";
google_ad_slot = "0815367707";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.20papercups.net/tutorials/tutorial-age-gauge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

