<?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>Hjælp mig nu</title>
	<atom:link href="http://blog.hjaelpmignu.dk/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hjaelpmignu.dk</link>
	<description>Hjælp til Adobes programmer</description>
	<lastBuildDate>Tue, 17 Apr 2012 19:20:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Gradient mesh with a hole in Adobe Illustrator (tutorial)</title>
		<link>http://blog.hjaelpmignu.dk/2012/04/gradient-mesh-with-a-hole-in-adobe-illustrator-tutorial/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/04/gradient-mesh-with-a-hole-in-adobe-illustrator-tutorial/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 19:20:11 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[Adobe Illustrator]]></category>
		<category><![CDATA[Tutorial og artikler]]></category>
		<category><![CDATA[compound path]]></category>
		<category><![CDATA[gradient mesh]]></category>
		<category><![CDATA[illustrator]]></category>
		<category><![CDATA[masking]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=527</guid>
		<description><![CDATA[Just a quick note, that I posted a new short tutorial on how to make a gradient mesh with a hole in. Nothing fancy, but nice to know. See http://www.hjaelpmignu.dk/content/punching-holes-gradient-mesh for more info]]></description>
			<content:encoded><![CDATA[<p>Just a quick note, that I posted a new short tutorial on how to make a gradient mesh with a hole in.</p>
<p>Nothing fancy, but nice to know. See <a title="Punching holes in a gradient mesh" href="http://www.hjaelpmignu.dk/content/punching-holes-gradient-mesh">http://www.hjaelpmignu.dk/content/punching-holes-gradient-mesh</a> for more info</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/04/gradient-mesh-with-a-hole-in-adobe-illustrator-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tile based games using the Starling framework</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/tile-based-games-using-the-starling-framework/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/tile-based-games-using-the-starling-framework/#comments</comments>
		<pubDate>Wed, 28 Mar 2012 10:54:20 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Adobe Flash Builder]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Gaming]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[starling]]></category>
		<category><![CDATA[tile based games]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=516</guid>
		<description><![CDATA[In my quest for making my sons game, I managed to get Starling up and running, and create a performance test, that showed me some pretty intense performance &#8211; that bode well for my continuous success in the making of a game. I have started to dive into tiles, and how to make tile based ...]]></description>
			<content:encoded><![CDATA[<p>In my quest for making <a href="http://blog.hjaelpmignu.dk/2012/03/dad-can-i-have-a-game/">my sons game</a>, I managed to get Starling <a href="http://blog.hjaelpmignu.dk/2012/03/getting-hooked-up-with-starling/">up and running</a>, and create a <a href="http://blog.hjaelpmignu.dk/2012/03/testing-starling-performance/">performance test</a>, that showed me some pretty intense performance &#8211; that bode well for my continuous success in the making of a game.</p>
<p>I have started to dive into tiles, and how to make tile based games. Some of the most relevant info on that topic was found in an old book called &#8220;Flash MX 2004 game design demystified&#8221;. As the title implies, it is not quite current, but the theories behind tiles still works a long way.</p>
<p>Continuing on the seas of The Web, I stumbled onto Tonypa and his section about <a href="http://www.tonypa.pri.ee/tbw/start.html">Tile Based Games</a> (via <a title="Terry Patons website" href="http://pixelpaton.com/">Terry Paton</a> - thanks). The problem with that info was, that it was just as old as my book reference. Therefor I decided to send him a mail, and ask for permission to update his tutorials to a more current ActionScript 3.0/Flash Builder/Starling version.</p>
<p>I will have to do a lot of learning, while porting this to AS3, and there may be introduced several errors and rewrites along the way, but I take that as a part of the proces. I will hopefully end up with a solid foundation of tutorial to use in my own game, and the continuing work on that project.</p>
<p>Feel free to comment, ask questions about unclear parts, and come with suggestions to improvements &#8211; I&#8217;m almost guaranteed not to get this right at the first shot <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Take a look at <a href="http://blog.hjaelpmignu.dk/tilebasedgames/">Tile Based Games</a> in the top menu.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/tile-based-games-using-the-starling-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Starling performance</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/testing-starling-performance/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/testing-starling-performance/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 14:13:06 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Adobe Flash Builder]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Tutorial og artikler]]></category>
		<category><![CDATA[undervisning]]></category>
		<category><![CDATA[www.hjaelpmignu.dk]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[performance test]]></category>
		<category><![CDATA[starling]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=461</guid>
		<description><![CDATA[Get the work files and final example here This post is just a quick solution to test the performance of Starling. It build upon the previous one called Getting hooked up with Starling, and just adds a lot of the mice and make them react to the mouse. The process here is simply: Initialize Starling ...]]></description>
			<content:encoded><![CDATA[<p>Get the <a href="http://blog.hjaelpmignu.dk/wp-content/uploads/manual_uploads/StarlingPerformance.zip">work files and final example here</a></p>
<p>This post is just a quick solution to test the performance of Starling. It build upon the previous one called <a title="Getting hooked up with Starling" href="http://blog.hjaelpmignu.dk/2012/03/getting-hooked-up-with-starling/">Getting hooked up with Starling</a>, and just adds a lot of the mice and make them react to the mouse.</p>
<p>The process here is simply:</p>
<ol>
<li>Initialize Starling</li>
<li>Wait for the stage to be ready</li>
<li>Insert a whole lot of images</li>
<li>Make each image react to their position in relation to the mouse.</li>
<li>See, how it goes</li>
</ol>
<p>Initializing starling and waiting for the stage, was explained in the other tutorial, so I will discuss the other topics here. This is an example of what you will be doing.</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="starlingtest" width="550" height="400">
      <param name="movie" value="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/StarlingPerformanceTest.swf" />
      <param name="wmode" value="direct" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/StarlingPerformanceTest.swf" width="550" height="400" wmode="direct">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h2>Insert more than one image</h2>
<p>When dealing with images, it is generally a good idea to define a custom class for them. This way it is easier to manipulate and retain individual information in the individual images. In this example you should go ahead and add a custom class. You can use the Start project to follow along or even better, complete the previous tutorial and use those files <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Right click on the default package and select <strong><em>New &gt; ActionScript Class</em></strong></p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/new-class.png"><img class="alignnone size-medium wp-image-462" title="new class" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/new-class-300x159.png" alt="" width="300" height="159" /></a></p>
<p>&nbsp;</p>
<p>Call the Class CustomImage and make the Image class <em>(starling.display.Image)</em>. Declare two public variables called destX and destY as numbers, so the code should look like this:</p>
<pre>package
{
  import starling.display.Image;
  import starling.textures.Texture;

  public class CustomImage extends Image
  {
<strong> public var destX:Number;</strong>
<strong> public var destY:Number;</strong>
    public function CustomImage(texture:Texture)
    {
      super(texture);
    }
  }
}</pre>
<p>These variables are used to store the destination coordinates for each image. They are supposed to move to a random new location from time to time, and these variables are taking care of the values.</p>
<p>Open the StarlStage.as file and declare the following variables:</p>
<pre>[Embed(source="assets/mouse.png")]
private var mouseBMP:Class;

<strong>private const NUM_MICE:uint = 500;</strong>
<strong>private var miceVector:Vector.&lt;Image&gt; = new Vector.&lt;Image&gt;(NUM_MICE, true);</strong>

<strong>private var mouseCoord:Point = new Point(); </strong>
<strong>private var maxDist:Number;</strong></pre>
<p>First, you create a constant that holds the number of mice you want in the test. Change that to suit your need. Then a vector object is created. The first argument is the number of elements in the vector, and the second argument (set to true) is creating a fixed vector, so it isn&#8217;t allowed to change size during runtime. These arguments are optional, and only set for performance reasons.</p>
<p>After that a Point object is created to hold the position of the mouse, and a value to hold the maximum distance there can be between two points on the stage (a diagonal line).</p>
<p>Change the onAdded() method, so it looks like this:</p>
<pre>private function onAdded(event:Event):void
  {
    q = new Quad(800, 600, 0x880000);
    addChild(q);</pre>
<pre>    maxDist = Math.sqrt((stage.stageWidth * stage.stageWidth) + (stage.stageHeight * stage.stageHeight));

    var texture:Texture = Texture.fromBitmap(new mouseBMP());

    for(var i:int = 0; i &lt; NUM_MICE; i++)
    {
      var image:CustomImage = new CustomImage(texture);
      image.pivotX = image.width &gt;&gt; 1;
      image.pivotY = image.height &gt;&gt; 1;
      image.alpha = Math.random();
      image.destX = Math.random()*stage.stageWidth;
      image.destY = Math.random()*stage.stageHeight;
      image.x = Math.random()*stage.stageWidth;
      image.y = Math.random()*stage.stageHeight;
      image.rotation = deg2rad(Math.random()*360);

      addChild(image);
      miceVector[i] = image;
    }
}</pre>
<p>The first two lines is the colored background, made from the Quad. Next line uses the <a href="http://www.hjaelpmignu.dk/content/calculating-distance">Pythagorean Theorem</a> to calculate the diagonal based on the width and height of the stage. The value is stored in maxDist for later use.</p>
<p>Then the texture is created, exactly like in the previous tutorial, and a loop is set up to generate the amount of mice, that is declared in NUM_MICE.</p>
<p>Fir you declare a variable called image as the type CustomImage, which is the class you made earlier. pivotX and pivotY is a nice set of properties that Starlings display objects have. They move the origin point inside the object to a new position. In Flash, you are normally moving the object in a negative direction in order to be able to rotate or scale around the middle of the object; With Starling you can just set the pivot point to be any coordinate in the image. The bit shift to the right (&gt;&gt;) is just a fancy way of divide by 2. I could have entered something like <strong>image.height/2</strong> but I am told that the performance of this bit shift is a bit faster, so I-ll stick with that <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The rest sets a lot of random numbers in the objects properties (alpha, x, y and rotation). It also sets the public variables inside the CustomImage class, with new random coordinates.</p>
<div class="callout_box"><h4>degrees to radian conversion</h4><div class="inner"> Pay attention to the rotation property. Starling uses radians, when working with rotation. Fortunately, you can use the little helper deg2rad() in Starling. Just remember to import starling.utils.deg2rad </div><!--inner--></div><!--callout_box-->
<p>Finally the image is added to the stage, and places inside the vector, so it can be referenced later on.</p>
<p>Test the project to see a lot of mice with different position, rotation and transparency on a dark red background.</p>
<h2>Getting the coordinates from the mouse</h2>
<p>The Starling framework do not work with mouse events, but it has (like Flash) an event called <a href="http://doc.starling-framework.org/core/starling/events/TouchEvent.html">TouchEvent</a>, that takes care of most of your needs. Add a listener called onTouch() and update the constructor so it looks like this:</p>
<pre>public function StarlStage()
{
  addEventListener(Event.ADDED_TO_STAGE, onAdded);
  addEventListener(TouchEvent.TOUCH, onTouch);
}

private function onTouch(event:TouchEvent):void
{
  var touch:Touch = event.getTouch(stage);
  mouseCoord = touch.getLocation(stage);
}</pre>
<p>In the constructor you listen for the TouchEvent (remember to import the Starling version), and set it to call the onTouch method. A touch event can contain multiple touch, so there is a method called getTouch() and one called getTouches(). Here we just take one single touch from stage, and keep it in the object called called touch. These <a href="http://doc.starling-framework.org/core/starling/events/Touch.html">Touch </a>objects holds all relevant information regarding the specific place it points to. Here you use the method getLocation to retrieve a Point objects, that is store in your own mouseCoord variable for later use.</p>
<h2>Make the mice move</h2>
<p>Not it&#8217;s time to move the mice. Start by registering a listener last in the onAdded() method, so the last line should look like this:</p>
<pre>private function onAdded(event:Event):void
{
  ...
  //previous code here
  ...
  stage.addEventListener(Event.ENTER_FRAME, draw);
}</pre>
<p>Again, remeber to import Starlings own Event class <em>(starling.events.event)</em>, instead of the one you are used to in Flash. Create a method called draw, and enter the following:</p>
<pre>private function draw(event:Event):void
{
  var distance:Number;
  var tmpImg:CustomImage;
  var ratio:Number;

  for (var i:int = 0; i &lt; NUM_MICE; i++)
  {
    tmpImg = miceVector[i] as CustomImage;
    tmpImg.x -= (tmpImg.x - tmpImg.destX) * .01;
    tmpImg.y -= (tmpImg.y - tmpImg.destY) * .01;
    if(Math.abs(tmpImg.x - tmpImg.destX) &lt; 1 &amp;&amp; Math.abs(tmpImg.y - tmpImg.destY) &lt; 1)
    {
      tmpImg.destX = Math.random()*stage.stageWidth;
      tmpImg.destY = Math.random()*stage.stageHeight;
    }

    distance = Point.distance(mouseCoord, new Point(tmpImg.x, tmpImg.y));

    ratio = distance/maxDist
    tmpImg.rotation += 0.2 * ratio;
    tmpImg.scaleX = tmpImg.scaleY = 1 - ratio;
  }
}</pre>
<p>That&#8217;s a lot, so let&#8217;s take it in small chunks. First you create two variables, one to hold the distance between the mouse and the current image. The other is just a temporary instance of the image made outside the loop, so you don&#8217;t fill up the memory with hundreds of local instances. This way, you are using the same object over and over again.</p>
<p>Then a loop is set up, that iterates through your pool of images, contained in the vector called <em>miceVector</em>. Inside the loop you first transfer a reference to the current objects to tmpImg. From now on, you can avoid the time consuming process it is for Flash to look up an object in arrays and vectors &#8211; it is also a bit easier to read.</p>
<pre>tmpImg.x -= (tmpImg.x - tmpImg.destX) * .01;
tmpImg.y -= (tmpImg.y - tmpImg.destY) * .01;</pre>
<p>Then each image is moved a bit closer to it&#8217;s destination. This is done, by taking the distance between the two points (current and destination) and multiply it with a low number &#8211; in this case <strong>.01</strong>. If you enter a higher value, they will travel faster, but since it is redrawn 60 times per second (if it can), you are working with very low numbers here. In more complex solutions, I would introduce dedicated tween classes, but this should do it, for a quick test.</p>
<pre>if(Math.abs(tmpImg.x - tmpImg.destX) &lt; 1 &amp;&amp; Math.abs(tmpImg.y - tmpImg.destY) &lt; 1)
{
  tmpImg.destX = Math.random()*stage.stageWidth;
  tmpImg.destY = Math.random()*stage.stageHeight;
}</pre>
<p>Then you test if the distance between current position and destination is below 1. If it is, a new destination point is set in the images destX and destY. This is done, because, you would never reach the exact same values when multiplying with .01.</p>
<pre>distance = Point.distance(mouseCoord, new Point(tmpImg.x, tmpImg.y));</pre>
<p>This one can be a bit tricky. It uses a stetic method in the Point class called distance, to get the distance between two point objects. it actually just does the same as the calculation of maxDist earlier on. Since tmpImage doesn&#8217;t have a Point as a property, you create one. I am not sure, that this is very performance effective, but I included it here, so you can see both methods in practice.</p>
<pre>ratio = distance/maxDist
tmpImg.rotation += 0.5 * ratio;
tmpImg.scaleX = tmpImg.scaleY = 1 - ratio;</pre>
<p>the last lines takes care of scaling and rotation of the objects. First a ratio between the distance variable and the maximum distance is found. This results in a value between 0 and 1. This value is the multiplied with 0.5 to get a number between 0 and 0.5 for the rotation. Remember, that rotation in Starling is in radians, so 0.5 is actually quite a lot <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The it scales both x and y on the image based on the same ratio and the value 1 (which mean 100%). The ratio is subtracted from 1. This makes the images smaller the further away from the mouse it is.</p>
<h2>Take it for a spin</h2>
<p>Try running the application. Fiddle with the values, to achieve other effect. Try to adjust NUM_MICE, to test the performance. There is certainly room for improvements, but I am pretty impressed with the performance. My machine is taking 1000 objects without blinking &#8211; I can&#8217;t recall doing that earlier, while using square root, and having semi-transparent images rotating and moving on top of each other- Starling is fun <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/testing-starling-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting hooked up with Starling</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/getting-hooked-up-with-starling/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/getting-hooked-up-with-starling/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 10:37:26 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Adobe Flash Builder]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Gaming]]></category>
		<category><![CDATA[Tutorial og artikler]]></category>
		<category><![CDATA[www.hjaelpmignu.dk]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[starling]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=433</guid>
		<description><![CDATA[Files with a working solution is available here One of the more promising aspects of the new Flash Player and AIR runtime is the Stage3D object. The obvious and breathtaking improvements is seen in experiments like http://www.unrealengine.com/flash/ where you are moving around in a 3D landscape in realtime, with reflections, light and all the Unreal Engine can ...]]></description>
			<content:encoded><![CDATA[<p>Files with a working solution is <a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/StarlingTest_Final.zip">available here</a></p>
<p>One of the more promising aspects of the new Flash Player and AIR runtime is the Stage3D object. The obvious and breathtaking improvements is seen in experiments like <a href="http://www.unrealengine.com/flash/">http://www.unrealengine.com/flash/</a> where you are moving around in a 3D landscape in realtime, with reflections, light and all the Unreal Engine can throw at you. Another great example is Unity, that exports to the Flash Player. Take a look at the video on <a href="http://blogs.unity3d.com/2011/09/01/unity-and-flash-a-sneak-peek/">http://blogs.unity3d.com/2011/09/01/unity-and-flash-a-sneak-peek/</a> to see how it raises the bar for what is possible in a browser.</p>
<p>My project with Mouse Wood calls for a simpler solution, though. Fortunately Stage3D is not only for 3D content. There are several initiatives like <a href="http://www.nulldesign.de/category/experiments/nd2d/">ND2D</a>, <a href="http://www.flashrealtime.com/m2d/">M2D</a> and <a href="http://citrusengine.com/">Citrus Engine</a>, that I find very interesting because of it&#8217;s buit-in level editor and side scrolling ability (and I am in desperate need of inside knowledge on how to make side scrolling games). My love falls on the <a href="http://gamua.com/starling/">Starling Framework</a>. First of all, it looks simple and straight forward &#8211; and have a lot of similarities to the traditional display list approach. Secondly, I hear it mentioned quite a few places, performing some nice results on both desktop as well as mobile. I may look into the other as well, but this article dives into Starling.</p>
<h2>Get the packages</h2>
<p>Starling originates from the gaming framework called Sparrow, that targets iOS game development. Both frameworks resides at the site http://gamua.com/ and provides easy access to packages and tutorials. You get access to the framework by doing the following:</p>
<ol>
<li>Go to http://gamua.com/starling/download/ and hit the <strong>Download Starling</strong> button (currently 1.0)</li>
<li>Unzip the package</li>
</ol>
<p>Inside you will find a few folders and a read me-, change log- and license file.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Skærmbillede-2012-03-13-kl.-16.26.48.png"><img class="alignnone  wp-image-434" title="Skærmbillede 2012-03-13 kl. 16.26.48" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Skærmbillede-2012-03-13-kl.-16.26.48.png" alt="Folders in Starling" width="432" height="319" /></a></p>
<p>There is a folder with some samples, and a folder to Flex Unit Tests. I will not discuss these files, but solely focus on the <em>starling</em> folder. Inside the Starling folder you will see a few folders. You are interested in the <em>bin</em> folder and the <em>src</em> folder &#8211; actually, you are only interested in the bin folder. Looking into the source folder, you will find all the ActionScript classes, that makes up the framework. The same files, are the one compiled into one handy swc-file (ShockWave Component, Small Web Component &#8211; or something like that. Nobody seems to know .. or care) located in the bin folder for you to keep.</p>
<p>Copy the starling.swc from the bin folder to where you keep you class-files. I have made a folder in my dropbox folder, so everytime I update a package on one of my machines, i automatically gets updated on the other machines.</p>
<h2>Link to the framework</h2>
<p>In my experiments I will primarily use Flash Builder for the development. So fire up Flash Builder and:</p>
<p>1. Create a new ActionScript project (File &gt; New &gt; ActionScript Project)<br />
2. Call it StarlingTest (or similar) as the project name.<br />
3. Click <strong>Next</strong> to go to the <em>Build Paths</em>.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Skærmbillede-2012-03-13-kl.-17.52.29.png"><img class="wp-image-436 alignnone" title="Library Path in Flash Builder" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Skærmbillede-2012-03-13-kl.-17.52.29.png" alt="" width="376" height="480" /></a><br />
4. In the dialog, click on the Add SWC button. Navigate to the startling.swc and add that to the list.<br />
5. Click on finish, to create the project.</p>
<p>NB: If you are following along in Flash Professional, you can add the swc&#8217;s so they are always available in the programs preferences (under ActionScript settings) or per project (in it&#8217;s publish settings)</p>
<h2>Making a stage3D object</h2>
<p>With the project in place. You will now make a Stage3D object. The Stage3D object i placed in the Display List just like any other display object. In fact it is placed behind the stage. You can have many stage3D object on top of each other, but they all have to be behind the normal stage and it&#8217;s contained objects &#8211; so no stage3D floating around a traditional animation in Flash, only the other way around.</p>
<p>With StarlingTest.as open &#8211; enter the following code. The highlighted areas is where you should enter text, if you follow along:</p>
<pre>package
{
  import flash.display.Sprite;
  import starling.core.Starling;

  <strong>[SWF(frameRate="60", width="800", height="600", backgroundColor="0xFFFFFF")]</strong>
  public class StarlingTest extends Sprite
  {
    <strong>private var starl:Starling;</strong>

    public function StarlingTest()
    {
      <strong>starl = new Starling(StarlStage, stage);
</strong><strong> starl.start();</strong></pre>
<pre>    }
  }
}</pre>
<p>First is the SWF-meta tag, that allows you to set specific parameters that is used at compile time. This one simply sets the frame rate to 60fps, width and hight and finally a white background.</p>
<p>Then you declare an object of the type Starling. This object is the main motor. It translates everything that goes into it into the native Stage3D framework underneath. Inside the constructor, it is initialized. The signature has a lot of parameters, but in general i believe you will use the first two in most cases. The first one, is the name of the Class that is supposed to be inserted on stage and the other one is just a reference to the stage object. This example references the StarlStage class that isn&#8217;t defined yet, so you will get an error &#8211; don&#8217;t worry. You will create it in a moment.</p>
<p>The Starling object itself has a few methods and properties, that can be used to control the behaviour of the object. The one you will use everytime is the start() method, that starts up the engines.</p>
<h2>Create the Starling sprite class</h2>
<p>Right click in the default package and choose <strong>New &gt; ActionScript Class</strong>. In the dialog box enter &#8220;StarlStage&#8221; in the name field (the same name you refered to in the main class). In the input field called Superclass, begin typing the word &#8220;sprite&#8221; to see the code completion kick in. Select the option called <strong>Sprite &#8211; starling.display </strong>to make sure that the sprite inherits from the right superclass.</p>
<dl id="attachment_444">
<dt><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/starling-dialog.png"><img title="Starling dialog" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/starling-dialog.png" alt="" width="497" height="393" /></a></dt>
</dl>
<h2>Showing a background with the Quad class</h2>
<p>One of the more interesting classes I found in the Starling framework is the <a title="The Quad class" href="http://doc.starling-framework.org/core/">Quad</a>. Maybe I just haven&#8217;t looked around, but I haven&#8217;t seen other objects, that made it so easy to manipulate rectangles, and their colors. The Quad is basically a rectangle that is able to present a uniform color or a gradient. Not a normal gradient, but a gradient that radiates from every vertex in the rectangle.</p>
<div id="attachment_442" class="wp-caption alignnone" style="width: 230px"><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Quad_vertices.png"><img class="size-full wp-image-442" title="Quad Vertices" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Quad_vertices.png" alt="Quad Vertices" width="220" height="218" /></a><p class="wp-caption-text">The individual vertex is stores in an array, going from top-left, over top-right and down to bottom-left and finally bottom-right</p></div>
<p>The signature of the class is:</p>
<pre>Quad(width:Number, height:Number, color:uint = 0xffffff, premultipliedAlpha:Boolean = true)</pre>
<p>You simply set width and height. If you leave all other parameters, you will get a white box. You can set the third and fourth parameter to change the color or the default alpha method, premultiplied alpha. (<a title="Premultipliead alpha" href="http://blog.hjaelpmignu.dk/2012/03/what-is-premultiplied-alpha-blending/">read more about it here</a>)</p>
<p>With the StarlStage.as active enter the following code. The text in bold is the one you should add:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> starling<span style="color: #000066; font-weight: bold;">.</span>display<span style="color: #000066; font-weight: bold;">.</span>Quad<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> starling<span style="color: #000066; font-weight: bold;">.</span>display<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> starling<span style="color: #000066; font-weight: bold;">.</span>events<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> StarlStage <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span>private <span style="color: #6699cc; font-weight: bold;">var</span> q<span style="color: #000066; font-weight: bold;">:</span>Quad<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">&lt;/</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> StarlStage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">&lt;</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ADDED_TO_STAGE</span><span style="color: #000066; font-weight: bold;">,</span> onAdded<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">&lt;/</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span>    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">&lt;</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onAdded<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span> q = <span style="color: #0033ff; font-weight: bold;">new</span> Quad<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">800</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">600</span><span style="color: #000066; font-weight: bold;">,</span> 0x880000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>q<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000066; font-weight: bold;">&lt;/</span>strong<span style="color: #000066; font-weight: bold;">&gt;</span>  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p lang="actionscript3">First it declares the Quad as the variable q. Then the class listens for the event that is triggered, when the object is added to the stage. It is a good habit to make sure the stage is available before adding content to it. In this case, you could do it all in the constructor, but you may end up in a situation later on, where you need to be sure that the stage is reachable &#8211; this way, you are on the safe side.</p>
<p lang="actionscript3">Finally in the onAdded() listener, you instatiate it with a width, a height and a dark red color. Because the similarity between traditional display objects and Starlings display objects, it&#8217;s just a matter of calling addChild() to place it in the display list.</p>
<h2 lang="actionscript3">Test it</h2>
<p lang="actionscript3">Now, would be a great time to see if everything is working as expected. If you try to run the code, you will see that it shows an error on the screen that says: &#8220;This application is not correctly embedded (wrong wmode value)&#8221;</p>
<p lang="actionscript3"><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/rendermode.png"><img class="alignnone size-full wp-image-446" title="Render mode" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/rendermode.png" alt="" width="690" height="132" /></a></p>
<p lang="actionscript3">This is a default message, that Starling displays if you haven&#8217;t enabled the the players the right access to the GPU. To do that, open the file called StarlingTest-app.xml that is located in the root of your source folder. This file contains all relevant information about the project, and this is where you will find the node called RenderMode (around line 104). Uncomment that node and correct it to:</p>
<pre>&lt;renderMode&gt;direct&lt;/renderMode&gt;</pre>
<p>This way you tell the application, that although you will render vectors through the software renderer they will blit through the GPU. It is also possible to enter &#8220;gpu&#8221; and make all the calculations in the GPU, but for the most part, I believe that direct gives the best performance &#8230; but you can fiddle with that.</p>
<p>Save all the documents and try again. You should see a dark, red colored window. If not, review your code, and look for errors. You might download and look at the accompanying files to look for things that might have gone wrong.</p>
<h2>Placing an image</h2>
<p>OK, with a Quad in place, it is time to step up a level &#8211; let&#8217;s introduce images. You can&#8217;t go around showing colored rectangles all day, at a point you have to bring in some detailed pixels. Update your code, so it looks like this:</p>
<pre>package
{
  import flash.display.Bitmap;

  import starling.display.Image;
  import starling.display.Quad;
  import starling.display.Sprite;
  import starling.events.Event;
  import starling.textures.Texture;

  public class StarlStage extends Sprite
  {
    private var q:Quad;

<strong> [Embed(source="assets/mouse.png")]</strong>
<strong> private var mouseBMP:Class;</strong><strong> </strong>
    public function StarlStage()
    {
      addEventListener(Event.ADDED_TO_STAGE, onAdded);
    }

    private function onAdded(e:Event):void
    {
      q = new Quad(800, 600, 0x880000);
      addChild(q);
<strong> var mouseBMP:Bitmap = new mouseBMP();</strong>
<strong> var mouseTEX:Texture = Texture.fromBitmap(mouseBMP);</strong>
<strong> var mouse:Image = new Image(mouseTEX);</strong>
<strong> addChild(mouse);</strong>
    }
  }
}</pre>
<p>That was a lot for a little result, you may think. Well, some of it is the way Flash Builder is used to embed images, and some of it is because of Starlings way to treat content inside images as textures &#8211; actually Image works like Flash&#8217;s own Bitmap class. <a title="The Image class" href="http://doc.starling-framework.org/core/starling/display/Image.html">In the references</a> it is told like this:</p>
<blockquote><p>An Image is a quad with a texture mapped onto it.</p>
<p>The Image class is the Starling equivalent of Flash&#8217;s Bitmap class. Instead of BitmapData, Starling uses textures to represent the pixels of an image. To display a texture, you have to map it onto a quad &#8211; and that&#8217;s what the Image class is for.</p></blockquote>
<p>Let&#8217;s take the code in chunks</p>
<pre> [Embed(source="assets/mouse.png")]
 private var mouseBMP:Class;</pre>
<p>This is the normal way to embed images in Flash Builder. By using the Embed meta tag you are making the image available from the beginning, instead of loading it in at runtime. You can read more about the embed meta tag at <a href="http://www.adobe.com/devnet/flash/articles/embed_metadata.html">http://www.adobe.com/devnet/flash/articles/embed_metadata.html</a></p>
<p>You are capturing the path to the source and immediately assign it to a class object. This is because you have to assign the image info to an object in order to reference it later on in the code. The second part of the code is executed when the stage is ready, and looks like this:</p>
<pre>var mouseTEX:Texture = Texture.fromBitmap(new mouseBMP());
var mouse:Image = new Image(mouseTEX);
addChild(mouse);</pre>
<p>First you make a texture, to place on your image. You are using the static methods in the <a href="http://doc.starling-framework.org/core/starling/textures/Texture.html">Texture class</a> to tell, what will be the foundation for the texture. This example use Texture.fromBitmap() to indicate that a bitmap info is used for the texture. As argument, you just enter &#8220;new mouseBMP()&#8221; so a new instance of the image is passed on to the texture.</p>
<p>The next line declares Starlings Image object, that is similar to the Flash Bitmap class. Instead of passing bitmap data to a bitmap object, you are passing the texture data to an image object in Starling &#8211; similar proces, different names. Finally it is added to the stage with addChild()</p>
<h2>Now what?</h2>
<p>This is the absolute fundamentals of presenting images and Quads. In the next tutorial, I will add some movement, but use this tutorial as a reference to get things out on the stage in Starling &#8211; Other articles I do will take it for granted and refer to this one for explanation.</p>
<p>Feel free to comment and ask quations in the <a title="ActionScript forum" href="http://www.hjaelpmignu.dk/forum/14">forum</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/getting-hooked-up-with-starling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is premultiplied alpha blending</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/what-is-premultiplied-alpha-blending/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/what-is-premultiplied-alpha-blending/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 10:35:54 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[www.hjaelpmignu.dk]]></category>
		<category><![CDATA[blending]]></category>
		<category><![CDATA[pre-multiplied alpha]]></category>
		<category><![CDATA[premultiplied alpha]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=452</guid>
		<description><![CDATA[In my game research, and the Starling framework, i stumpled on the name premultiplied alpha. It take some time to get the head around the concept, but it is a faster way to blend images on top of each other. This is just on nice to know basis, and there may be some aspects of ...]]></description>
			<content:encoded><![CDATA[<p>In my game research, and the Starling framework, i stumpled on the name premultiplied alpha. It take some time to get the head around the concept, but it is a faster way to blend images on top of each other.</p>
<p>This is just on nice to know basis, and there may be some aspects of this, that is not 100% right presented here, but I am sure that you get a fairly solid understanding of what premultiplied alpha blending is about.</p>
<p>When you have a color value, you normally take the color values and multiply with the alpha value afterwards. In premultiplied alpha, the reduction in color value is calculated and presented in the number.</p>
<p>This brings some serious performance improvements at the expense of a little precision. This method is also an effective way to introduce transparency in the color and minimize fringes around the edges. An example.</p>
<p>A value of (1, 0, 0, 0.5) would normally give 100% red at 50% transparency. In premultiplied alpha, it would be (0.5, 0 , 0, 0.5) &#8211; the alpha value is merged into the color value.</p>
<p>Most people understand the concept of premultiplied values, by how much they occupy a pixel, so you don&#8217;t make it transparent but fill out an area of a pixel. Let&#8217;s take a yellow and cyan color, and mix them.</p>
<p>Yellow is normally (1, 1, 0), so that would be (0.5, 0.5, 0, 0.5) in premultiplied. Because we now, that the calculation is made beforehand, we can assume this to be a fully yellow color occupying 50% of the pixel.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/perceptual1.png"><img title="Premultiplied step 1" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/perceptual1.png" alt="" width="133" height="137" /></a></p>
<p>The cyan would be (0, 0.5, 0.5, 0.5) for at 50% pure cyan color. Placed on top it will look something like this.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Perceptual2.png"><img title="Premultiplied step 2" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Perceptual2.png" alt="" width="135" height="137" /></a></p>
<p>Because you have a pre-multiplied value, you can use an equation like this:</p>
<pre>val = frontval+((max-frontalpha)*backval);</pre>
<p>to calculate the final value. With yellow as back color and cyan as from color, that turns out as:</p>
<p>R = 0+((1-0.5)*0.5)<br />
G = 0.5+((1-0.5)*0.5)<br />
B = 0.5+((1-0.5)*0)<br />
A=0.5+((1-0.5)*0.5)</p>
<p>which results in a RGBA value of (0.25, 0.75, 0.50, 0,75). You will see the difference in the Red and Blue channel reflects, which of the colors that are placed on top of the other.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/step3.png"><img title="Premultiplied step 3" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/step3.png" alt="" width="135" height="137" /></a></p>
<p>Here the result is illustrated by the RGB letters. Think of small letters as having half the weight as the capital letters. Because the cyan is in front of the yellow it&#8217;s channels are &#8220;eroding&#8221; information away from the underlying color. So the calculation is something like: red (1), green (1+2=3), blue (2) that reflects the ratios as the calculated result.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/what-is-premultiplied-alpha-blending/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dad, can I have a game</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/dad-can-i-have-a-game/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/dad-can-i-have-a-game/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 08:36:37 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Adobe Flash Builder]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Gaming]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=427</guid>
		<description><![CDATA[The other day my 7-year-old som (Demolition Man) asked me if he could have his own game. &#8220;Well, of course,&#8221; I instinctively replied. I can do Flash &#8211; which makes it an easy task. Then again &#8211; I have never made a game before. I have made small exercises like jig-saws, puzzles, small one-screen shooters, nut never ...]]></description>
			<content:encoded><![CDATA[<p>The other day my 7-year-old som (Demolition Man) asked me if he could have his own game. &#8220;Well, of course,&#8221; I instinctively replied. I can do Flash &#8211; which makes it an easy task. Then again &#8211; I have never made a game before. I have made small exercises like jig-saws, puzzles, small one-screen shooters, nut never a game of larger scale, with highscores, instruction screen, multiple levels and so on.</p>
<p>In addition to that, Flash is stepping up, when it comes to games. It have a dedicated site at http://gaming.adobe.com/ and is actively supporting efforts to bring Flash into the game sphere. I think it has advantages in its multi-platform approach, and unique possibilities of adding timeline animation and effects from Flash Professional and the scalability and modularity of Flash Builder.</p>
<p>My son made some initial drawings, and I am in the progress of changing them into illustrations, that can be used in the game. There will be alterations, re-drawings and enhancements along the way, but it is my intention that he does the designs, and I muscle the machine.</p>
<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/montage.png"><img class="alignnone  wp-image-428" title="Mouse Wood" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/montage.png" alt="" width="539" height="381" /></a></p>
<h2>Mouse Wood</h2>
<p>The plot, as far as I can decipher is something like this:</p>
<p>&#8220;The game is about a mouse (without ears) that runs through the woods. While running, it should avoid snails and other animal trying to get to it. Furthermore, there is apples dropping down from the trees that should be avoided. Around the woods, there is small chunks of cheese, lying around. Is the mouse eats enough of these cheeses, it can spit on the animals to make the smaller. When small enough, they can be eaten or jumped upon. Each level ends with the mouse is getting home to it&#8217;s mouse hole, or fighting a boss.&#8221;</p>
<p>&#8230; I can see something like that in my head &#8211; so that should be doable.</p>
<h2>Why are you doing this</h2>
<p>Because I haven&#8217;t made larger games before, there is a lot of learning here for me to do, also. Several new technologies/frameworks has arrived, and I thought I would just dive in, and see how far I can get with this experiment. I hope that this journey will bring some interesting blog posts about how to create a game. There isn&#8217;t planned any structure, and it is trial and error, but hopefully in the end, I will end up with a decent result.</p>
<p>When completed, I hope I can wrap it up in a set of tutorial, to help other getting their head around programming simple games with Flash.</p>
<h2>Where do I start?</h2>
<p>I have several aspects in top of my head, that I would settle with. First I will do some experiments with the <a href="http://gamua.com/starling/">Starling framework</a>, to see if I can get the characters to show up, in a Stage3D environment. Then I would apply some physics with either <a href="http://www.box2dflash.org/">Box2D</a> or <a href="https://github.com/deltaluca/nape">Nape</a> (other other, that may come around). I may also integrate TweenLite into animation of titles etc. (aah, familiar grounds). I think that I will wrap it all up in a framework, like the one presented in the comprehensive book called &#8220;<a href="http://www.amazon.com/Essential-Guide-Flash-Games-Entertainment/dp/1430226145">The Essential Guide To Flash Games: Building Interactive Entertainment with ActionScript 3.0</a>&#8221; (sounds like a proper title for my purpose). It is a large framework, but I see it referenced quite a few places, like Lee Brimelows tutorial on Finite State Machine <a href="http://www.gotoandlearn.com/play.php?id=154">Part 1</a> and <a href="http://www.gotoandlearn.com/play.php?id=155">Part 2</a>.</p>
<p>The levels also has to be drawn. I am actually on thin ice here. I have seen a few, but my son, wan&#8217;t something scrolling left and right. I think I will take that, as I progress &#8211; enough to stat with already. If you have any inspiration, links to tutorials, info on game creation or AS-classes, that should be considered &#8211; please leave a comment.</p>
<p>See, ya!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/dad-can-i-have-a-game/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adobe sheds some light on Adobe Shadow</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/adobe-sheds-some-light-on-adobe-shadow/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/adobe-sheds-some-light-on-adobe-shadow/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 10:15:50 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[Adobe Software]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[Adobe Shadow]]></category>
		<category><![CDATA[devices]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[responsive design]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=422</guid>
		<description><![CDATA[A recent presentation in Adobe Labs, are bringing a new and effective tool to the web designers tool belt. The problem, a lot of web designers have to deal with, is the time consuming task of optimizing the display to multiple screen sizes. Something designed for a desktop suffers when presented on the screen of ...]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Adobe_Shadow.png"><img class="alignright size-full wp-image-425" title="Adobe_Shadow" src="http://blog.hjaelpmignu.dk/wp-content/uploads/2012/03/Adobe_Shadow.png" alt="Adobe Shadow" width="93" height="96" /></a>A recent presentation in Adobe Labs, are bringing a new and effective tool to the web designers tool belt. The problem, a lot of web designers have to deal with, is the time consuming task of optimizing the display to multiple screen sizes. Something designed for a desktop suffers when presented on the screen of a small device. Dealing with users rotating their device from portrait to landscape, viewing in different resolutions on different operating systems &#8211; all in all cumbersome, and something you surely aren&#8217;t waving your hand saying &#8220;Pick me, pick meee!&#8221; for.</p>
<p>Well &#8230; until now, I guess. Adobe has made a tool called Adobe Shadow, that sync your browser (Google Chrome) with devices in the same network. Users of <a title="Mag+" href="http://www.magplus.com/">Mag+</a> know the magical feeling, when you update something in InDesign, and it updates instantly on the tablet. It is like it is all wired up, and the tablet just appears as a second (or third) monitor in your workflow. Now you can do it (for free) with Adobe Shadow.</p>
<p>To get it up and running just go to <a href="http://labs.adobe.com/technologies/shadow/">http://labs.adobe.com/technologies/shadow/</a> and follow the instructions. Some things to pay attention to, during installation, is:</p>
<ol>
<li>There was a problem with the URL to the Chrome extension in the instructions. The right one is <a href="https://chrome.google.com/webstore/detail/ijoeapleklopieoejahbpdnhkjjgddem">https://chrome.google.com/webstore/detail/ijoeapleklopieoejahbpdnhkjjgddem</a></li>
<li>If the extension doesn&#8217;t start, you may have forgotten to start the Shadow Application (<a href="http://labs.adobe.com/downloads/shadow.html">http://labs.adobe.com/downloads/shadow.html</a>)</li>
<li>It only works if the devices are on the <em>same</em> network. Some companies have several network for you to choose from. Maybe you have a separate secure network for your workstation, that doesn&#8217;t allow devices in. If you are facing problems, you could start a wireless hot spot on your device, and connect via that.</li>
</ol>
<p>What Adobe Shadow tries to do, is to take some of the pain out of handling multiple style sheets targeted specific devices or resolutions. When making responsive designs, you often have to work on remote servers, in a desktop-upload-device-update manner. In the &#8220;nudging&#8221; phase, that is a bit tedious. When Adobe Shadow is set up correctly, it will redirect the information from the desktop browser to all connected devices instantly. It will evaluate the devices specifications and present the relevant style sheet. This way, you can have four devices laid out next to you, that responds whenever you preview your website in the Chrome Browser.</p>
<p>You ought to try it out. It is pretty impressive to see multiple devices, that updates almost instantly as you work in the desktop browser. You can event remotely access the device and alter the CSS or HTML shown. Take a look at <a href="http://tv.adobe.com/watch/adobe-technology-sneaks-2012/adobe-shadow">http://tv.adobe.com/watch/adobe-technology-sneaks-2012/adobe-shadow</a> if you just want to see it in action &#8230; after that &#8211; if you design for multiple devices &#8211; you surely wanna try it yourself <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>/ockley</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/adobe-sheds-some-light-on-adobe-shadow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 Canvas (Book Review)</title>
		<link>http://blog.hjaelpmignu.dk/2012/03/html5-canvas-book-review/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/03/html5-canvas-book-review/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 13:33:40 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Book review]]></category>
		<category><![CDATA[canvas element]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=415</guid>
		<description><![CDATA[Book review of the book "HTML5 Canvas" that takes on teaching you all relevant information regarding the element and how to present and engage with content drawn on it.]]></description>
			<content:encoded><![CDATA[<h2><strong><img class="alignright" title="HTML5 Canvas" src="http://akamaicovers.oreilly.com/images/0636920013327/cat.gif" alt="Image of book cover for HTML5 Canvas" width="180" height="236" />Title: HTML5 Canvas</strong></h2>
<p><strong></strong><strong>Pages: </strong>652</p>
<p><strong>Writer: Steve Fulton &amp; Jeff Fulton</strong></p>
<p><strong>Publisher: <a title="HTML5 Canvas" href="http://shop.oreilly.com/product/0636920013327.do" target="_blank">O&#8217;Reilly</a></strong></p>
<p><strong>Genre: JavaScript</strong></p>
<p><strong>ISBN:</strong><br />
ISBN-10: 1-4493-9390-X<br />
ISBN: 978-1-4493-9390-8</p>
<h2>Introduction</h2>
<p>A lot of the hype around HTML5 and it&#8217;s so-called &#8220;Flash killer&#8221;-ability evolves around the canvas element &#8211; an element that makes it possible to draw on a designated area of the HTML-document and update or interact with it via JavaScript.</p>
<p>With the Flash Player abandoning the mobile browsers, this opens an immediate need for a technology, that satisfy the developers desire to create engaging content that goes beyond fading DOM-elements in or out of the page with jQuery &#8211; we are talking Physics, object collision and mouse interaction like we are used to on the Flash Platform. This is where the canvas element comes to the rescue, and this book tries to give you enough tools to work with the element.</p>
<h3>Tone of voice</h3>
<p>The book is precise and consistent in its tone of voice. It is not utterly humorously written, but is keeping a light and positive that makes most the more complex topics easier to swallow.</p>
<h2>Content</h2>
<p>It is topic based, and all the topics takes you through the main areas of the usage of the canvas element. It starts with simple topics and primitive examples on how to set up the canvas, and how to draw and render text on the elements.</p>
<p>When that is all set, it takes on Physics and animation. Inspired by elder and well written routines from books like &#8220;Flash MX 2004 Game Programming&#8221;, and &#8220;Flash Animation &#8211; making things move&#8221; (FriendsOfEd) they are converting the concepts, so the work in the JavaScript environment.</p>
<p>After a throughout discussion on the other two main areas, where HTML5 is said to lift the Flash burden &#8211; video and audio &#8211; it goes into creating a game, and discusses many topics surrounding game development: Timers, transformation of elements, Game State Machine etc.</p>
<p>Finally, there is a topic that shows how PhoneGap can be used to make programs, made with HTML and JavaScript, install and work as native Apps on the device. It also gives pointers for further exploration in WebGL (3D-environment) and working with ElectroServer.</p>
<h2>Conclusion</h2>
<p>For me, it is the first book on the HTML5 Canvas element I have ever read. As an experienced Flash Developer and well wandered in the world of JavaScript, I had no problem following the examples and extract the new knowledge presented in the book. However, this is not a book teaching you JavaScript. You may learn a lot of new JavaScript-stuff along the way, but there is little help when it comes to understanding of function, conditions and loops. Instead you are getting solid information  about the do&#8217;s and don&#8217;ts of Canvas-programming.</p>
<p>It is not a beginners book, it&#8217;s not a reference book, but for an intermediate programmer, you should have no problems at all. When it comes to the physics stuff and game logic, you may have to read it a few times to get your head around it.</p>
<p>If you are facing any problems, when it comes to topics covered in the book &#8211; things you don&#8217;t understand or wan&#8217;t a discussion about, feel free to ask questions in the <a title="Help me now - JavaScript forum" href="http://www.hjaelpmignu.dk/forum/17">JavaScript forum</a>.</p>
<p>Kind Regards<br />
Karsten Vestergaard (ockley)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/03/html5-canvas-book-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agnostic Programming</title>
		<link>http://blog.hjaelpmignu.dk/2012/02/agnostic-programming/</link>
		<comments>http://blog.hjaelpmignu.dk/2012/02/agnostic-programming/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 13:08:02 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[Agnostic Programming]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaSript]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=411</guid>
		<description><![CDATA[OK, I&#8217;m moving in a new direction here &#8211; and then again, I&#8217;m not. When teaching programming to my students, I am often trying to communicate the the general aspects of programming, the similarities with normal written languages, and the concept of treating the different languages as dialects rather than a complete new language. We ...]]></description>
			<content:encoded><![CDATA[<p>OK, I&#8217;m moving in a new direction here &#8211; and then again, I&#8217;m not. When teaching programming to my students, I am often trying to communicate the the general aspects of programming, the similarities with normal written languages, and the concept of treating the different languages as dialects rather than a complete new language. We all know, that there is variables and functions. We know about loops and conditions, but somehow a lot of students fail to elevate that to a level, where the concept behind is independent to the implementation of the concept.</p>
<p>If the have learned how to declare a variable in ActionScript, they sigh, when they have to do it in Java. If the are rotating and use trigonometry in Processing, it&#8217;s a drag to make the do the same in ActionScript. Sometimes you have to write more (or less), but in a lot of the situations, you just have to write it different to make it work.</p>
<p>I have started a book writing over at Help me now called <a title="Agnostic Programming" href="http://www.hjaelpmignu.dk/content/agnostic-programming">Agnostic Programming</a>. The title don&#8217;t refer to the &#8220;I don&#8217;t know if God exists&#8221;-thought, but rather the fact, that my knowledge about how to program, in many ways, don&#8217;t have to be tied up to a single language, but the ideas behind syntax and how to construct sentences can be used to model a sentence to a different dialect &#8211; for the most part, not even a different language, as it is the same keywords there is widely used. Sometimes a statement in one language is called something else, but in other cases, it&#8217;s just a matter of constructing the sentences.</p>
<p>I have started with languages that I use myself, as a starting point, and that is ActionScript, JavaScript, Processing and Java. While I know that they fundamentally pairs down to ECMA and Java, it is something I can relate to. I may introduce other languages, if someone will help transcribing to them. I might just take my own medicine and see, if I can dig it up myself. I&#8217;m actually already doing that with Java, which I&#8217;m not that familiar with.</p>
<p>This is an open write in progress, and in the beginning it&#8217;s probably most text. Later on the diagrams may show up. I do not make beautiful sentences in english, so this is also a way of practicing my english skills, so please comment on grammar and typos too &#8211; &#8220;my english is Willy bath!&#8221; <img src='http://blog.hjaelpmignu.dk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>My intention is to gather enough relevant material to publish it in some form, and nail down some teaching concepts along the way. My students will then learn how to program in a way that makes them less independent of the platform they are programming on.</p>
<p>Ideas are highly appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2012/02/agnostic-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapping coordinates from one range to another in ActionScript 3.0</title>
		<link>http://blog.hjaelpmignu.dk/2011/11/mapping-coordinates-from-one-range-to-another-in-actionscript-3-0/</link>
		<comments>http://blog.hjaelpmignu.dk/2011/11/mapping-coordinates-from-one-range-to-another-in-actionscript-3-0/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 11:51:57 +0000</pubDate>
		<dc:creator>ockley</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Tips og tricks]]></category>
		<category><![CDATA[Tutorial og artikler]]></category>
		<category><![CDATA[undervisning]]></category>
		<category><![CDATA[www.hjaelpmignu.dk]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[mapping]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://blog.hjaelpmignu.dk/?p=389</guid>
		<description><![CDATA[I am very fond of Processing, and the way it easily makes it possible to experiment with design ideas. One of the powerful features are it&#8217;s map() function, that takes a value and remaps it from one range to another. I haven&#8217;t seen a similar function in the ActionScript 3.0 reference, so I decided to make one ...]]></description>
			<content:encoded><![CDATA[<p>I am very fond of Processing, and the way it easily makes it possible to experiment with design ideas. One of the powerful features are it&#8217;s<a title="map function" href="http://processing.org/reference/map_.html"> map()</a> function, that takes a value and remaps it from one range to another. I haven&#8217;t seen a similar function in the ActionScript 3.0 reference, so I decided to make one to my own utils-library, because it is something the can come in handy in many situations. First I looked at using the Point() class, and interpolation to describe the ratios, but actually found it easier to just do it with regular math.</p>
<p>I haven&#8217;t looked at performance yet, and there is possibly a lot of ways to optimize this code &#8211; this article can be a start for further fine tuning. The helper function looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span>val<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> inMin<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> inMax<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> outMin<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> outMax<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> decimals<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> returnVal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// Sets the total range for both in and out</span>
<span style="color: #6699cc; font-weight: bold;">var</span> inRange<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = inMax <span style="color: #000066; font-weight: bold;">-</span> inMin<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> outRange<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = outMax <span style="color: #000066; font-weight: bold;">-</span> outMin<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//calculates the ratio for the value in relation to the in range</span>
<span style="color: #6699cc; font-weight: bold;">var</span> ratio<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>val <span style="color: #000066; font-weight: bold;">-</span> inMin<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span>inRange<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #009900; font-style: italic;">//makes sure that the value is within range</span>
val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>inMin<span style="color: #000066; font-weight: bold;">,</span> val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>inMax<span style="color: #000066; font-weight: bold;">,</span> val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #009900; font-style: italic;">//Finds the same ratio in the out range and shift it into place</span>
returnVal = <span style="color: #000000;">&#40;</span>outRange <span style="color: #000066; font-weight: bold;">*</span> ratio<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> outMin<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//rounding of the decimals</span>
<span style="color: #6699cc; font-weight: bold;">var</span> factor<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pow</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span>decimals<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
returnVal = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>returnVal<span style="color: #000066; font-weight: bold;">*</span>factor<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span>factor<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">return</span> returnVal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The function takes the the following arguments:</p>
<ul>
<li>val: the number to be remapped</li>
<li>inMin and inMax: The original range, in which the value is measures in relation to.</li>
<li>outMin and outMax: The destination range, that the value will be re-mapped to</li>
<li>decimals: sets the precision in decimals (optional)</li>
</ul>
<div>First it calculates the ranges with</div>
<div>
<pre>var inRange:Number = inMax - inMin;
var outRange:Number = outMax - outMin;</pre>
</div>
<div>These variables are defining the total range for both the incoming number, and the range of number is eventually will be placed within.</div>
<div>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>inMin<span style="color: #000066; font-weight: bold;">,</span> val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>inMax<span style="color: #000066; font-weight: bold;">,</span> val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

</div>
<div>In this function I have decided to make sure, that the value is within the boundaries of the inRange. If i took the value 4, and sets the range of number to be within 10 and 100, I would get some unexpected results. You could treat this differently, or even make it possible by omitting these lines.</div>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> ratio<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>val <span style="color: #000066; font-weight: bold;">-</span> inMin<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span>inRange<span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<div>ratio holds a number between 0 and 1 that defines where in the original range it is located. The closer to zero it gets, the lower the value &#8211; closer to one goes towards inMax.</div>
<pre>returnVal = (outRange * ratio) + outMin;</pre>
<div>Then the number is multiplied to the outRange to find the same relative point in the target range, and finally shifted into place by adding the outMin value. Negative values are treated fine in these ranges &#8211; when you add -200 to something, it is actually subtracted from the number.</div>
<div>
<pre>var factor:int = Math.pow(10,decimals);
returnVal = Math.round(returnVal*factor)/factor;</pre>
<div>The last part of the function is meant to round of the values. For performance, this could easily be removed, but it may be useful in some cases to have odd long numbers rounded and presented with only a few digits precition. First, the variable factor holds the value to be used in the rounding proces. For 1 digit it should be &#8220;10&#8243;, 2 digits is &#8220;100&#8243;, 3 is &#8220;1000&#8243; and so forth. That value is used in the line below, where it first multiplies the final value by the factor and then rounds it to an integer. Then it divides with the factor again. It goes something like this:</div>
</div>
<div>
<ol>
<li>factor is 1000 (3 digits)</li>
<li>value is <strong>134.744637</strong></li>
<li>Multiplication results in<strong> 134744.637</strong></li>
<li>Rounding returns <strong>134745</strong></li>
<li>divided by 1000 gives the result <strong>134.745</strong></li>
</ol>
<div>Finally the value is returned as a Number.</div>
</div>
<h4>Optimization</h4>
<div>There are a lot of code shortening, that can produce a speed improvement. You could also work with int or uint exclusively to gain some performance i suppose. Finally, you can remove the decimals and the lines that check if the number is in range. I have left the to show what functionality that could come in handy.</div>
<h4>An example</h4>
<div>As a practical example of the map() function you can  copy the following code into a Flash document. Here the map() fuction is directly inside the main code, but you could start building your own utils-library with the helper functions as public static function for easier access.</div>
<div>The code draws two squares with different sizes. When the mouse moves over the small square, it draw lines in the larger square, while maintaining the relative coordinates inside the squares.</div>
<div>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//make the small square and color it dark grey</span>
<span style="color: #6699cc; font-weight: bold;">var</span> inArea<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
inArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x333333<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
inArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
inArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>inArea<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//make the large square and color it black</span>
<span style="color: #6699cc; font-weight: bold;">var</span> outArea<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x000000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> 0xFFFFFF<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.3</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>outArea<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = inArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//adds a listener for the small square, that listens for mouse movement</span>
inArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_MOVE</span><span style="color: #000066; font-weight: bold;">,</span> updateDrawing<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #339966; font-weight: bold;">function</span> updateDrawing<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//Map the local coordinates from the mouse movement to the size of the large square</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> targetX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">localX</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> targetY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">localY</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">target</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//Draws the line in the large square.</span>
	outArea<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span>targetX<span style="color: #000066; font-weight: bold;">,</span> targetY<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//Helper function</span>
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">map</span><span style="color: #000000;">&#40;</span>val<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> inMin<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> inMax<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> outMin<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> outMax<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span> decimals<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> returnVal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Sets the total range for both in and out</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> inRange<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = inMax <span style="color: #000066; font-weight: bold;">-</span> inMin<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> outRange<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = outMax <span style="color: #000066; font-weight: bold;">-</span> outMin<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//makes sure that the value is within range</span>
	val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">max</span><span style="color: #000000;">&#40;</span>inMin<span style="color: #000066; font-weight: bold;">,</span>val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	val = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">min</span><span style="color: #000000;">&#40;</span>inMax<span style="color: #000066; font-weight: bold;">,</span>val<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//calculates the ratio for the value in relation to the in range</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> ratio<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span>val <span style="color: #000066; font-weight: bold;">-</span> inMin<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> inRange<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//Finds the same ratio in the out range and shift it into place</span>
	returnVal = <span style="color: #000000;">&#40;</span>outRange <span style="color: #000066; font-weight: bold;">*</span> ratio<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> outMin<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">//rounding of the decimals</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> factor<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">pow</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span>decimals<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	returnVal = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>returnVal<span style="color: #000066; font-weight: bold;">*</span>factor<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">/</span>factor<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> returnVal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.hjaelpmignu.dk/2011/11/mapping-coordinates-from-one-range-to-another-in-actionscript-3-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

