<?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>Dave Gardner - PHP Developer &#187; tutorial</title>
	<atom:link href="http://www.davegardner.me.uk/blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davegardner.me.uk/blog</link>
	<description>Just behind the bleeding edge of PHP.</description>
	<lastBuildDate>Fri, 02 Jul 2010 08:25:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting started with Flash AS3 from a PHP developer&#8217;s perspective</title>
		<link>http://www.davegardner.me.uk/blog/2010/01/27/getting-started-with-flash-as3-from-a-php-developers-perspective/</link>
		<comments>http://www.davegardner.me.uk/blog/2010/01/27/getting-started-with-flash-as3-from-a-php-developers-perspective/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 21:00:35 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.davegardner.me.uk/blog/?p=57</guid>
		<description><![CDATA[They say that to become a better programmer, you should learn a new programming language. This blog post aims to give an introduction to Flash AS3 from the perspective of an experienced programmer. It covers how to organise code, how to create objects and add them to a scene and how to handle events.]]></description>
			<content:encoded><![CDATA[<p><a href="http://stackoverflow.com/questions/76364/what-is-the-single-most-effective-thing-you-did-to-improve-your-programming-skill/76466#76466" target="blank">They say</a> that to become a better programmer, you should learn a new programming language. Exposure to a new language allows you to step outside of the patterns and workflow you&#8217;re used to and tackle a problem from a different perspective. There are extremes to this, for instance learning <a href="http://www.haskell.org/" target="_blank">Haskell</a> <em>(an advanced purely functional programming language)</em>. I&#8217;ve decided to postpone my learning of Haskell, for now, and instead learn <a href="http://www.adobe.com/products/flash/" target="_blank">Adobe Flash AS3</a>.</p>
<p>This blog post aims to give an introduction to Flash AS3. One good thing about Flash is the large volume of tutorials out there; however this is a double-edged sword. Seperating the wheat from the chaff can be hard work. This post aims to give an introduction to Flash from the perspective of an experienced PHP programmer; someone who already understands the key ideas of OOP. It covers:</p>
<ol>
<li><a href="#organisingcode">Getting started; how to organise code</a></li>
<li><a href="#addingclips">Adding Movie Clips to the scene</a></li>
<li><a href="#eventhandling">Handling events</a></li>
<li><a href="#arrays">The power of arrays</a></li>
<li><a href="#sourcecode">Download source code</a></li>
</ol>
<p>Once you&#8217;ve put it all together, you&#8217;ll have a super-duper rocket-firer (the big white space below is the finished article &#8211; hint <strong>click on the milk bottle bottom left to launch a rocket</strong>)</p>
<div style="border:2px solid #666;width:500px;">
<div id="fireworksflash">
<p>You&#8217;ll need FLASH to see this! Makes sense being that this is a Flash tutorial!</p>
</p></div>
</div>
<p><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script></p>
<p><script type="text/javascript">
    swfobject.embedSWF("http://www.davegardner.me.uk/blog/wp-content/uploads/2010/01/fireworks.swf", "fireworksflash", "500", "600", "9.0.0");
</script></p>
<p>One more thing&#8230; a <strong>big</strong> shout out to other tutorials I&#8217;ve found helpful, especially the <a href="http://www.untoldentertainment.com/blog/tag/as3/" target="_blank">concise and engaging tutorials</a> from <a href="http://www.untoldentertainment.com/" target="_blank">untoldentertainment.com</a>. You can check out <a href="http://delicious.com/davegmpd/as3" target="_blank">all my AS3 links on Delicious</a>.</p>
<h3><a name="organisingcode">How to organise code</a></h3>
<p>As Ryan explains in <a href="http://www.untoldentertainment.com/blog/2009/08/25/tutorial-understanding-classes-in-as3-part-1/" target="_blank">this tutorial</a>, Flash beginners tend to put Actionscript code <em>everywhere</em>.</p>
<blockquote><p>On different frames of the timeline, embedded in nested MovieClips – if i can write code on it, i do”</p></blockquote>
<p>The fast path to Flash Pro is to put all your Actionscript in <strong>seperate files</strong> and break your application down into <strong>objects</strong> (classes). To get started, fire up Flash (I&#8217;m using CS4) and create a new “Flash File (ActionScript 3.0)”. Get the Properties window up (click “Window” &gt; “Properties” if you can&#8217;t see it). It should look like this:</p>
<p><img class="alignnone size-full wp-image-64" title="Document properties" src="http://www.davegardner.me.uk/blog/wp-content/uploads/2010/01/flashblog.jpg" alt="Document properties" width="284" height="438" /></p>
<p>Type “Main” into the Class box. This is the name of the class that will be attached to main movie; the entry-point of the application. Now we can create this class. Click “File” &gt; “New” and choose “ActionScript File”. Paste in the following code:</p>
<pre class="code">package
{
  import flash.display.MovieClip;

  /**
   * Main class - entry point for App
   */
  public class Main extends MovieClip
  {
    /**
     * Constructor
     */
    public function Main()
    {
      trace('It works! - blatantly ripped off from the other tutorial');
    }

    /**
     * Another method
     */
    public function myFirstMethod():void
    {
    }
  }
}</pre>
<p>This is a basic “Main” class. We could create other classes in a similar fashion. However we should <strong>put each class in its own file</strong>. Press CTL-Enter to compile and run; you should find the <strong>OUTPUT</strong> tab shows the “It works” message.</p>
<h4>package</h4>
<p>Namespacing basically; I&#8217;m just leaving this blank for now. Could be <strong>package myExcellentPackage</strong></p>
<h4>import flash.display.MovieClip</h4>
<p>Similar to PHP&#8217;s <strong>require_once</strong>. We need to make sure that everything this class needs is <em>imported</em>. This starts to be a pain when you use various libraries / built-in Flash classes as you need to know <em>what</em> to import. For example, need to change colour? You&#8217;ll need <strong>import flash.geom.ColorTransform</strong>. Easiest way of knowing what to import is simply to Google it.</p>
<h4>public class Main extends MovieClip</h4>
<p>The class declaration.</p>
<h4>trace(&#8217;some text&#8217;);</h4>
<p>The Flash equivalent of <strong>print_r</strong>, ish. Useful for outputting console messages for debugging.</p>
<h4>public function myFirstMethod():void</h4>
<p>Another method declaration. Notice the <strong>:void</strong> bit which defines the return type explictly; I quite like this feature. Another important thing to note is that the constructor <strong>Main</strong> must not have any return type defined.</p>
<h3><a name="addingclips">Adding Movie Clips to the scene</a></h3>
<p>To more further down the Flash Pro road, we need to be able to setup the <em>scene</em> (Flash canvas) programatically. We need to be able to add things and remove things as needed, for example adding a high-score table at the end of a game or a splash screen at the beginning. In this example we&#8217;ll add everthing to the scene using code. It&#8217;s dead simple.</p>
<p>For my rocket firing example, I want to fire the rockets from a milk bottle. We&#8217;ll create this as a <em>Symbol</em>. According to the <a href="http://www.smartwebby.com/Flash/flash_symbols.asp" target="_blank">first thing I found on Google</a> (it <em>must</em> be right), a Symbol is simply a <strong>reusable object</strong>. From a code sense, a Symbol is like a class. We can create copies (instances) of the Symbol and place them in our scene; we can do this from code or from within the Flash GUI. To create the Sybmol, from the FLA, click “Insert” &gt; “New Symbol”. I&#8217;ve added a JPEG picture of a milk bottle on the Symbol.</p>
<p>Now we need to set it up so we can <strong>tie the Symbol to ActionScript</strong>. Get the Symbol properties window up. The easiest way is to find the Symbol from the library, right click on it and click “Properties”. Fill out the details, as shown below:</p>
<p><img class="alignnone size-full wp-image-65" title="Symbol Properties" src="http://www.davegardner.me.uk/blog/wp-content/uploads/2010/01/symbolproperties.jpg" alt="Symbol Properties" width="413" height="541" /></p>
<p>The key thing is the <strong>Class</strong> property. This is the class it will be tied to in ActionScript.</p>
<p>Now we&#8217;ll adjust our <strong>Main</strong> class to get this added to the scene. Adjust Main.as by adding in the property <strong>launchpad</strong> and adding to the constructor:</p>
<pre class="code">  /**
   * Main class - entry point for App
   */
  public class Main extends MovieClip
  {
    /**
     * Milk bottle (launch pad)
     * @var MovieClip
     */
    private var launchPad:MovieClip;

    /**
     * Constructor
     */
    public function Main()
    {
      launchPad = new milkBottle();
      launchPad.x = 50;
      launchPad.y = stage.stageHeight-25;
      launchPad.height = 40;
      launchPad.scaleX = launchPad.scaleY;
      addChild(launchPad);
    }
  }</pre>
<h4>private var launchPad:MovieClip;</h4>
<p>So you&#8217;ll probably recognise this as declaring a private class property. The key things is the <strong>:MovieClip</strong> which defines what <em>type</em> of variable this is. You&#8217;ll see this a lot in Flash.</p>
<h4>launchPad = new milkBottle();</h4>
<p>Create an instance of our <strong>milkBottle</strong> class (Symbol). You&#8217;ll notice that we don&#8217;t need to bother with <strong>this.</strong> (or $this-&gt;) as we would in PHP to access the member property launchPad.</p>
<h4>addChild(launchPad);</h4>
<p>This adds the object to the stage. It&#8217;s a bit like the JavaScript DOM in a lot of ways. This is the simplest way of adding an object to the scene; we can also use other methods like <strong>addChildAt(launchPad, 1)</strong>.</p>
<h4>launchPad.height = 40;</h4>
<p>Resize the object. <strong>launchPad.scaleX = launchPad.scaleY</strong> completes the job &#8211; adjusting the width automatically to make the aspect ratio correct.</p>
<h3><a name="eventhandling">Handling events</a></h3>
<p>Event handling in AS3 will be instantly familiar to anyone who&#8217;s used jQuery, or indeed JavaScript, so long as they&#8217;ve stuck to the idea of seperating content from functionality (no inline event handlers). For the rocket launching app, we need the launchPad object to handle a <strong>mouse click</strong> event and fire off a rocket (click on the milk bottle to fire a rocket!) We can attach an event handler by adding the following line to <strong>Main()</strong>.</p>
<pre class="code">  launchPad.addEventListener(MouseEvent.CLICK, launchRocket);</pre>
<p>To access the event handler functionality, we also have to make sure we <em>import</em> the right libraries.</p>
<pre class="code">  import flash.events.Event;
  import flash.events.MouseEvent;</pre>
<p>Finally, we need a method to handle this event. We&#8217;ll add this to our class:</p>
<pre class="code">  /**
   * Launch a new rocket
   * @param Event e
   */
  public function launchRocket(e:Event):void
  {
    trace('Launch!');
  }</pre>
<h3><a name="arrays">The power of arrays</a></h3>
<p>So we&#8217;ve made a good start. Next up, we want to be able to launch a rocket. We want to launch one whenever the milk bottle is clicked. Some how we need to keep track of all these rockets. This is where an array proves useful. We can add a new class property.</p>
<pre class="code">  /**
   * Rockets
   * @var Array
   */
  private var rockets:Array;</pre>
<p>Within the Main class constructor we&#8217;ll initialise the rockets array.</p>
<pre class="code">	rockets = new Array();</pre>
<p>Then we&#8217;ll flesh out the event handler to create new rocket objects.</p>
<pre class="code">  var rocket = new firework();
  rocket.x = 50;
  rocket.y = stage.stageHeight-25;
  rocket.height = 40;
  rocket.scaleX = rocket.scaleY;
  addChildAt(rocket,1);
  rockets.push(rocket);</pre>
<p>Now the rocket class is an interesting one. This is where I&#8217;ve started to get a bit more interesting &#8211; starting to explore Flash. The key thing to realise is that the <strong>rocket</strong> class encapsulates the behaviour of a rocket. It sets itself up (via the constructor) and then it adjusts its own behaviour (in this instance location on the stage) each frame. How it does this is another interesting part of Flash: the <strong>Event.ENTER_FRAME</strong> event. Within the firework class constructor we have:</p>
<pre class="code">  addEventListener(Event.ENTER_FRAME, updateState);</pre>
<p>This fires off an event every time Flash starts a new “frame” &#8211; n times a second, where n is defined within the main Flash file&#8217;s properties.</p>
<p>If you dig through the firework class (and indeed the smoke class), you&#8217;ll see how they set themselves up, update their state once a frame and eventually remove themselves once a predefined lifetime has completed.</p>
<h3><a name="sourcecode">Source code</a></h3>
<p><a href="http://www.davegardner.me.uk/blog/wp-content/uploads/2010/01/flashfireworks.devel.zip">Click here to download the full source code.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.davegardner.me.uk/blog/2010/01/27/getting-started-with-flash-as3-from-a-php-developers-perspective/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
