<?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>Patrick Gunderson &#187; Code</title>
	<atom:link href="http://pat.theorigin.net/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://pat.theorigin.net</link>
	<description>Art, Design, Code</description>
	<lastBuildDate>Mon, 03 May 2010 19:02:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hey, I made a video!</title>
		<link>http://pat.theorigin.net/2010/05/03/hey-i-made-a-video/</link>
		<comments>http://pat.theorigin.net/2010/05/03/hey-i-made-a-video/#comments</comments>
		<pubDate>Mon, 03 May 2010 19:02:50 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Art]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=348</guid>
		<description><![CDATA[
Shempi Vis from Patrick Gunderson on Vimeo.
This is a demo of a music visualization I made in flash. This was recorded in real-time.
I love using music for data vis because the spectrum contains copious amounts of organized, yet seemingly random data.
The music is Shempi by Ratatat who have an album coming in June http://www.ratatatusic.com
]]></description>
			<content:encoded><![CDATA[<p><object width="700" height="394"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=11402633&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=6699ff&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=11402633&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=6699ff&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="700" height="394"></embed></object>
<p><a href="http://vimeo.com/11402633">Shempi Vis</a> from <a href="http://vimeo.com/user3715396">Patrick Gunderson</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>This is a demo of a music visualization I made in flash. This was recorded in real-time.</p>
<p>I love using music for data vis because the spectrum contains copious amounts of organized, yet seemingly random data.</p>
<p>The music is Shempi by Ratatat who have an album coming in June http://www.ratatatusic.com</p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2010/05/03/hey-i-made-a-video/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creep (in motion)</title>
		<link>http://pat.theorigin.net/2010/05/03/creep-in-motion/</link>
		<comments>http://pat.theorigin.net/2010/05/03/creep-in-motion/#comments</comments>
		<pubDate>Mon, 03 May 2010 18:58:17 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Art]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=349</guid>
		<description><![CDATA[
creep from Patrick Gunderson on Vimeo.
Here&#8217;s something new: A video of a flash program performing audio spectrum analysis. This was recorded in real-time.
The music is Scala &#038; Kolacney Brother&#8217;s &#8220;Creep&#8221;. 
]]></description>
			<content:encoded><![CDATA[<p><object width="700" height="700"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=11372644&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=6699ff&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=11372644&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=6699ff&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="700" height="700"></embed></object>
<p><a href="http://vimeo.com/11372644">creep</a> from <a href="http://vimeo.com/user3715396">Patrick Gunderson</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Here&#8217;s something new: A video of a flash program performing audio spectrum analysis. This was recorded in real-time.</p>
<p>The music is Scala &#038; Kolacney Brother&#8217;s &#8220;Creep&#8221;. </p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2010/05/03/creep-in-motion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Composition #68</title>
		<link>http://pat.theorigin.net/2009/01/23/composition-68/</link>
		<comments>http://pat.theorigin.net/2009/01/23/composition-68/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 17:02:34 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Art]]></category>
		<category><![CDATA[Painting]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[blue]]></category>
		<category><![CDATA[pink]]></category>
		<category><![CDATA[purple]]></category>
		<category><![CDATA[red]]></category>
		<category><![CDATA[RGY]]></category>
		<category><![CDATA[rings]]></category>
		<category><![CDATA[yellow]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=221</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><img src="http://pat.theorigin.net/wp-content/uploads/2009/01/68-700x700.jpg" alt="68" title="68" width="700" height="700" class="alignnone size-medium wp-image-220" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2009/01/23/composition-68/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript 3.0 and clickTag</title>
		<link>http://pat.theorigin.net/2008/10/28/actionscript-30-and-clicktag/</link>
		<comments>http://pat.theorigin.net/2008/10/28/actionscript-30-and-clicktag/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 09:25:32 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=111</guid>
		<description><![CDATA[I just discovered that in order to use a Mouse Event to initialize a navigateToURL() call, the HTML tags on your movie must have AllowScriptAccess set to always.
If AllowScriptAccess is set to never or sameDomain, the call will fail.
]]></description>
			<content:encoded><![CDATA[<p>I just discovered that in order to use a Mouse Event to initialize a navigateToURL() call, the HTML tags on your movie must have AllowScriptAccess set to always.</p>
<p>If AllowScriptAccess is set to never or sameDomain, the call will fail.</p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/actionscript-30-and-clicktag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Two Methods to Make a Countdown Timer</title>
		<link>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/</link>
		<comments>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 08:44:44 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Counting]]></category>
		<category><![CDATA[Timer]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=70</guid>
		<description><![CDATA[Setting up a countdown timer can be helpful in a number of situations, particularly in situations that have a time limit or deadline. Here are two methods for creating and stopping countdown timers. To set the scene, place a dynamic textfield on stage and have it listen for the variable _root.timeLeft . Next draw a [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up a countdown timer can be helpful in a number of situations, particularly in situations that have a time limit or deadline. Here are two methods for creating and stopping countdown timers. To set the scene, place a dynamic textfield on stage and have it listen for the variable _root.timeLeft . Next draw a simple box stage, select it and convert it to a MovieClip by pressing the f8 key. Give your movieclip the name stopButton. Remember to also give your instance the name stopButton by clicking back to the Stage, selecting your box and naming it in the properties palette.</p>
<h3>Counting Frames Method</h3>
<p>First let&#8217;s go over the more basic, but ultimately more complex method. Since  movieclips can execute commands every frame using an onEnterFrame event handler (a specially named function) if we know the framerate, we can count the frames and divide to find the number of seconds past.</p>
<p>Declare a new variable timeLeft and give it a value of 10 (our counter will count down from 10). Create a new Movieclip called tenSecondTimer and assign it properties for frameRate, and counter. We&#8217;ll use the counter to count how many frames have passed.</p>
<pre class="code">var timeLeft:Number = 10;
createEmptyMovieClip("tenSecondTimer", getNextHighestDepth());
tenSecondTimer.frameRate = 24; // this movie runs at 24 fps
tensecondTimer.counter = 0;</pre>
<p>Now create an onEnterFrame event handler, which will be executed with every frame, that increases the counter, compares it to the framerate, and if they match (a second has passed) reduce the timeLeft by one. for this function we&#8217;ll use the modulo operator (%), the increment operator(++) and the decrement operator (&#8211;). The modulo operator (%divides two numbers and returns the remainder. if the remainder is 0 then the first number is evenly divisible by the second. The increment operator (++) increases the value by one. x++ is the same as x = x + 1. The decrement operator (&#8211;) reduces the value by 1. x&#8211; is the same as x = x &#8211; 1.</p>
<pre class="code">tenSecondTimer.onEnterFrame = funtion(){
    this.counter++;
    if (this.counter % this.frameRate == 0){
        _root.timeLeft--;
    }
}</pre>
<p>In addition to decreasing the timeLeft after every second, we want it to do something when the time is up. Then remove the timer.</p>
<pre class="code">tenSecondTimer.onEnterFrame = funtion(){
    this.counter++;
    if (this.counter % this.frameRate == 0){
        _root.timeLeft--;
    }
    if (timeLeft &lt;= 0){
        trace("TIME'S UP");
        this.removeMovieClip();
    }
}</pre>
<p>We also have the button which we want to stop the clock, so we can define an onRelease event handler for it.</p>
<pre class="code">stopButton.onRelease = funtion(){
    trace("TIMER STOPPED");
    _root.tenSecondTimer.removeMovieClip();
}</pre>
<h3>setInterval Method</h3>
<p>The second method for creating a countdown timer uses the setInterval command. setInterval creates a timer that will execute a function after a certain number of milliseconds. Otherwise the method is similar. This simplifies things a bit because now we don&#8217;t have to count frames to determine if a second has passed, flash will do that for us.</p>
<p>Setup the stage the same way as above, with the textfield and the stopButton.</p>
<p>Declare timeLeft, but this time we don&#8217;t need a counter or framerate, so all se need is to make a setInterval.</p>
<pre class="code">var timeLeft:Number = 10;

// execute the function countdown every 1000 miliseconds (1 second);
tenSecondTimer = setInterval(countDown, 1000);</pre>
<p>now we need to define a function CountDown to decrease the limeLeft every second, and to do something once timeleft is 0.</p>
<pre class="code">function countDown(){
    timeLeft--;
    if (timeLeft &lt;= 0){
        trace("TIME'S UP");
        clearInterval(tenSecondTimer);
    }
}</pre>
<p>Be sure to clear the interval when time is up and when the stop button is clicked or else you will get &#8220;TIME&#8217;s UP&#8221; messages every second for the rest of your movie!</p>
<pre class="code">stopButton.onRelease = funtion(){
    trace("TIMER STOPPED");
    clearInterval(_root.tenSecondTimer);
}</pre>
<p>There you have it. Two different methods to display a countdown timer and to stop the countdown with a stop button.</p>
<p>About the author: Patrick Gunderson is a web developer and designer at TWBA\Tequila in Los Angeles.</p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: Drag and throw an object on stage</title>
		<link>http://pat.theorigin.net/2008/10/28/tutorial-drag-and-throw-an-object-on-stage/</link>
		<comments>http://pat.theorigin.net/2008/10/28/tutorial-drag-and-throw-an-object-on-stage/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 08:33:08 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[AS2]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=64</guid>
		<description><![CDATA[In this tutorial we will build a movie that contains a simple movieclip that allows the user to click and drag the clip, and when released exhibits some inertia and continues to move and bounce off walls like an air hockey puck.
[kml_flashembed movie="http://www.theorigin.net/tutorialFiles/HockeyPuck/hockeypuck.swf" height="400" width="550" /]
To setup the stage, create a movieclip that contains a [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we will build a movie that contains a simple movieclip that allows the user to click and drag the clip, and when released exhibits some inertia and continues to move and bounce off walls like an air hockey puck.</p>
<p align="center">[kml_flashembed movie="http://www.theorigin.net/tutorialFiles/HockeyPuck/hockeypuck.swf" height="400" width="550" /]</p>
<p>To setup the stage, create a movieclip that contains a square aligned at 0,0. Place an instance on the main stage and give it a name of &#8220;puck&#8221;.</p>
<p>First we need to enable a click and drag on the puck. To do this we&#8217;ll use the onPress Event handler to create an onEnterFrame function that keeps the puck aligned with our mouse.</p>
<pre class="code">puck.onPress = function(){
	this.onEnterFrame = function(){
        this._x = _root._xmouse;
        this._y = _root._ymouse;
    }
}</pre>
<p>This script is OK, but rather than smoothly picking up the puck where the mouse touched it, the puck snaps its top corner to our mouse. To alleviate this we need to subtract the mouse position relative to the puck from the mouse position relative to the stage by setting an anchor when the mouse is pressed. We won&#8217;t worry about garbage collecting this onEnterFrame because we are going to overwrite it when the mouse releases.</p>
<p>Note: in general you shouldn&#8217;t just leave open onEnterFrame functions running. When you get a number of onEnterFrame functions going at the same time the performance of your movie can suffer greatly or even crash. For this reason you should terminate your onEnterFrame functions when they have finished their purpose. This is called garbage collection.</p>
<pre class="code">puck.onPress = function(){
     anchorX = this._xmouse;
	anchorY = this._ymouse;
	this.onEnterFrame = function(){
        this._x = _root._xmouse - anchorX;
        this._y = _root._ymouse - anchorY;
    }
}</pre>
<p>Now we need a way to determine which way the mouse was moving when it released the puck. For this we can record _xmouse and _ymouse values every frame that the mouse is down and store them for use in the onEnterFrame we are going to create onRelease.</p>
<pre class="code">puck.onPress = function(){
    anchorX = this._xmouse;
	anchorY = this._ymouse;
	this.onEnterFrame = function(){
        lastXMouse = _root._xmouse;
        lastYMouse = _root._ymouse;

        this._x = _root._xmouse - anchorX;
        this._y = _root._ymouse - anchorY;
    }
}</pre>
<p>To make the calculation as to which direction to send the puck after release, we only need the mouse position one frame back, but if we were to record more frames, we could get an average direction just prior to release or even find a curve for the puck to follow. Here though we&#8217;ll just send the puck in a straight line.</p>
<p>The math to determine which direction the mouse was moving is a simple slope equation using two points (our current and previous mouse points) We&#8217;ll define this function for onRelease and onReleaseOutside just in case your mouse is a bit quicker than your movie.</p>
<pre class="code">puck.onRelease = puck.onReleaseOutside = function(){
    xDirection = (_root._xmouse - lastXMouse);
    yDirection  = (_root._yMouse - lastYMouse);
}</pre>
<p>We use the direction data to create a new movieclip that moves the puck around.</p>
<pre class="code">puck.onRelease = puck.onReleaseOutside = function(){
    xDirection = (_root._xmouse - lastXMouse);
    yDirection  = (_root._yMouse - lastYMouse);
    this.onEnterFrame = function(){
	    this._x += xDirection;
	    this._y += yDirection;
    }
}</pre>
<p>In order to make the puck bounce off the walls we need to check to see whether or not it has reached the edge of the stage, and have it reverse direction if it has.</p>
<pre class="code">puck.onRelease = puck.onReleaseOutside = function(){
    xDirection = (_root._xmouse - lastXMouse);
    yDirection  = (_root._yMouse - lastYMouse);
    this.onEnterFrame = function(){

	// Bounce off the Walls
	if (this._x &lt; 0){
			xDirection *= -1;
			this._x = 0;
		}
		if (this._x + this._width &gt; Stage.width){
			xDirection *= -1;
			this._x = Stage.width - this._width;
		}
		if (this._y &lt; 0){
			yDirection *= -1;
			this._y = 0;
		}

		if (this._y + this._height &gt; Stage.height){
			yDirection *= -1;
			this._y = Stage.height - this._height;
		}

        // Set New Position
        this._x += xDirection;
	    this._y += yDirection;
     }
}</pre>
<p>Finally we want the puck to slow down and eventually stop over time so we&#8217;ll add a decay function to the x and Y directions that reduce them by 10% every frame.</p>
<p>Additionally once our puck has stopped, we need to terminate and garbage collect the onEnterFrame function.</p>
<pre class="code">puck.onRelease = puck.onReleaseOutside = function(){
    xDirection = (_root._xmouse - lastXMouse);
    yDirection  = (_root._yMouse - lastYMouse);
	this.onEnterFrame = function(){

		// decay x
		if (Math.abs(xDirection) &gt; 0.5){
			xDirection *= .9;
		} else {
			xDirection = 0;
		}

		// decay y
		if (Math.abs(yDirection) &gt; 0.5){
			yDirection *= .9;
		} else {
			yDirection = 0;
		}

		//Garbage Collection
		if (xDirection == 0 &amp;&amp; yDirection==0){
			this.onEnterFrame = null;
		}

		//Bounce off the walls
		if (this._x &lt; 0){
			xDirection *= -1;
			this._x = 0;
		}
		if (this._x + this._width &gt; Stage.width){
			xDirection *= -1;
			this._x = Stage.width - this._width;
		}
		if (this._y &lt; 0){
			yDirection *= -1;
			this._y = 0;
		}

		if (this._y + this._height &gt; Stage.height){
			yDirection *= -1;
			this._y = Stage.height - this._height;
		}

		//Position
		this._x += xDirection;
		this._y += yDirection;

	}
}</pre>
<p align="center"><a href="../tutorialFiles/HockeyPuck/hockeypuck.zip">Download Source</a></p>
<p>Patrick Gunderson is a Flash Designer and Developer at TBWA\Tequila\ in Los Angeles.</p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/tutorial-drag-and-throw-an-object-on-stage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I am here</title>
		<link>http://pat.theorigin.net/2008/10/28/i-am-here/</link>
		<comments>http://pat.theorigin.net/2008/10/28/i-am-here/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 07:14:40 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Art]]></category>
		<category><![CDATA[1680x1050]]></category>
		<category><![CDATA[Abstract]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Wallpaper]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=12</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><a href="http://pat.theorigin.net/wp-content/uploads/2008/10/i_am_here.jpg"><img class="alignnone size-medium wp-image-13" title="I am here" src="http://pat.theorigin.net/wp-content/uploads/2008/10/i_am_here-530x331.jpg" alt="" width="530" height="331" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/i-am-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tokyo Lomo</title>
		<link>http://pat.theorigin.net/2008/10/28/tokyo-lomo/</link>
		<comments>http://pat.theorigin.net/2008/10/28/tokyo-lomo/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 06:25:04 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Art]]></category>
		<category><![CDATA[1680x1050]]></category>
		<category><![CDATA[Abstract]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Wallpaper]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=6</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p><a href="http://pat.theorigin.net/wp-content/uploads/2008/10/tokyo_lomo1.jpg"><img class="alignnone size-medium wp-image-9" title="Tokyo Lomo" src="http://pat.theorigin.net/wp-content/uploads/2008/10/tokyo_lomo1-530x331.jpg" alt="" width="530" height="331" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/tokyo-lomo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
