<?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>dndigital.net &#187; Free Tutorials</title>
	<atom:link href="http://dndigital.net/category/free-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://dndigital.net</link>
	<description>Notes on Adobe Flash, Flex, AIR Development</description>
	<lastBuildDate>Mon, 07 May 2012 15:12:56 +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>Brand new HTML5 video tutorial!</title>
		<link>http://dndigital.net/2010/09/brand-new-html5-video-tutorial/</link>
		<comments>http://dndigital.net/2010/09/brand-new-html5-video-tutorial/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 10:27:55 +0000</pubDate>
		<dc:creator>Kat</dc:creator>
				<category><![CDATA[Free Tutorials]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=268</guid>
		<description><![CDATA[Hi everyone! Check out our new video for HTML 5 at: http://www.youtube.com/watch?v=hhTQPcqLWTI You can find all our free video tutorials on our YouTube Channel. Feel free to subscribe to keep up-to-date with our future video releases! http://www.youtube.com/DNDigital Feel free to ask questions, leave comments or request further material. Enjoy!]]></description>
			<content:encoded><![CDATA[<p>Hi everyone!</p>
<p>Check out our new video for HTML 5 at: <a href="http://www.youtube.com/watch?v=hhTQPcqLWTI" target="_blank">http://www.youtube.com/watch?v=hhTQPcqLWTI</a></p>
<p>You can find all our free video tutorials on our YouTube Channel. Feel free to subscribe to keep up-to-date with our future video releases!</p>
<p><strong></strong><span style="font-size: x-small"> </span></p>
<p><strong><a href="http://www.youtube.com/DNDigital" target="_blank">http://www.youtube.com/DNDigital</a></strong></p>
<p><strong>Feel free to ask questions, leave comments or request further material.</strong></p>
<p>Enjoy!</p>
<p><span style="font-size: x-small"><strong><br />
</strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/09/brand-new-html5-video-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced Actionscript Tutorial &#8211; The Abstract Factory Design Pattern</title>
		<link>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/</link>
		<comments>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 11:17:58 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Free Tutorials]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=180</guid>
		<description><![CDATA[Although it wasn&#8217;t always the case, these days a good understanding of object oriented programming principles and the use of common design patterns are essential in order to become a successful Actionscript developer. When we&#8217;re working on projects, the Head First Design Patterns book is never far away &#8211; and one of the patterns that [...]]]></description>
			<content:encoded><![CDATA[<p>Although it wasn&#8217;t always the case, these days a good understanding of object oriented programming principles and the use of common design patterns are essential in order to become a successful Actionscript developer. When we&#8217;re working on projects, the <a href="http://www.amazon.co.uk/Head-First-Design-Patterns-Freeman/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1278608410&amp;sr=8-1" target="_blank">Head First Design Patterns</a> book is never far away &#8211; and one of the patterns that has been incredibly useful time and time again is the Abstract Factory and variations thereof, so I thought we&#8217;d dedicate a YouTube tutorial to it.</p>
<p>I&#8217;d love to hear about your own experiences in regards to object oriented programming in AS3 &#8211; please leave a comment!</p>
<p>The material in this blog post and in the video is an excerpt from our <a href="http://dndigital.net/training/advanced-as3-oop-patterns/">Advanced Actionscript 3 (Object Oriented Programming and Design Patterns) training course</a>.</p>
<p>You can watch the video and read up on the details after the break.</p>
<p><span id="more-180"></span></p>
<h2>Making Choices</h2>
<p>The Abstract Factory Design Pattern is all about choice. Not any kind of choice mind you &#8211; but very specifically choosing which class to instantiate given certain conditions. Whether you realise it or not, you are probably making these choices numerous times in your own programming code. Here are some examples:</p>
<ul>
<li>Which type of menu to display based on the admin privileges of the current user</li>
<li>Which type of skin to apply to your game characters, based on the selections made at the beginning of the game &#8211; and subsequently which types of weapons are available for that character</li>
<li>Which type of button graphic to use based on the size of a banner</li>
<li>Which type of XML parser to use based on the type of feed the user has loaded in</li>
</ul>
<p>and so on. Once you&#8217;re dealing with object oriented programming, most of those choices are going to result in a class being instantiated and the trick is to choose the correct class based on a set of circumstances.</p>
<p>No problem, right? You just whack an if-statement in there:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">menu</span> = <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">menu</span> = <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Weeds Everywhere</h2>
<p>Of course, using if-statements will work fine, however it spreads the choices throughout your application, like a weed taking a strong-hold in your garden, then your front lawn, then your walls&#8230;you get the picture. Suppose you introduce a third user privilege level? Time to revisit all those many if-statements (of course you&#8217;ll remember where they are!). The more complex your project, the more difficult, time-consuming and error prone such a change becomes.</p>
<h2>Factory Pattern to the Rescue</h2>
<p>It is in these cases that the Factory Design Pattern comes to your rescue, by moving the choice into a single, isolated piece of code. The resulting class that is instantiated &#8211; the factory &#8211; will then provide the application with the correct class instances. As a result, you only need to change the single point-of-choice and create a new factory class in order to make wide-ranging changes to your entire application.</p>
<p>In practice, it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// isolated choice code (eg. after user logs in)</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> AdminUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> NormalUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the AdminUserFactory class:</span>
<span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the NormalUserFactory class:</span>
<span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// much later, in a remote location in your code-tree</span>
<span style="color: #0066CC;">menu</span> = factory.<span style="color: #006600;">getMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Can you see how you would add a third user level &#8211; and how much more manageable it has become compared to the previous example?</p>
<p>Let&#8217;s follow the example code through to its logical conclusion. Say we have 10 user levels. Then we&#8217;ll end up with 10 different factory classes (AdminUserFactory, NormalUserFactory, SuperUserFactory, etc). So far so good. But how does the code that is actually using the factory to retrieve the menu know how to deal with all those 10 different classes without using another if-statement? Surely that will lead to type-errors if we don&#8217;t know the exact data-type?</p>
<h2>Interfacing with the Factory</h2>
<p>In order to allow code throughout your application to use different factory classes, we&#8217;re going to use an interface to define what methods a factory class provides. This interface will be used as the data-type when passing the factory around to other pieces of code. So in practice it doesn&#8217;t matter which factory class is used behind the scenes and dependencies within your code are reduced to a single interface class, which is what we like to see!</p>
<p>Our user-menu example seems to use factory to create its user-interface elements, so let&#8217;s create an interface called IUIFactory. We start with just a single function &#8211; we can always add to it later on:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// in the IUIFactory.as file</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IUIFactory <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As a rule, you&#8217;d start with your interface first, then create your first factory class and then create your choice code &#8211; otherwise you&#8217;ll end up in a bit of a pickle.</p>
<p>Here is the sample code from earlier, with the interface thrown in.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// we start with an interface</span>
<span style="color: #000000; font-weight: bold;">var</span> factory:IUIFactory;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> AdminUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> NormalUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the AdminUserFactory class - which implements IUIFactory</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the NormalUserFactory class - which also implements IUIFactory</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// much later, in a remote location in your code-tree</span>
<span style="color: #000000; font-weight: bold;">var</span> factory:IUIFactory;
<span style="color: #0066CC;">menu</span> = factory.<span style="color: #006600;">getMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Splendid.</p>
<p>Another beneficial side-effect of using an interface in this way comes into play when adding new features to your factory. Any new feature requires a new method in your factory interface. That new method will immediately result in compiler errors because none of your factory classes has implemented it yet. You have no option but to implement the new method in every single factory class &#8211; hence automatically making sure that your code is properly maintained.</p>
<p>Doesn&#8217;t that sound better than finding a long-lost if-statement deep down in the murky depths of your code-tree that your colleague forgot to change when they added a new user-group?</p>
<h2>YouTube Tutorial and Source Code</h2>
<p>The YouTube tutorial steps through a different example of coding a user-interface that can change depending on the age-group of the viewer.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/spfienbiqqs&amp;hl=en_GB&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/spfienbiqqs&amp;hl=en_GB&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>You can <a title="Factory Design Pattern Source Code" href="http://dndigital.net/wp/wp-content/uploads/2010/07/dndigital-abstract-factory.zip">download the source-code as a FlashBuilder 4 project</a> so that you can follow the tutorial and experiment further. There are a couple of extra images in the assets folder so you can produce a third factory class. Copyright / licensing info is included in each file, all images used are publicly available via <a href="http://sxc.hu" target="_blank">sxc.hu</a>.</p>
<h2>Further Thoughts &#8211; Factory Pattern and Dependency Injection</h2>
<p>The Factory Design Pattern&#8217;s strengths come from isolating choices in a single location. Furthermore, each factory class also encapsulates a very simple set of actions &#8211; it knows how to create the correct instances but doesn&#8217;t require knowledge of the surrounding application to do so &#8211; so we are adhering to the  &#8217;one class &#8211; one responsibility&#8217; concept.</p>
<p>Of course you still need to pass the actual factory instance around to the classes that require them and there are various ways of managing these dependencies. In the YouTube tutorial, I simply pass the factory instance to the view classes via their constructor and this is a perfectly valid way of handling it.</p>
<p>There are of course other options &#8211; and Dependency Injection (DI) is especially helpful when working with the factory pattern.</p>
<p>Since DI hinges in principal around matching interface data-types to concrete classes, switching factory classes becomes a doddle. You simply specify the appropriate factory class to be used when the DI container encounters the factory interface and your project can be dramatically changed by essentially touching only a single line of code. This is especially true when there is a fixed, pre-determined choice of factory class, for example when repurposing an entire project for a different market or target audience.</p>
<p>I&#8217;d love to hear your own experiences of the Factory Pattern and OOP in general &#8211; leave a note in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using XML with Actionscript 3 &#8211; Tutorial now on YouTube</title>
		<link>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/</link>
		<comments>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 18:24:20 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Free Tutorials]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=189</guid>
		<description><![CDATA[Are you uncertain of how to use XML data in Actionscript 3 and Flash? If so, you&#8217;re in luck because we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in two parts and guides you through how to load and use XML data in Actionscript 3. In particular, the tutorial works with [...]]]></description>
			<content:encoded><![CDATA[<p>Are you uncertain of how to use XML data in Actionscript 3 and Flash? If so, you&#8217;re in luck because we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in two parts and guides you through how to load and use XML data in Actionscript 3. In particular, the tutorial works with an RSS feed &#8211; but of course the techniques apply to all types of XML data.</p>
<p>Here are some of the points covered:</p>
<ul>
<li>loading an XML file in Actionscript 3</li>
<li>extracting particular data from the XML file</li>
<li>extracting lists of tags from the XML file</li>
<li>working with namespaces</li>
<li>using filtering expressions</li>
</ul>
<p>It&#8217;s all pretty straightforward once you&#8217;ve mastered the technique &#8211; come and take a look.</p>
<p>View the videos after the break &#8211; or simply <a title="DN Digital YouTube Channel" href="http://youtube.com/dndigital" target="_blank">visit our YouTube channel</a>.</p>
<p><span id="more-189"></span>The tutorial comes in 3 parts, it&#8217;s probably best to watch them back to back. Total viewing time comes to around 14 minutes.</p>
<p>The videos are available in full 720p HD format on YouTube &#8211; link underneath each embedded video.</p>
<p>Enjoy!</p>
<p><object width="579" height="351"><param name="movie" value="http://www.youtube.com/v/t49NRfwpxl8&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/t49NRfwpxl8&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="579" height="351"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=t49NRfwpxl8">Part 1 &#8211; Available in HD on YouTube</a></p>
<p><object width="580" height="351"><param name="movie" value="http://www.youtube.com/v/R-Ub5ZqGpzk&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/R-Ub5ZqGpzk&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="580" height="351"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=R-Ub5ZqGpzk">Part 2 &#8211; Available in HD on YouTube</a></p>
<p>Enjoy &#8211; and please let us know in the comments what you&#8217;d like to learn next!</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript Maths Tutorial &#8211; Relative Positioning and Centring</title>
		<link>http://dndigital.net/2010/06/actionscript-maths-positioning-centring/</link>
		<comments>http://dndigital.net/2010/06/actionscript-maths-positioning-centring/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 18:49:04 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Free Tutorials]]></category>
		<category><![CDATA[Beginner]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=126</guid>
		<description><![CDATA[It&#8217;s Friday again &#8211; and that means we&#8217;ve just uploaded another Actionscript training video to YouTube! This one forms the beginning of a series on Maths-based Actionscript programming and looks at positioning and centring visual elements on screen. Think of it as something gentle to get you started&#8230; I really love these topics and it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s Friday again &#8211; and that means we&#8217;ve just uploaded another Actionscript training video to YouTube! This one forms the beginning of a series on Maths-based Actionscript programming and looks at positioning and centring visual elements on screen. Think of it as something gentle to get you started&#8230;</p>
<p>I really love these topics and it&#8217;s an area that at first leaves quite a few people stumped on our <a href="http://dndigital.net/training/actionscript-introduction/">Introduction to Actionscript Training Course</a> so it seemed like an excellent candidate for another training video.</p>
<p>We&#8217;d also love to hear about what kind of maths causes you difficulties when writing Actionscript  - please leave a comment.</p>
<p>View the video after the break, along with further tips and tricks.</p>
<p><span id="more-126"></span>Although it&#8217;s not exactly glamorous, writing code to position elements on screen is perhaps one of the most common tasks for Actionscript development. Pretty much every project requires it. That&#8217;s the same whether you are developing in the Flash IDE or creating a Flex application. More often than not you&#8217;ll also be required to adjust the layout relative to the width and height of the SWF &#8211; and this is where some simple mathematics work wonders.</p>
<p>Take a look at the YouTube tutorial &#8211; or <a href="http://www.youtube.com/watch?v=UUylDqWMvQA" target="_blank">view it on YouTube</a> &#8211; and then read the rest of the post for a more in-depth look at the topic.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/UUylDqWMvQA&amp;hl=en_GB&amp;fs=1&amp;rel=0&amp;hd=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube-nocookie.com/v/UUylDqWMvQA&amp;hl=en_GB&amp;fs=1&amp;rel=0&amp;hd=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<h2>Remember the registration point</h2>
<p>When you write positioning code, it&#8217;s vital to consider the registration point of your display object. You&#8217;ll need slightly different calculations depending on where it is. This is because setting the x and y values moves the display object so that the registration point is positioned at those coordinates.</p>
<blockquote><p>Note that the registration point only really comes to bear in Flash &#8211; when you&#8217;re working with external code-editors and graphics that are loaded or created with code, your registration point is generally in the top-left corner.</p></blockquote>
<p>It&#8217;s easy to see the effect the registration point:</p>
<ul>
<li>Create a MovieClip from any kind of shape, place it on the stage and give it an instance name (&#8216;mc&#8217; will do for now).</li>
<li>Write a line of code that positions the MovieClip instance at a certain location:</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">200</span>;
mc.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">200</span>;</pre></div></div>

<ul>
<li>Publish and see where the MovieClip ends up.</li>
<li>Now go back into the MovieClip symbol (double-click on it on the stage) and simply move the graphics around a little. This in effect changes the registration point.</li>
<li>Re-publish and compare the different positions.</li>
</ul>
<p>Once you are aware of the importance of the registration point, everything else suddenly becomes very straightforward.</p>
<h2>Calculating stage dimensions</h2>
<p>To do any kind of relative positioning or centring, you&#8217;ll need to be able to retrieve the width and height of the SWF. This is straightforward via the stage object:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The stage width is: &quot;</span>, <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The stage height is: &quot;</span>, <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That is actually pretty easy, isn&#8217;t it?</p>
<blockquote><p>stage.stageWidth and stage.stageHeight also work in Flex and Actionscript-only projects &#8211; but you must make sure that your code is in a DisplayObject which has already been added to the stage, otherwise you&#8217;ll get a null object exception. There are events for that kind of thing but that&#8217;s another story.</p></blockquote>
<h2>Right-aligned objects</h2>
<p>So now you know the dimensions of the stage &#8211; how do you align an object to the right-hand edge? This is done in three easy steps &#8211; and we&#8217;ll assume that the registration point is in the top-left hand corner for now.</p>
<p>We&#8217;ll continue with the &#8216;mc&#8217; MovieClip instance from the previous example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mc.<span style="color: #0066CC;">width</span> - <span style="color: #cc66cc;">10</span>; <span style="color: #808080; font-style: italic;">// 10px margin</span></pre></div></div>

<p>Not sure why that works? Well, either consider the little diagram below or grab a pencil and try to sketch it.</p>
<p><a href="http://dndigital.net/wp/wp-content/uploads/2010/06/positioning_right.png"><img class="alignnone size-full wp-image-135" title="positioning_right" src="http://dndigital.net/wp/wp-content/uploads/2010/06/positioning_right.png" alt="" width="560" height="340" /></a></p>
<p>I find that visualising this scenario in my head or scribbling on a bit of paper works wonders for finding the right positioning code.</p>
<h2>Centring</h2>
<p>Never mind writing Actionscript &#8211; finding the right spelling for that word was a whole different ball-game! If you&#8217;re reading this from the USA, you might prefer &#8220;Centering&#8221;&#8230;</p>
<p>Anyway, one of the most frequent positioning tasks is to centre objects either relative to one another or relative to the entire SWF. The same techniques we used for relative positioning above also work here!</p>
<p>Centring with a top-left registration point looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = <span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> - mc.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
mc.<span style="color: #006600;">y</span> = <span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> - mc.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>Not sure how it works? Again, scribble on a piece of paper or consider the following diagram:</p>
<p><a href="http://dndigital.net/wp/wp-content/uploads/2010/06/positioning_centre.png"><img class="alignnone size-full wp-image-142" title="positioning_centre" src="http://dndigital.net/wp/wp-content/uploads/2010/06/positioning_centre.png" alt="" width="560" height="340" /></a></p>
<p>Centring with a middle registration point looks like this. It&#8217;s a little more straightforward, so if you know the object will be centred, you can choose your registration point accordingly.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageWidth</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
mc.<span style="color: #006600;">y</span> = <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">stageHeight</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>And, finally, if you need to centre one object relative to another, you can use exactly the same technique &#8211; just replace the stage object with another MovieClip!</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = otherMc.<span style="color: #006600;">x</span> + <span style="color: #66cc66;">&#40;</span> otherMc.<span style="color: #0066CC;">width</span> - mc.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;
mc.<span style="color: #006600;">y</span> = otherMc.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span> otherMc.<span style="color: #0066CC;">height</span> - mc.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span>;</pre></div></div>

<p>And, as a final little twist, if you need to ensure that the resulting position will always sit on a full pixel, you can simply round the result:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">mc.<span style="color: #006600;">x</span> = otherMc.<span style="color: #006600;">x</span> + <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> otherMc.<span style="color: #0066CC;">width</span> - mc.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#41;</span>;
mc.<span style="color: #006600;">y</span> = otherMc.<span style="color: #006600;">y</span> + <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span> otherMc.<span style="color: #0066CC;">height</span> - mc.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>No more diagrams for this one &#8211; hopefully you&#8217;ll have worked it out by now!</p>
<h2>Conclusion</h2>
<p>These techniques should give you all the tools you need to create a wide range of dynamically calculated layouts. I can&#8217;t stress enough how much easier it is if you work everything out on paper first &#8211; I&#8217;ve even been known to cut little squares out and move them around to see the effect it has.</p>
<p>Still not sure what it&#8217;s all about? Leave a comment and I&#8217;ll try to help.</p>
<p>There&#8217;s more coming soon, happy coding in the meantime!</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/06/actionscript-maths-positioning-centring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript 3 Drag and Drop now on YouTube</title>
		<link>http://dndigital.net/2010/05/actionscript-3-drag-and-drop-now-on-youtube/</link>
		<comments>http://dndigital.net/2010/05/actionscript-3-drag-and-drop-now-on-youtube/#comments</comments>
		<pubDate>Fri, 21 May 2010 17:14:36 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Free Tutorials]]></category>

		<guid isPermaLink="false">http://dndigital.net/blog/?p=47</guid>
		<description><![CDATA[As part of our regular training videos, we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in three parts and guides you through creating a drag and drop shopping cart in Flash. The video covers essential Actionscript 3 skills, such as external classes, event listeners and of course drag and drop and [...]]]></description>
			<content:encoded><![CDATA[<p>As part of our regular training videos, we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in three parts and guides you through creating a drag and drop shopping cart in Flash.</p>
<p>The video covers essential Actionscript 3 skills, such as external classes, event listeners and of course drag and drop and uses object-oriented techniques.</p>
<p>View the videos after the break &#8211; or simply <a title="DN Digital YouTube Channel" href="http://youtube.com/dndigital" target="_blank">visit our YouTube channel</a>.</p>
<p><span id="more-47"></span>The tutorial comes in 3 parts, it&#8217;s probably best to watch them back to back. Total viewing time comes to around 24 minutes.</p>
<p>The videos are actually available in full 720p HD format on YouTube &#8211; link underneath each embedded video.</p>
<p>Enjoy!</p>
<p><object width="580" height="360"><param name="movie" value="http://www.youtube-nocookie.com/v/ALqGYMsRWxw&#038;hl=en_GB&#038;fs=1&#038;border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/ALqGYMsRWxw&#038;hl=en_GB&#038;fs=1&#038;border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="580" height="360"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=1hR3CVIdfuY">Part 1 &#8211; Available in HD on YouTube</a></p>
<p><object width="580" height="360"><param name="movie" value="http://www.youtube-nocookie.com/v/1hR3CVIdfuY&#038;hl=en_GB&#038;fs=1&#038;border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/1hR3CVIdfuY&#038;hl=en_GB&#038;fs=1&#038;border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="580" height="360"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=ALqGYMsRWxw">Part 2 &#8211; Available in HD on YouTube</a></p>
<p><object width="580" height="360"><param name="movie" value="http://www.youtube-nocookie.com/v/g1kvSTHkbtQ&#038;hl=en_GB&#038;fs=1&#038;border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/g1kvSTHkbtQ&#038;hl=en_GB&#038;fs=1&#038;border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="580" height="360"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=g1kvSTHkbtQ">Part 3 &#8211; Available in HD on YouTube</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/05/actionscript-3-drag-and-drop-now-on-youtube/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

