<?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>Emanuele Feronato</title>
	<atom:link href="http://www.emanueleferonato.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 02 Sep 2010 09:32:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Creation of new data types in AS3: Linked List</title>
		<link>http://www.emanueleferonato.com/2010/09/02/creation-of-new-data-types-in-as3-linked-list/</link>
		<comments>http://www.emanueleferonato.com/2010/09/02/creation-of-new-data-types-in-as3-linked-list/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 09:32:52 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3274</guid>
		<description><![CDATA[In computer programming, a data type (or datatype) is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, stating the possible values for that type, the operations that can be done on that type, and the way the values of that type are stored. (source: Wikipedia). Obviously any language [...]]]></description>
			<content:encoded><![CDATA[<p>In computer programming, a data type (or datatype) is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, stating the possible values for that type, the operations that can be done on that type, and the way the values of that type are stored. (source: <a href="http://en.wikipedia.org/wiki/Data_type" target = "_blank">Wikipedia</a>).</p>
<p>Obviously any language is capable to create new data structures. I did not find any complete AS3 complete tutorial to create a new data type from scratch with AS3, so I am writing it by myself.</p>
<p>We are going to define a simple <strong>linked list</strong> data type.</p>
<p>A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.</p>
<p><img src="/wp-content/uploads/2010/09/408px-Singly-linked-list.svg_.png" /></p>
<p>(source and image: <a href="http://en.wikipedia.org/wiki/Linked_list" target = "_blank">Wikipedia</a>. Yes, I made a donation so I feel free to copy/paste :)).</p>
<p>In this example, we are going to reproduce the same linked list you see in the picture. Usually the &#8220;records&#8221; Wikipedia is talking about are called &#8220;nodes&#8221;, so we&#8217;ll define a node datatype.<span id="more-3274"></span></p>
<p>This is the class, in a file called <code>node.as</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> triqui<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> node <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> node_data<span style="color: #000000; font-weight: bold;">:*</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> next_node<span style="color: #000000; font-weight: bold;">:</span>node;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> node<span style="color: #000000;">&#40;</span>node_content<span style="color: #000000; font-weight: bold;">:*</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			node_data=node_content;
			next_node=<span style="color: #0033ff; font-weight: bold;">null</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> get_node_data<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> node_data;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> insert_prev<span style="color: #000000;">&#40;</span>n<span style="color: #000000; font-weight: bold;">:</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			n.next_node=<span style="color: #0033ff; font-weight: bold;">this</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> insert_next<span style="color: #000000;">&#40;</span>n<span style="color: #000000; font-weight: bold;">:</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			next_node=n;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> get_next_node<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>node <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> next_node;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 1</strong>: package definition. Notice there is a name after the package to preserve class name uniqueness. Whithout boring you with useless theory, just remember that since package name is <code>triqui</code>, you will need to save it in a <code>triqui</code> folder in the same path of you main <code>.fla</code> file. Refer to the downloadable source code available at the end of the post for a concrete example.</p>
<p><strong>Line 2</strong>: definition of the class. It&#8217;s called <code>node</code>.</p>
<p><strong>Line 3</strong>: <code>node_data</code> is the container of the information we need to store. Referring to Wikipedia example, the numbers <code>12</code>, <code>99</code> and <code>37</code>. Giving <code>*</code> as data type makes it capable of having any data type, such as strings, arrays, integers or even other nodes.</p>
<p><strong>Line 4</strong>: <code>next_node</code> is the link to the next node in the list and obviously is a <code>node</code> type.</p>
<p><strong>Line 5</strong>: the main function, or constructor. It wants the content of the node (<code>12</code>, <code>99 </code> or<code>37</code> in the example) as argument. Notice the type is <code>*</code> because we said it will accept any kind of data types.</p>
<p><strong>Line 6</strong>: assigning <code>node_content</code> (the constructor argument) value to <code>node_data</code> (the information container of the node).</p>
<p><strong>Line 7</strong>: assigning <code>null</code> to <code>next_node</code>. The node will not link any other node.</p>
<p><strong>Line 9</strong>: function to retrieve the information stored in a node (notice the <code>*</code>: you should know why).</p>
<p><strong>Line 10</strong>: simply returning the content of <code>node_data</code> variable.</p>
<p><strong>Line 12</strong>: function to insert the previous node to a node, that is the node in the argument will link the current node. </p>
<p><strong>Line 13</strong>: assigning the current node to <code>next_node</code> variable of the node passed in the argument.</p>
<p><strong>Line 15</strong>: function to insert the next node to a node, that is the node in the argument will be linked by the current node. </p>
<p><strong>Line 16</strong>: assigning the node passed as argument to <code>next_node</code> variable</p>
<p><strong>Line 18</strong>: function to get the next node of a node. Returns (obviously) a node.</p>
<p><strong>Line 19</strong>: returning <code>next_node</code> content</p>
<p>And this is an example of the class at work&#8230; in a file called <code>datatype.as</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> triqui.node;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> datatype extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> datatype<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> n1<span style="color: #000000; font-weight: bold;">:</span>node=<span style="color: #0033ff; font-weight: bold;">new</span> node<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">12</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> n2<span style="color: #000000; font-weight: bold;">:</span>node=<span style="color: #0033ff; font-weight: bold;">new</span> node<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">99</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> n3<span style="color: #000000; font-weight: bold;">:</span>node=<span style="color: #0033ff; font-weight: bold;">new</span> node<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span>;
			n2.insert_prev<span style="color: #000000;">&#40;</span>n1<span style="color: #000000;">&#41;</span>;
			n2.insert_next<span style="color: #000000;">&#40;</span>n3<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>n1.get_next_node<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.get_next_node<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.get_node_data<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 3</strong>: importing node library</p>
<p><strong>Lines 6-8</strong>: creating the three nodes as in the example</p>
<p><strong>Line 9</strong>: setting <code>n1</code> node as the previous of <code>n2</code></p>
<p><strong>Line 10</strong>: setting <code>n3</code> node as the next of <code>n2</code></p>
<p><strong>Line 11</strong>: This will output the content of <code>n3</code> node starting from <code>n1</code>&#8230; <code>37</code>.</p>
<p><a href="/wp-content/uploads/2010/09/datatype.zip">Download the source code</a>.</p>
<p>During next days we&#8217;ll see various more complex data types such as double linked lists and trees.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/09/02/creation-of-new-data-types-in-as3-linked-list/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flash Games Market Survey &#8211; 2010 edition</title>
		<link>http://www.emanueleferonato.com/2010/08/27/flash-games-market-survey-2010-edition/</link>
		<comments>http://www.emanueleferonato.com/2010/08/27/flash-games-market-survey-2010-edition/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 19:49:08 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Monetize]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3270</guid>
		<description><![CDATA[After the huge success of Flash Games Market Survey, Mochi Media in partnership with Flash Game License and Adobe present 2010 Flash Games Market Survey. Last year&#8217;s survey showed just how vibrant the space is, it&#8217;s time to give some information again to help Mochi and partners to improve Flash gaming world. In addition, you [...]]]></description>
			<content:encoded><![CDATA[<p>After the huge success of <a href="http://www.emanueleferonato.com/2009/07/08/flash-games-market-survey/">Flash Games Market Survey</a>, <a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank">Mochi Media</a> in partnership with <a href="http://www.flashgamelicense.com/" target = "_blank">Flash Game License</a> and <a href="http://www.adobe.com/" target ="_blank">Adobe</a> present <strong><a target = "_blank" href="http://www.surveymonkey.com/s/fgms2010">2010 Flash Games Market Survey</a></strong>.</p>
<p><a target = "_blank" href="http://www.surveymonkey.com/s/fgms2010"><img src="/wp-content/uploads/2010/08/c7bef074-60d4-4179-b581-991c1b1a7013.png" /></a></p>
<p>Last year&#8217;s survey showed just how vibrant the space is, it&#8217;s time to give some information again to help Mochi and partners to improve Flash gaming world. In addition, you can also win one of two $50 Amazon gift cards.</p>
<p>This survey is completely anonymous. Thanks for your time!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/27/flash-games-market-survey-2010-edition/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tic Tac Toe Artificial Intelligence</title>
		<link>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/</link>
		<comments>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 09:29:51 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3260</guid>
		<description><![CDATA[During these days I played a bit with Artificial Intelligence for a project I am working on (big news on the horizon) and I&#8217;m coming with a brief introduction to the simplest game you can use to test artificial intelligence. Tic Tac Toe. It&#8217;s a pencil-and-paper game for two players, O and X, who take [...]]]></description>
			<content:encoded><![CDATA[<p>During these days I played a bit with Artificial Intelligence for a project I am working on (big news on the horizon) and I&#8217;m coming with a brief introduction to the simplest game you can use to test artificial intelligence. Tic Tac Toe.</p>
<p>It&#8217;s a pencil-and-paper game for two players, O and X, who take turns marking the spaces in a 3×3 grid, usually X  going first. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game. (source: <a href="http://en.wikipedia.org/wiki/Tic-tac-toe" target = "_blank">Wikipedia</a>).</p>
<p>But most of all, it&#8217;s a good playground to apply artificial intelligence for four reasons:</p>
<p>It&#8217;s a <strong>perfect information game</strong>: all players know all moves that have taken place.</p>
<p>It&#8217;s a <strong>sequential game</strong>: when a player moves, the other player waits. And have the waiting player see the moves of the playing one.</p>
<p>It&#8217;s a <strong>zero sum game</strong>: if one player wins, the ohter loses. No cooperation. </p>
<p>It&#8217;s a <strong>non-random game</strong>: there isn&#8217;t any randomizing element such as dices, dealing of cards, and so on. Non-random games are pure strategy.</p>
<p><strong>How many board configurations?</strong></p>
<p>Now, we need to know how many boards configurations can exist in a Tic Tac Toe game. We&#8217;ll do it using php. We&#8217;ll code an empty board like a string with nine points (<code>.........</code>). Every point represents an empty cell. Then, with a recursive function, we&#8217;ll generate all possible board combinations, assuming the first player puts an <code>X</code> and the second player uses the <code>O</code>.</p>
<p>Here it is the script:<span id="more-3260"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #339933;">,</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$boards</span><span style="color: #339933;">,</span><span style="color: #000088;">$symbols</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$board</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$app</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #000088;">$symbols</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$boards</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$symbols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;XO&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boards</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
place_move<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.........&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boards</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$empty_spots</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Combinations with &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">-</span><span style="color: #000088;">$empty_spots</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; symbol(s): <span style="color: #006699; font-weight: bold;">$values</span>&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Elapsed time: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And the result is:</p>
<p>Combinations with <strong>1</strong> symbol(s): <strong>9</strong> &#8211; easy to see, in an empty board you can place an <code>X</code> in any of the nine spots</p>
<p>Combinations with <strong>2</strong> symbol(s): <strong>72</strong> &#8211; for each of the 9 different combinations with one symbol, you can place a <code>O</code> in any of the remaining eight spots. 9*8 =72.</p>
<p>Combinations with <strong>3</strong> symbol(s): <strong>504</strong> &#8211; for each of the 72 previous combinations, you can place an X in any of the remaining seven spots. 72*7 = 504.</p>
<p>Combinations with <strong>4</strong> symbol(s): <strong>3024</strong> &#8211; 504*6</p>
<p>Combinations with <strong>5</strong> symbol(s): <strong>15120</strong> &#8211; 3024*5</p>
<p>Combinations with <strong>6</strong> symbol(s): <strong>60480</strong> &#8211; 15120*4 </p>
<p>Combinations with <strong>7</strong> symbol(s): <strong>181440</strong> &#8211; 60480*3</p>
<p>Combinations with <strong>8</strong> symbol(s): <strong>362880</strong> &#8211; 181440*2</p>
<p>Combinations with <strong>9</strong> symbol(s): <strong>362880</strong> &#8211; 362880*1. Same number as the combinations with 8 symbols, because when there are 8 symbols in game, you can only place an <code>O</code> in just one spot.</p>
<p>Elapsed time: <strong>6.0569689273834</strong> &#8211; six seconds to calculate <strong>986409</strong> different combinations. So the total possible board configurations are <strong>986410</strong>, including the empty board.</p>
<p>According to these results, we can say the number of possible combinations in a board with n symbols is <strong>9!/(9-n)!</strong>.</p>
<p><strong>How many REAL board configurations?</strong></p>
<p>Obviously there&#8217;s something wrong with this script because it does not consider when a player wins. If a player wins, the game is over and there are no more moves. So a lot of board configurations are impossible to reach.</p>
<p>So let&#8217;s modify a bit the script to check for victories and the number of necessary moves to win:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #339933;">,</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$boards</span><span style="color: #339933;">,</span><span style="color: #000088;">$wins</span><span style="color: #339933;">,</span><span style="color: #000088;">$symbols</span><span style="color: #339933;">;</span>
     <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$board</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$app</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #000088;">$symbols</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$boards</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>winning_board<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$wins</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> winning_board<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span>	<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$symbols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;XO&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boards</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$wins</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
place_move<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.........&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boards</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$empty_spots</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Combinations with &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">-</span><span style="color: #000088;">$empty_spots</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; symbol(s): <span style="color: #006699; font-weight: bold;">$values</span>&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Wins in &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$x</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; moves: <span style="color: #006699; font-weight: bold;">$wins</span>[<span style="color: #006699; font-weight: bold;">$x</span>]&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Elapsed time: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And the result is:</p>
<p>Combinations with <strong>1</strong> symbol(s): <strong>9</strong></p>
<p>Combinations with <strong>2</strong> symbol(s): <strong>72</strong></p>
<p>Combinations with <strong>3</strong> symbol(s): <strong>504</strong></p>
<p>Combinations with <strong>4</strong> symbol(s): <strong>3024</strong></p>
<p>Combinations with <strong>5</strong> symbol(s): <strong>15120</strong></p>
<p>Combinations with <strong>6</strong> symbol(s): <strong>54720</strong></p>
<p>Combinations with <strong>7</strong> symbol(s): <strong>148176</strong></p>
<p>Combinations with <strong>8</strong> symbol(s): <strong>200448</strong></p>
<p>Combinations with <strong>9</strong> symbol(s): <strong>127872</strong></p>
<p>Wins in <strong>5</strong> moves: <strong>1440</strong></p>
<p>Wins in <strong>6</strong> moves: <strong>5328</strong></p>
<p>Wins in <strong>7</strong> moves: <strong>47952</strong></p>
<p>Wins in <strong>8</strong> moves: <strong>72576</strong></p>
<p>Wins in <strong>9</strong> moves: <strong>81792</strong></p>
<p>Elapsed time: <strong>4.7160170078278</strong> &#8211; Less than five seconds to generate all possible Tic Tac Toe combinations.</p>
<p>This means <strong><code>X</code> will win</strong> 1440+47952+81792 = <strong>131184 times</strong> while <strong><code>O</code> will win</strong> 5328+72576 = <strong>77904 times</strong>. The first player has more chances to win.</p>
<p>How can we determine the drawn games? They are represented by the number of possible boards with nine symbols (127872) minus the number of wins at the 9th move (81792) = <strong>46080 draws</strong>.</p>
<p>According to these results, next time we&#8217;ll introduce an algorithm to make the computer play Tic Tac Toe.</p>
<p>Meanwhile enjoy these results.</p>
<p>PS: anyone willing to translate this into AS3 or other languages?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Create a Flash game like Blockage &#8211; Movement prototype</title>
		<link>http://www.emanueleferonato.com/2010/08/21/create-a-flash-game-like-blockage-movement-prototype/</link>
		<comments>http://www.emanueleferonato.com/2010/08/21/create-a-flash-game-like-blockage-movement-prototype/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 17:55:10 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3254</guid>
		<description><![CDATA[This is one of those days when you want to make a thing work that way FULLSTOP. And that&#8217;s it&#8230; this is the typical Blockage movement: I wanted to do it without any timeline tween or tricks to change registration point on the fly, just using localToGlobal method. If you aren&#8217;t familiar with this method, [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those days when you want to make a thing work that way FULLSTOP.</p>
<p>And that&#8217;s it&#8230; this is the typical <a href="http://www.emanueleferonato.com/2010/08/11/create-a-flash-game-like-blockage/">Blockage</a> movement:</p>
<p><embed src="/wp-content/uploads/2010/08/blkg.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="100" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>I wanted to do it without any timeline tween or tricks to <a href="http://www.emanueleferonato.com/2010/08/04/changing-a-movieclip-registration-point-on-the-fly-with-as3/">change registration point on the fly</a>, just using localToGlobal method. If you aren&#8217;t familiar with this method, read <a href="http://www.emanueleferonato.com/2010/06/22/understanding-as3-localtoglobal-method/">understanding AS3 localToGlobal method</a> first.</p>
<p>So here it is the uncommented and unoptimized prototype. While comments and tutorial will come when I&#8217;ll integrate it into the making of the game, I think this can be useful to someone of you to have a sneak peak of what&#8217;s going on.</p>
<p>This is the main class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> blkg extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> block<span style="color: #000000; font-weight: bold;">:</span>block_mc=<span style="color: #0033ff; font-weight: bold;">new</span> block_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> blkg<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			block.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">0</span>;
			block.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">50</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>block<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>and this is <code>block_mc</code> class:<span id="more-3254"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Point</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> block_mc extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> point<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> defpoint<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> rotation_dir=<span style="color: #000000; font-weight:bold;">5</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> block_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,on_enter_frame<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> on_enter_frame<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">+</span>=rotation_dir;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>rotation<span style="color: #000000; font-weight: bold;">%</span>90==<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">rotation</span>=<span style="color: #000000; font-weight:bold;">0</span>;
				steps<span style="color: #000000; font-weight: bold;">++</span>;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>steps==<span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					steps=<span style="color: #000000; font-weight:bold;">0</span>;
					rotation_dir<span style="color: #000000; font-weight: bold;">*</span>=<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>rotation_dir<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						point=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						point=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>rotation_dir<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						defpoint=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>defpoint<span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						defpoint=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>defpoint<span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> tmp_point<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Point</span>=<span style="color: #004993;">localToGlobal</span><span style="color: #000000;">&#40;</span>point<span style="color: #000000;">&#41;</span>;
			tmp_point=defpoint.<span style="color: #004993;">subtract</span><span style="color: #000000;">&#40;</span>tmp_point<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>=tmp_point.<span style="color: #004993;">x</span>;
			<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>=tmp_point.<span style="color: #004993;">y</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Just remember I am using a 50 pixel sided square with origin at <code>0,0</code> and that <code>steps==9</code> at <strong>line 18</strong> is used only to make the square go back and forth.</p>
<p><a href="/wp-content/uploads/2010/08/blkg.zip">Download the source code</a> and solve the mystery&#8230; or wait for next step.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/21/create-a-flash-game-like-blockage-movement-prototype/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Worms-like destructible terrain in Flash &#8211; Part 3</title>
		<link>http://www.emanueleferonato.com/2010/08/18/worms-like-destructible-terrain-in-flash-part-3/</link>
		<comments>http://www.emanueleferonato.com/2010/08/18/worms-like-destructible-terrain-in-flash-part-3/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 18:30:32 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3247</guid>
		<description><![CDATA[After another truck of Aspirin, Jordi Sanglas Molist is back with the 3rd step of his Worms tutorial. I&#8217;ve finished part 3. I almost got a headache. Forgive me if there are some mistakes, I wrote this post and the comments quickly. Now the character is affected by the explosions. To do that, I added [...]]]></description>
			<content:encoded><![CDATA[<p>After another truck of Aspirin, <strong>Jordi Sanglas Molist</strong> is back with the 3rd step of his Worms tutorial.</p>
<p>I&#8217;ve finished part 3. I almost got a headache. Forgive me if there are some mistakes, I wrote this post and the comments quickly.</p>
<p>Now the character is affected by the explosions. To do that, I added an horizontal speed and a friction. However, I don&#8217;t want the character to slide when I&#8217;m using the arrow keys, so:</p>
<p>* The arrow keys won&#8217;t use horizontal speed<br />
* If the horizontal speed is different than 0 (an explosion is moving the character), I can&#8217;t use the arrow keys</p>
<p>To calculate the impulse, we will need to use trigonometry: </p>
<p><img src="/wp-content/uploads/2010/08/WormsPrototype_trigonometry.png"  /></p>
<p>I&#8217;ve also solved another bug: if the character was falling (without using the space bar), it could jump in the air. Here I removed the jumping variable and I check if the character is on the ground.</p>
<p>Pay attention!!! Sometimes I check if the character is ON the ground (<code>character.y+10</code>) and sometimes I check if the character is TOUCHING the ground (<code>character.y+9</code>).</p>
<p>KNOWN BUGS:</p>
<p>* The horizontal speed won&#8217;t decrease faster or more slowly if the character is on a slope.<br />
* The <code>for</code> loop isn&#8217;t exact when the horizontal speed is a decimal number (I think).</p>
<p>What should I do in the next part? I must do it before I start school, or I won&#8217;t have time.</p>
<p>While you think about the next part to request, here it is the script:<span id="more-3247"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Bitmap</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">BitmapData</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">BlendMode</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Point</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Rectangle</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Matrix</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> worms extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> terrain_bmpd<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">550</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,0xFF00FF00<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> terrain_bmp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span>terrain_bmpd<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> character<span style="color: #000000; 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: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> x_speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> y_speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> hole<span style="color: #000000; 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: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> hole_matrix<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Matrix</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> left_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> right_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> space_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> x_diff<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//x_diff and y_diff will be the difference between the character and the explosion centre</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> y_diff<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> explosion_radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">100</span>;<span style="color: #009900;">//That's the radius of the explosion. If the character is inside the radius, it will move away.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> explosion_intensity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">10</span>;<span style="color: #009900;">//The explosion intensity is the speed (pixels/frame) that the character will</span>
		<span style="color: #009900;">//get if the distance between the explosion and the character is 0 (the closest one).</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//We will use that variable to store the distance between the character and the explosion</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//and that one to store the angle</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> applied_speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//That will be the total speed we need to apply (the vector length).</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> applied_x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//Then, we will separate the applied_speed in applied_x</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> applied_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;<span style="color: #009900;">//and applied_y</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> ground_friction<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.2</span>;<span style="color: #009900;">//The ground has more friction than the air,</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> air_friction<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0.05</span>;<span style="color: #009900;">//so I have used different variables</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> worms<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			draw_objects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,move_character<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>,mouse_up<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> move_character<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>left_key<span style="color: #000000; font-weight: bold;">&amp;&amp;</span>x_speed==<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//If the player is moving, we won't be able to control it</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">3</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">6</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.x<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>right_key<span style="color: #000000; font-weight: bold;">&amp;&amp;</span>x_speed==<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//Let's do the same with the right key...</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">3</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">5</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">++</span>;
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>x_speed<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//This block is similar; however, we don't know the speed,</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>Math.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>x_speed<span style="color: #000000;">&#41;</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//so the for loop will use x_speed (there's a bug, read KNOWN BUGS)</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">6</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.x<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//If the player hits a wall, it must stop(!). Otherwise, when the wall is removed,</span>
						<span style="color: #009900;">//the player will continue moving</span>
						x_speed=<span style="color: #000000; font-weight:bold;">0</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//That's the same, but with left key</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>x_speed; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">5</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">++</span>;
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						x_speed=<span style="color: #000000; font-weight:bold;">0</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>space_key<span style="color: #000000; font-weight: bold;">&amp;&amp;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				y_speed=<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>;<span style="color: #009900;">//Instead of using the jumping variable, I check if the character is on the ground</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				x_speed<span style="color: #000000; font-weight: bold;">*</span>=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>ground_friction;<span style="color: #009900;">//Again, I check if the character is on the ground (to apply ground or air friction)</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
				x_speed<span style="color: #000000; font-weight: bold;">*</span>=<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">-</span>air_friction;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>x_speed<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">0.1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				x_speed=<span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #000000;">&#125;</span><span style="color: #3f5fbf;">/*I don't want the character to move at 0.0000000001 pixels/frame,
			so if the x_speed is less than 0.1 we will stop it*/</span>
			y_speed<span style="color: #000000; font-weight: bold;">++</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>y_speed<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>y_speed; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//I changed character.y+9--&gt; character.y+10</span>
						character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">++</span>;
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						y_speed=<span style="color: #000000; font-weight:bold;">0</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>Math.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>y_speed<span style="color: #000000;">&#41;</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.y<span style="color: #000000; font-weight: bold;">--</span>;
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						y_speed=<span style="color: #000000; font-weight:bold;">0</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			hole_matrix=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Matrix</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			hole_matrix.<span style="color: #004993;">translate</span><span style="color: #000000;">&#40;</span>e.stageX<span style="color: #000000; font-weight: bold;">-</span>terrain_bmp.<span style="color: #004993;">x</span>,e.stageY<span style="color: #000000; font-weight: bold;">-</span>terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			terrain_bmpd.<span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span>hole,hole_matrix,<span style="color: #0033ff; font-weight: bold;">null</span>,<span style="color: #004993;">BlendMode</span>.<span style="color: #004993;">ERASE</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//Let's calculate the impulse. You'll have to understand trigonometric functions</span>
			x_diff=character.x<span style="color: #000000; font-weight: bold;">-</span>e.<span style="color: #004993;">stageX</span>;<span style="color: #009900;">//If we invert that, the character will move to the centre of the explosion instead of moving away</span>
			y_diff=character.y<span style="color: #000000; font-weight: bold;">-</span>e.<span style="color: #004993;">stageY</span>;
			<span style="color: #004993;">angle</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>y_diff,x_diff<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//We calculate the angle</span>
			<span style="color: #004993;">distance</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>x_diff<span style="color: #000000; font-weight: bold;">*</span>x_diff<span style="color: #000000; font-weight: bold;">+</span>y_diff<span style="color: #000000; font-weight: bold;">*</span>y_diff<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//Then, we use Pythagorean theorem</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>distance<span style="color: #000000; font-weight: bold;">&lt;</span>explosion_radius<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				applied_speed = <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #004993;">distance</span> <span style="color: #000000; font-weight: bold;">/</span> explosion_radius<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> explosion_intensity;
				<span style="color: #009900;">//Maybe that's difficult to understand:</span>
				<span style="color: #009900;">//How_far_is_the_character = distance / explosion_radius</span>
				<span style="color: #009900;">//How_near_is_the_character = 1 - How_far_is_the_character</span>
				<span style="color: #009900;">//applied_speed = How_near_is_the_character * explosion_intensity</span>
&nbsp;
				applied_x=<span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>applied_speed;<span style="color: #009900;">//We separate the applied_speed in x...</span>
				applied_y=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>applied_speed;<span style="color: #009900;">//... and y</span>
				y_speed<span style="color: #000000; font-weight: bold;">+</span>=applied_y;
				x_speed<span style="color: #000000; font-weight: bold;">+</span>=applied_x;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				left_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				right_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				left_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				right_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_objects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			terrain_bmp.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">200</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>terrain_bmp<span style="color: #000000;">&#41;</span>;
&nbsp;
			character.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x0000FF<span style="color: #000000;">&#41;</span>;
			character.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span>;
			character.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">250</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span>;
&nbsp;
			hole.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x000000<span style="color: #000000;">&#41;</span>;
			hole.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/08/worms3.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>As usual, left and right to move the character, spacebar to make it jump and mouse click to create explosions.</p>
<p><a href="/wp-content/uploads/2010/08/worms3.zip">Download the source code</a> and don&#8217;t forget to request the next part.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/18/worms-like-destructible-terrain-in-flash-part-3/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Create Box2D levels in a quick with Bison Kick</title>
		<link>http://www.emanueleferonato.com/2010/08/16/create-box2d-levels-in-a-quick-with-bison-kick/</link>
		<comments>http://www.emanueleferonato.com/2010/08/16/create-box2d-levels-in-a-quick-with-bison-kick/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 21:41:38 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3235</guid>
		<description><![CDATA[Some time ago I published a Basic Box2D editor using Flash movieclips. Now it&#8217;s time to show you something way more interesting called Bison Kick by Jacob Schatz who runs the blog jacobschatz.com (bookmark it! It contains a lot of useful information). It&#8217;s an online Box2D editor with live preview. Besides it&#8217;s still under development, [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I published a <a href="http://www.emanueleferonato.com/2010/07/12/basic-box2d-editor-using-flash-movieclips/">Basic Box2D editor using Flash movieclips</a>.</p>
<p>Now it&#8217;s time to show you something way more interesting called <strong><a target = "_blank" href="http://www.jacobschatz.com/bisonkick/">Bison Kick</a></strong> by <strong>Jacob Schatz</strong> who runs the blog <a href="http://jacobschatz.com/" target = "_blank">jacobschatz.com</a> (bookmark it! It contains a lot of useful information).</p>
<p><a target = "_blank" href="http://www.jacobschatz.com/bisonkick/"><img src="/wp-content/uploads/2010/08/bison.png" /></a></p>
<p>It&#8217;s an online Box2D editor with live preview.</p>
<p>Besides it&#8217;s still under development, with more options to be added and a few bugs to be removed, it&#8217;s very fun and simple to use.</p>
<p>I tried to design a level to be used in a game like Totem Destroyer and I managed to export it both in AS3:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID </span>
 <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">map</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span>
<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">175</span>,<span style="color: #000000; font-weight:bold;">324</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">325</span>,<span style="color: #000000; font-weight:bold;">325</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">375</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'ground'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">275</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">150</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">125</span>,<span style="color: #000000; font-weight:bold;">225</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">125</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">375</span>,<span style="color: #000000; font-weight:bold;">225</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.25</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'totem'</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#93;</span>;</pre></div></div>

<p>and in XML:<span id="more-3235"></span></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;level&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;175&lt;/xprop&gt;
    &lt;yprop&gt;324&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.5,0.5,'normal'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;325&lt;/xprop&gt;
    &lt;yprop&gt;325&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.5,0.5,'normal'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;250&lt;/xprop&gt;
    &lt;yprop&gt;375&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;500&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;false&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.5,0.5,'ground'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;250&lt;/xprop&gt;
    &lt;yprop&gt;275&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;300&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.5,0.5,'normal'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;250&lt;/xprop&gt;
    &lt;yprop&gt;200&lt;/yprop&gt;
    &lt;height&gt;100&lt;/height&gt;
    &lt;width&gt;150&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.5,0.5,'normal'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;125&lt;/xprop&gt;
    &lt;yprop&gt;225&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,2,0.5,'heavy'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;250&lt;/xprop&gt;
    &lt;yprop&gt;125&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,2,0.5,'heavy'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;375&lt;/xprop&gt;
    &lt;yprop&gt;225&lt;/yprop&gt;
    &lt;height&gt;50&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,2,0.5,'heavy'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
  &lt;completeShape&gt;
    &lt;xprop&gt;250&lt;/xprop&gt;
    &lt;yprop&gt;50&lt;/yprop&gt;
    &lt;height&gt;100&lt;/height&gt;
    &lt;width&gt;50&lt;/width&gt;
    &lt;rotation&gt;0&lt;/rotation&gt;
    &lt;isDynamic&gt;true&lt;/isDynamic&gt;
    &lt;shape&gt;SQUARE&lt;/shape&gt;
    &lt;physicsandID&gt;1,0.25,0.5,'totem'&lt;/physicsandID&gt;
  &lt;/completeShape&gt;
&lt;/level&gt;</pre></div></div>

<p>Then, creating a Box2D world from these data was really easy thanks to the <a href="http://jacobschatz.com/?p=117">snippet of code</a> provided by Jacob:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
</pre></td><td 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> Box2D.Dynamics.Joints.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Dynamics.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Collision.Shapes.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> Box2D.Common.<span style="color: #004993;">Math</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world<span style="color: #000000; font-weight: bold;">:</span>b2World=<span style="color: #0033ff; font-weight: bold;">new</span> b2World<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> b2Vec2<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">10.0</span><span style="color: #000000;">&#41;</span>,<span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> world_scale<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">30</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const <span style="color: #004993;">SQUARE</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;SQUARE&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const CIRCLE<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;CIRCLE&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> fixtureDef<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> specialB2Body<span style="color: #000000; font-weight: bold;">:</span>b2Body;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> motorOn<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
&nbsp;
		<span style="color: #009900;">//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID </span>
 <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">map</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span>
<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">175</span>,<span style="color: #000000; font-weight:bold;">324</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">325</span>,<span style="color: #000000; font-weight:bold;">325</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">375</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">500</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">false</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'ground'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">275</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">150</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'normal'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">125</span>,<span style="color: #000000; font-weight:bold;">225</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">125</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">375</span>,<span style="color: #000000; font-weight:bold;">225</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'heavy'</span><span style="color: #000000;">&#93;</span>
,<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">250</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,<span style="color: #990000;">'SQUARE'</span> , <span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">0.25</span>,<span style="color: #000000; font-weight:bold;">0.5</span>,<span style="color: #990000;">'totem'</span><span style="color: #000000;">&#93;</span>
<span style="color: #000000;">&#93;</span>;		<span style="color: #009900;">//</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> 
		<span style="color: #000000;">&#123;</span>
			debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">map</span>.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> sx<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sy<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sizerh<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sizerw<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> srot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sIsDynamic<span style="color: #000000; font-weight: bold;">:*</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sShape<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sFric<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sMass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> sRest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #6699cc; font-weight: bold;">var</span> boxId<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">map</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#93;</span>;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>sShape == <span style="color: #004993;">SQUARE</span><span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>	
					draw_box<span style="color: #000000;">&#40;</span>sx, sy, sizerw, sizerh, sIsDynamic,srot,sFric,sMass,sRest,boxId<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>sShape == CIRCLE<span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					draw_circle<span style="color: #000000;">&#40;</span>sx, sy, sizerw <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">2</span>, sIsDynamic,srot,sFric,sMass,sRest,boxId<span style="color: #000000;">&#41;</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, update<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_circle<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, py<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, r<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>, rot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,
		fric<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,mass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,rest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,boxId<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef= <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px <span style="color: #000000; font-weight: bold;">/</span> world_scale, py <span style="color: #000000; font-weight: bold;">/</span> world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> 
			<span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			my_body.<span style="color: #004993;">angle</span> = rot;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_circle<span style="color: #000000; font-weight: bold;">:</span>b2CircleShape=<span style="color: #0033ff; font-weight: bold;">new</span> b2CircleShape<span style="color: #000000;">&#40;</span>r<span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.density = mass;
			my_fixture.friction = fric;
			my_fixture.restitution = rest;
			my_fixture.shape=my_circle;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body = world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_box<span style="color: #000000;">&#40;</span>px<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, py<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, w<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, h<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>, rot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,
		fric<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,mass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,rest<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>,boxId<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_body<span style="color: #000000; font-weight: bold;">:</span>b2BodyDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2BodyDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			my_body.<span style="color: #004993;">position</span>.Set<span style="color: #000000;">&#40;</span>px <span style="color: #000000; font-weight: bold;">/</span> world_scale, py <span style="color: #000000; font-weight: bold;">/</span> world_scale<span style="color: #000000;">&#41;</span>;
			my_body.<span style="color: #004993;">angle</span> = rot;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span> 
			<span style="color: #000000;">&#123;</span>
				my_body.<span style="color: #004993;">type</span>=b2Body.b2_dynamicBody;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> my_box<span style="color: #000000; font-weight: bold;">:</span>b2PolygonShape = <span style="color: #0033ff; font-weight: bold;">new</span> b2PolygonShape<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_box.SetAsBox<span style="color: #000000;">&#40;</span>w<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale, h<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000; font-weight: bold;">/</span>world_scale<span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> my_fixture<span style="color: #000000; font-weight: bold;">:</span>b2FixtureDef = <span style="color: #0033ff; font-weight: bold;">new</span> b2FixtureDef<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			my_fixture.density = mass;
			my_fixture.friction = fric;
			my_fixture.restitution = rest;
			my_fixture.shape=my_box;
			<span style="color: #6699cc; font-weight: bold;">var</span> world_body<span style="color: #000000; font-weight: bold;">:</span>b2Body=world.CreateBody<span style="color: #000000;">&#40;</span>my_body<span style="color: #000000;">&#41;</span>;
			world_body.CreateFixture<span style="color: #000000;">&#40;</span>my_fixture<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> debug_draw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_draw<span style="color: #000000; font-weight: bold;">:</span>b2DebugDraw = <span style="color: #0033ff; font-weight: bold;">new</span> b2DebugDraw<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> debug_sprite<span style="color: #000000; 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: #004993;">addChild</span><span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetSprite<span style="color: #000000;">&#40;</span>debug_sprite<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFillAlpha<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.3</span><span style="color: #000000;">&#41;</span>;
			debug_draw.SetDrawScale<span style="color: #000000;">&#40;</span>world_scale<span style="color: #000000;">&#41;</span>;
			debug_draw.SetFlags<span style="color: #000000;">&#40;</span>b2DebugDraw.e_shapeBit<span style="color: #000000;">&#41;</span>;
			world.SetDebugDraw<span style="color: #000000;">&#40;</span>debug_draw<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			world.Step<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">30</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;			
			world.ClearForces<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			world.DrawDebugData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>&#8230; and this is what you&#8217;ll get:</p>
<p><embed src="/wp-content/uploads/2010/08/bison.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>This the same scene of the screenshot you can see at the beginning of this post.</p>
<p>Next time, I&#8217;ll show you how to generate the same thing reading an external XML, and how to add more features.</p>
<p><a href="/wp-content/uploads/2010/08/bison.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/16/create-box2d-levels-in-a-quick-with-bison-kick/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create a Flash game like Blockage</title>
		<link>http://www.emanueleferonato.com/2010/08/11/create-a-flash-game-like-blockage/</link>
		<comments>http://www.emanueleferonato.com/2010/08/11/create-a-flash-game-like-blockage/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 16:24:27 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3228</guid>
		<description><![CDATA[Did you play Blockage? It&#8217;s a perfect game to make a tutorial series because at every level it introduces a new feature, so level after level you will see something new both in the game and in the prototype. Other than that, it&#8217;s a tile based puzzle. This first part will focus on level data. [...]]]></description>
			<content:encoded><![CDATA[<p>Did you play <strong><a href="http://www.triqui.com/2010/08/03/blockage/" target = "_blank">Blockage</a></strong>?</p>
<p><a href="http://www.triqui.com/2010/08/03/blockage/" target = "_blank"><img src="/wp-content/uploads/2010/08/blockage.png" /></a></p>
<p>It&#8217;s a perfect game to make a tutorial series because at every level it introduces a new feature, so level after level you will see something new both in the game and in the prototype.</p>
<p>Other than that, it&#8217;s a tile based puzzle.</p>
<p>This first part will focus on level data. Since it&#8217;s a tile based game, you know we should create a bidimensional array, map all levels assigning each tile type a value such as zero for the empty space and one for the walls, and start creating the array tile after tile, just like in <a href="http://www.emanueleferonato.com/2010/05/10/create-a-flash-game-like-rebuild-chile/">Create a Flash game like Rebuild Chile</a>.</p>
<p>In this prototype I will try to &#8220;compress&#8221; the level packing it in a string containing comma separated values.</p>
<p>Let me explain the idea: count, from left to right, from upper to bottom corners, the number of contiguous walls.</p>
<p>There are 49 walls. Then, 14 empty spaces. Then, two walls. And so on. Exluding the actors (the square and the goal), create a string like this one:</p>
<p><code>49,1,14,0,2,1,14,0,2,1,14,0,8,1,1,0,56,1</code></p>
<p>That means the level has 49 walls (marked with <code>1</code>), 14 empty spaces (marked with <code>0</code>), then 2 walls, 14 spaces and so on.</p>
<p>So this code:<span id="more-3228"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> blockage extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> const LEVEL_WIDTH<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">16</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> const LEVEL_HEIGHT<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">10</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> leveldata<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #990000;">&quot;49,1,14,0,2,1,14,0,2,1,14,0,8,1,1,0,56,1&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tmp_level<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">level</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> wall<span style="color: #000000; font-weight: bold;">:</span>wall_mc;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> blockage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			tmp_level=leveldata.<span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;,&quot;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>tmp_level.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">+</span>=<span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j=<span style="color: #000000; font-weight:bold;">1</span>; j<span style="color: #000000; font-weight: bold;">&lt;</span>=tmp_level<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>; j<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #004993;">level</span>.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>tmp_level<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>tmp_level<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>==<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						wall = <span style="color: #0033ff; font-weight: bold;">new</span> wall_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>wall<span style="color: #000000;">&#41;</span>;
						wall.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">level</span>.length<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">%</span>LEVEL_WIDTH<span style="color: #000000;">&#41;</span>;
						wall.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">level</span>.length<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span>LEVEL_WIDTH<span style="color: #000000;">&#41;</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Draws the level and stores it in <code>level</code> array, starting from <code>leveldata</code> that is the &#8220;compressed&#8221; level data.</p>
<p><strong>Line 11</strong>: splits <code>leveldata</code> into <code>tmp_level</code> array. It&#8217;s just as if I did</p>
<p><code>tmp_level = [49,1,14,0,2,1,14,0,2,1,14,0,8,1,1,0,56,1];</code></p>
<p>But starting with a string I can easily import levels from an external editor.</p>
<p><strong>Line 12</strong>: loops through all <code>tmp_level</code> <strong>even</strong> elements.</p>
<p><strong>Lines 13-21</strong>: creating the level itself in <code>level</code> array pushing <code>i</code> times <code>i+1</code> value, and if such value is a wall (<code>1</code>), then adding the wall movieclip.</p>
<p>This is the result:</p>
<p><embed src="/wp-content/uploads/2010/08/blockage.swf" allowscriptaccess="always" menu="false" quality="high" width="512" height="320" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>The same old thing made in a different way. Does it make any sense to you?</p>
<p><a href="/wp-content/uploads/2010/08/blockage.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/11/create-a-flash-game-like-blockage/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Create your own tween manager class in AS3</title>
		<link>http://www.emanueleferonato.com/2010/08/09/create-your-own-tween-manager-class-in-as3/</link>
		<comments>http://www.emanueleferonato.com/2010/08/09/create-your-own-tween-manager-class-in-as3/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 19:40:53 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3220</guid>
		<description><![CDATA[I publish this tutorial with the permission of Krasimir Tsonev, from Bulgaria, who runs krasimirtsonev.com (take a look at the light bulbs: don&#8217;t you think it can be done using a basic Box2D rope?). It&#8217;s an amazing tutorial with clean and well commented code that will guide you through the process of the creation of [...]]]></description>
			<content:encoded><![CDATA[<p>I publish this tutorial with the permission of <strong>Krasimir Tsonev</strong>, from Bulgaria, who runs <a href="http://krasimirtsonev.com/" target = "_blank"><strong>krasimirtsonev.com</strong></a> (take a look at the light bulbs: don&#8217;t you think it can be done using a <a href="http://www.emanueleferonato.com/2009/10/05/basic-box2d-rope/">basic Box2D rope</a>?). It&#8217;s an amazing tutorial with clean and well commented code that will guide you through the process of the creation of a custom tween manager class with AS3.</p>
<p>There are some features in Flash that we can&#8217;t work without. Tween classes are among the most used ones. They give you ability to animate objects without using the timeline, to change the animation fast and easy. The idea of these classes is very simple. That&#8217;s why I think that it is a good idea to have your own tween manager that you can modify to fit into your needs.</p>
<p>The basic structure of our tween manager:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TweenManager extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TweenManager<span style="color: #000000;">&#40;</span>objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_objectToModify = objectToModify;
			_properties = properties;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>We are going to pass the object that we want to modify and the properties that we want to change. The idea is to create a function that calls every frame. All the magic will be done there. As you can see in the code below I created a public function &#8211; start, which adds listener for ENTER_FRAME event. So now we have a repeated method, i.e. loop.<span id="more-3220"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TweenManager extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TweenManager<span style="color: #000000;">&#40;</span>objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_objectToModify = objectToModify;
			_properties = properties;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>What are we going to pass as properties parameter? I think it is a good idea to use JSON object, because it&#8217;s really flexible and we can add/remove properties really fast without changing anything in our class. Here is an example of the creation of an object from our tween class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> App extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> clip<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> App<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #000000;">&#123;</span><span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">50</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">390</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span>TweenManager = <span style="color: #0033ff; font-weight: bold;">new</span> TweenManager<span style="color: #000000;">&#40;</span>clip,properties<span style="color: #000000;">&#41;</span>;
			t.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>There are two things that you have to notice. The first one is that you should have a movie clip on the stage that you can use for the tween. The second thing is the properties object. As you can see we are going to change the &#8220;x&#8221; property of the clip from 50 to 390 for 100 steps, i.e. 100 frames. To be able to do that we need the value of &#8220;x&#8221; for each one of these 100 frames. The function &#8220;calculateValues&#8221; will do that for us:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TweenManager extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TweenManager<span style="color: #000000;">&#40;</span>objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_objectToModify = objectToModify;
			_properties = properties;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.values = calculateValues<span style="color: #000000;">&#40;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #004993;">start</span>,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.end,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.steps<span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> calculateValues<span style="color: #000000;">&#40;</span>startPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, endPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> values<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>steps<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				values.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>startPos <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>endPos <span style="color: #000000; font-weight: bold;">-</span> startPos<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> steps<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> values;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see we are adding dynamically a new array for every property in our properties object. &#8220;values&#8221; is an array with all the values of &#8220;x&#8221; for each of these 100 frames. The last thing that we should make is to pass the values to our clip, i.e. to write the content of the &#8220;loop&#8221; method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TweenManager extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TweenManager<span style="color: #000000;">&#40;</span>objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_objectToModify = objectToModify;
			_properties = properties;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.values = calculateValues<span style="color: #000000;">&#40;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #004993;">start</span>,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.end,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.steps<span style="color: #000000;">&#41;</span>;
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex = <span style="color: #000000; font-weight:bold;">0</span>;
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone = <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// looping through the properties  </span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// checking if the properties tween is done     </span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// checking if the current value is the last one            </span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex == _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.steps <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// marking the tween as a done </span>
						_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone = <span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// applying the value     </span>
						_objectToModify<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.values<span style="color: #000000;">&#91;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex<span style="color: #000000;">&#93;</span>;<span style="color: #009900;">// incrementing the values' index </span>
						_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex <span style="color: #000000; font-weight: bold;">+</span>=  <span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span><span style="color: #009900;">// checking if all the tweens are done   </span>
			<span style="color: #6699cc; font-weight: bold;">var</span> areAllDone<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					areAllDone = <span style="color: #0033ff; font-weight: bold;">false</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span><span style="color: #009900;">// if yes then remove the listener    </span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>areAllDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> calculateValues<span style="color: #000000;">&#40;</span>startPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, endPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> values<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>steps<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				values.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>startPos <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>endPos <span style="color: #000000; font-weight: bold;">-</span> startPos<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> steps<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> values;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Please notice lines 18 and 19. We added a flag (isItDone) that indicates when the tween is finished and &#8220;valueIndex&#8221;, which we used to go through all the values.<br />
When you run the flash you will see that the circle is moving from x=50 to x=390 for 100 frames. Let&#8217;s add some other properties and see how it looks:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> App extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> clip<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> App<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #000000;">&#123;</span><span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">50</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">390</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">50</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">390</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">0</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">180</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">alpha</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">1</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">0.5</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span>TweenManager = <span style="color: #0033ff; font-weight: bold;">new</span> TweenManager<span style="color: #000000;">&#40;</span>clip,properties<span style="color: #000000;">&#41;</span>;
			t.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>We can change as many properties as we want and obviously to set different steps for each one of them.</p>
<p>Ok, the Tween class looks good so far but it isn&#8217;t as cool as we wanted. It&#8217;s because there is no easing. We used only a linear type of motion. To add other types we&#8217;re going to use some maths by <a href="http://robertpenner.com/" target = "_blank">Robert Penner</a>. We will just change the calculateValues method and it will support different types of motions. Here is the final version of the class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TweenManager extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TweenManager<span style="color: #000000;">&#40;</span>objectToModify<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			_objectToModify = objectToModify;
			_properties = properties;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.values = calculateValues<span style="color: #000000;">&#40;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #004993;">start</span>,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.end,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.steps,_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #004993;">method</span><span style="color: #000000;">&#41;</span>;
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex = <span style="color: #000000; font-weight:bold;">0</span>;
				_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone = <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">loop</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// looping through the properties          </span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// checking if the properies twee it's done </span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// checking if the current value is the last one    </span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex == _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.steps <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// marking the tween as a done          </span>
						_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone = <span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #000000;">&#125;</span>
					<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">// applying the value </span>
						_objectToModify<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.values<span style="color: #000000;">&#91;</span>_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex<span style="color: #000000;">&#93;</span>;<span style="color: #009900;">// incrementing the values' index     </span>
						_properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.valueIndex <span style="color: #000000; font-weight: bold;">+</span>=  <span style="color: #000000; font-weight:bold;">1</span>;
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span><span style="color: #009900;">// checking if all the tweens are done </span>
			<span style="color: #6699cc; font-weight: bold;">var</span> areAllDone<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i <span style="color: #0033ff; font-weight: bold;">in</span> _properties<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> _properties<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.isItDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					areAllDone = <span style="color: #0033ff; font-weight: bold;">false</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span><span style="color: #009900;">// if yes then remove the listener  </span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>areAllDone<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, <span style="color: #004993;">loop</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> calculateValues<span style="color: #000000;">&#40;</span>startPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, endPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, mtd<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> arr<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> calcEndPos<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>=steps; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				calcEndPos = <span style="color: #000000;">&#40;</span>endPos<span style="color: #000000; font-weight: bold;">-</span>startPos<span style="color: #000000;">&#41;</span>;
				<span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>mtd<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InBack&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InBack<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">1.70158</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutBack&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutBack<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">1.70158</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutBack&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutBack<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">1.70158</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutBounce&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutBounce<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InBounce&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InBounce<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutBounce&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutBounce<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InCirc&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InCirc<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutCirc&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutCirc<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutCirc&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutCirc<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;In&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.In<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;Out&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.Out<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOut&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOut<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InElastic&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InElastic<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutElastic&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutElastic<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutElastic&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutElastic<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InExpo&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InExpo<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutExpo&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutExpo<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutExpo&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutExpo<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;Linear&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.Linear<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InLinear&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InLinear<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutLinear&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutLinear<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutLinear&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutLinear<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InQuad&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InQuad<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutQuad&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutQuad<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutQuad&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutQuad<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InQuart&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InQuart<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutQuart&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutQuart<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutQuart&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutQuart<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InQuint&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutQuint&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutQuint&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InSine&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;OutSine&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.OutQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">case</span> <span style="color: #990000;">&quot;InOutSine&quot;</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.InOutQuint<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
					<span style="color: #0033ff; font-weight: bold;">default</span> <span style="color: #000000; font-weight: bold;">:</span>
						arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = Ease.Linear<span style="color: #000000;">&#40;</span>i,startPos,calcEndPos,steps<span style="color: #000000;">&#41;</span>;
						<span style="color: #0033ff; font-weight: bold;">break</span>;
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> arr;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And the usage:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">MovieClip</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> App extends <span style="color: #004993;">MovieClip</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> clip<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> App<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> properties<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #000000;">&#123;</span><span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">50</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">390</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">170</span>,<span style="color: #004993;">method</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;OutBack&quot;</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">50</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">390</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">170</span>,<span style="color: #004993;">method</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;OutBounce&quot;</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">0</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">180</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">300</span>,<span style="color: #004993;">method</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #990000;">&quot;OutElastic&quot;</span><span style="color: #000000;">&#125;</span>,<span style="color: #004993;">alpha</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#123;</span><span style="color: #004993;">start</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">1</span>,end<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">0.5</span>,steps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span>TweenManager = <span style="color: #0033ff; font-weight: bold;">new</span> TweenManager<span style="color: #000000;">&#40;</span>clip,properties<span style="color: #000000;">&#41;</span>;
			t.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Together with &#8220;start&#8221;, &#8220;end&#8221; and &#8220;steps&#8221; we are passing a new property &#8220;method&#8221; which is used by the &#8220;calculateValues&#8221; function. The class &#8220;Ease&#8221; contains mathematics methods that calculate the values. No need to understand it, just have to know how to use it.</p>
<p>And this is an example:</p>
<p><embed src="/wp-content/uploads/2010/08/tween.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="390" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>You may need to reload the page to see it in action.</p>
<p>Of course the class is not fully functional. I mean there are no checks if some of the properties like &#8220;start&#8221; or &#8220;end&#8221; are missing. We can also add an ability to call a callback method when some of the tweens finish. It&#8217;s just a basic manager that you can develop.</p>
<p><a href="/wp-content/uploads/2010/08/tween.zip">Download the source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/09/create-your-own-tween-manager-class-in-as3/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Worms-like destructible terrain in Flash &#8211; Part 2</title>
		<link>http://www.emanueleferonato.com/2010/08/05/worms-like-destructible-terrain-in-flash-part-2/</link>
		<comments>http://www.emanueleferonato.com/2010/08/05/worms-like-destructible-terrain-in-flash-part-2/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 21:49:30 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3213</guid>
		<description><![CDATA[After having some troubles with the email, I am finally able to post Jordi Sanglas Molist&#8216;s second step of Worms-like destructible terrain in Flash. Now the character can jump and move. I&#8217;ve also solved a bug: in the last script I checked a collision using the feet, so if the terrain was between both feet [...]]]></description>
			<content:encoded><![CDATA[<p>After having some troubles with the email, I am finally able to post <strong>Jordi Sanglas Molist</strong>&#8216;s second step of <a href="http://www.emanueleferonato.com/2010/06/25/worms-like-destructible-terrain-in-flash/">Worms-like destructible terrain in Flash</a>.</p>
<p>Now the character can jump and move. I&#8217;ve also solved a bug: in the last script I checked a collision using the feet, so if the terrain was between both feet the character would fall.</p>
<p><img src="/wp-content/uploads/2010/08/WormsPrototype_bugSolved.png" /></p>
<p>Now, instead of using a hitTest between a BitmapData and a point I use a hitTest between a BitmapData and a Rectangle.</p>
<p>We have to check more collisions, so I used four rectangles:</p>
<p><img src="/wp-content/uploads/2010/08/WormsPrototype_rectangles.png" /></p>
<p>* The rectangle below the character is used to check if the character must fall</p>
<p>* The rectangle above the character is used to check if the character hit the ground (while he was jumping)</p>
<p>* The rectangles at the sides are used to check if the character can move.</p>
<p>These rectangles aren&#8217;t as long as the character, they&#8217;re 17 pixels height instead of 20. That&#8217;s because, if the character wants to move left and the obstacle isn&#8217;t high enough (high enough to reach the rectangle), the character will be able to move. A similar strategy is used in &#8220;<a href="http://www.emanueleferonato.com/2007/03/31/create-a-flash-draw-game-like-line-rider-or-others-part-5/">Create a flash draw game like line rider or others &#8211; part 5</a>&#8220;, where a point (the knee point) is used to check if the character can move.</p>
<p>I renamed the function &#8220;fall&#8221;: now it&#8217;s &#8220;move_character&#8221;. I only commented the new lines. Now I&#8217;m working on part 3, but I still don&#8217;t know how to calculate the explosion impulse. I looked for some information, but now I&#8217;m thinking about maths (an explosion is a growing circle).<span id="more-3213"></span></p>
<p>This is the script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Bitmap</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">BitmapData</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">BlendMode</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Point</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Rectangle</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Matrix</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">KeyboardEvent</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> worms extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> terrain_bmpd<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">550</span>,<span style="color: #000000; font-weight:bold;">200</span>,<span style="color: #0033ff; font-weight: bold;">true</span>,0xFF00FF00<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> terrain_bmp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span>=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span>terrain_bmpd<span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> character<span style="color: #000000; 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: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> character_speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//That number is the y speed that the character has</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> hole<span style="color: #000000; 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: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> hole_matrix<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Matrix</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> left_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> right_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> space_key<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> jumping<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>=<span style="color: #0033ff; font-weight: bold;">true</span>;<span style="color: #009900;">//When that variable is true, the character is in the air</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;<span style="color: #009900;">//We will use this variable to make a loop</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> worms<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			draw_objects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,move_character<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_UP</span>,mouse_up<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_DOWN</span>,key_down<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span>.<span style="color: #004993;">KEY_UP</span>,key_up<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> move_character<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//If left key is pressed, we'll move the character to the left</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>left_key<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">3</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//Do you remember when we made the character fall? We had to move the character pixel by pixel</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">6</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.x<span style="color: #000000; font-weight: bold;">--</span>;	<span style="color: #3f5fbf;">/*If the character doesn't hit the ground, we can move left. However,
										the character may be sunk under the ground. We have to lift it*/</span>
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>right_key<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//Well, that's the same for the right key</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">3</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">5</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span>,<span style="color: #000000; font-weight:bold;">17</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">++</span>;
						<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							character.y<span style="color: #000000; font-weight: bold;">--</span>;
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>space_key<span style="color: #000000; font-weight: bold;">&amp;&amp;!</span> jumping<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//That's easy: if he isn't jumping and you press space, his speed will be negative and he'll jump</span>
				character_speed=<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>;
				jumping=<span style="color: #0033ff; font-weight: bold;">true</span>;<span style="color: #009900;">//Now the character can't jump again</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			character_speed<span style="color: #000000; font-weight: bold;">++</span>;<span style="color: #009900;">//Every frame we will increase character's speed</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>character_speed<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">//If the speed is positive, we will check a collision between the terrain and the rectangle below the character</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>character_speed; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//We check the collision pixel by pixel...</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">9</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">++</span>;<span style="color: #009900;">//If there isn't a collision, the character will fall</span>
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						jumping=<span style="color: #0033ff; font-weight: bold;">false</span>;<span style="color: #009900;">//If there's a collision with the ground, the character isn't jumping</span>
						character_speed=<span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//The speed is 0, because the character hit the ground</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>Math.<span style="color: #004993;">abs</span><span style="color: #000000;">&#40;</span>character_speed<span style="color: #000000;">&#41;</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">//If the speed is negative, the for loop won't work. We have to use Math.abs().</span>
				<span style="color: #009900;">//Now we will check the collision between the terrain and the rectangle above the character</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span> terrain_bmpd.<span style="color: #004993;">hitTest</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span>terrain_bmp.<span style="color: #004993;">x</span>,terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>,0x01,<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span>character.x<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,character.y<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						character.y<span style="color: #000000; font-weight: bold;">--</span>;
					<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
						character_speed=<span style="color: #000000; font-weight:bold;">0</span>;<span style="color: #009900;">//Well, that's the same: the character hit the ground</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> mouse_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			hole_matrix=<span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Matrix</span>  ;
			hole_matrix.<span style="color: #004993;">translate</span><span style="color: #000000;">&#40;</span>e.stageX<span style="color: #000000; font-weight: bold;">-</span>terrain_bmp.<span style="color: #004993;">x</span>,e.stageY<span style="color: #000000; font-weight: bold;">-</span>terrain_bmp.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
			terrain_bmpd.<span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span>hole,hole_matrix,<span style="color: #0033ff; font-weight: bold;">null</span>,<span style="color: #004993;">BlendMode</span>.<span style="color: #004993;">ERASE</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_down<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				left_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				right_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_key=<span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> key_up<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				left_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				right_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">keyCode</span>==<span style="color: #000000; font-weight:bold;">32</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				space_key=<span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> draw_objects<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			terrain_bmp.<span style="color: #004993;">y</span>=<span style="color: #000000; font-weight:bold;">200</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>terrain_bmp<span style="color: #000000;">&#41;</span>;
&nbsp;
			character.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x0000FF<span style="color: #000000;">&#41;</span>;
			character.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span>;
			character.<span style="color: #004993;">x</span>=<span style="color: #000000; font-weight:bold;">250</span>;
			<span style="color: #004993;">stage</span>.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span>;
&nbsp;
			hole.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x000000<span style="color: #000000;">&#41;</span>;
			hole.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And this is the result:</p>
<p><embed src="/wp-content/uploads/2010/08/worms.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Move the player with left &#8211; right arrow keys and click on the terrain to create an explosion.</p>
<p><a href="/wp-content/uploads/2010/08/worms.zip">Download the source code</a>. Now Jordi is trying to manage explosion impulse but he&#8217;s having some problems. Any blog-bomber reader willing to read?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/05/worms-like-destructible-terrain-in-flash-part-2/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Changing a Movieclip registration point on the fly with AS3</title>
		<link>http://www.emanueleferonato.com/2010/08/04/changing-a-movieclip-registration-point-on-the-fly-with-as3/</link>
		<comments>http://www.emanueleferonato.com/2010/08/04/changing-a-movieclip-registration-point-on-the-fly-with-as3/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 09:49:57 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3198</guid>
		<description><![CDATA[Do you know what is a Movieclip registration point? If you ever played with Flash drawing tools, you should. The registration point is like the origin in a Cartesian coordinate system, the default point of a Movieclip from which its x and y coordinates are calculated. In the example we are about to see, I [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know what is a Movieclip registration point? If you ever played with Flash drawing tools, you should.</p>
<p>The registration point is like the origin in a Cartesian coordinate system, the default point of a Movieclip from which its x and y coordinates are calculated.</p>
<p>In the example we are about to see, I created this square with a 100 pixel side and placed at (-50,-50), so its center of rotation is at (0,0), as you can see from the tiny white crosshair in the centre of the square.</p>
<p><img src="/wp-content/uploads/2010/08/registration.png" /></p>
<p>Now the question is: can I change dynamically the registration point with AS3?</p>
<p>The answer is: <strong>NO</strong>. No code to download, and see you later.</p>
<p>Anyway, there is a little trick.</p>
<p>Look at this script:<span id="more-3198"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> registration extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> square<span style="color: #000000; font-weight: bold;">:</span>square_mc;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> static_square<span style="color: #000000; font-weight: bold;">:</span>static_square_mc
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> registration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			square = <span style="color: #0033ff; font-weight: bold;">new</span> square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span>,<span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
			square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>
			square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>square<span style="color: #000000;">&#41;</span>;
			static_square = <span style="color: #0033ff; font-weight: bold;">new</span> static_square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>static_square<span style="color: #000000;">&#41;</span>;
			static_square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span>
			static_square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			static_square.<span style="color: #004993;">alpha</span>=<span style="color: #000000; font-weight:bold;">0.3</span>
			<span style="color: #009900;">//</span>
			square = <span style="color: #0033ff; font-weight: bold;">new</span> square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">50</span>,<span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span>;
			square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">250</span>
			square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>square<span style="color: #000000;">&#41;</span>;
			static_square = <span style="color: #0033ff; font-weight: bold;">new</span> static_square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>static_square<span style="color: #000000;">&#41;</span>;
			static_square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">250</span>
			static_square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			static_square.<span style="color: #004993;">alpha</span>=<span style="color: #000000; font-weight:bold;">0.3</span>
			<span style="color: #009900;">//</span>
			square = <span style="color: #0033ff; font-weight: bold;">new</span> square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
			square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">400</span>
			square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>square<span style="color: #000000;">&#41;</span>;
			static_square = <span style="color: #0033ff; font-weight: bold;">new</span> static_square_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>static_square<span style="color: #000000;">&#41;</span>;
			static_square.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">400</span>
			static_square.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span>
			static_square.<span style="color: #004993;">alpha</span>=<span style="color: #000000; font-weight:bold;">0.3</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In a very lazy way (because it&#8217;s not the focus of this post) it creates three squares and three static squares.</p>
<p>Let&#8217;s concentrate on <code>square_mc</code> class constructor:</p>
<p><code>square = new square_mc(x,y);</code></p>
<p>Where <code>x</code> and <code>y</code> represent the coordinates of the new registration point. So (0,0) will mean the upper left corner, (50,50) the center and (100,100) the lower right corner.</p>
<p>Let&#8217;s see <code>square_mc.as</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td 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> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span>.<span style="color: #004993;">Rectangle</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> square_mc extends <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> new_reg_x,new_reg_y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> pin<span style="color: #000000; font-weight: bold;">:</span>pin_mc=<span style="color: #0033ff; font-weight: bold;">new</span> pin_mc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> square_mc<span style="color: #000000;">&#40;</span>reg_x,reg_y<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			new_reg_x=reg_x;
			new_reg_y=reg_y;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,on_enter_frame<span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED_TO_STAGE</span>,on_added<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_added<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">rect</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Rectangle</span>=<span style="color: #004993;">getBounds</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> x_offset=new_reg_x<span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">rect</span>.<span style="color: #004993;">x</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> y_offset=new_reg_y<span style="color: #000000; font-weight: bold;">+</span><span style="color: #004993;">rect</span>.<span style="color: #004993;">y</span>
			<span style="color: #009900;">// updating container position</span>
			<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>=x_offset;
			<span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">+</span>=y_offset;
			<span style="color: #009900;">// just adding a pin to highlight registration point</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> par<span style="color: #000000; font-weight: bold;">:</span>registration=<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">parent</span> <span style="color: #0033ff; font-weight: bold;">as</span> registration
			par.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>pin<span style="color: #000000;">&#41;</span>;
			pin.<span style="color: #004993;">x</span>=<span style="color: #004993;">x</span>;
			pin.<span style="color: #004993;">y</span>=<span style="color: #004993;">y</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span>; i<span style="color: #000000; font-weight: bold;">&lt;</span>numChildren; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// updating children position</span>
				<span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>.x<span style="color: #000000; font-weight: bold;">-</span>=x_offset;
				<span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>.y<span style="color: #000000; font-weight: bold;">-</span>=y_offset;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on_enter_frame<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">rotation</span><span style="color: #000000; font-weight: bold;">+</span>=<span style="color: #000000; font-weight:bold;">2</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>First, I get the bounds rectangle of the object (not necessary since I know it&#8217;s a square, but I could be anything else), then I determine the offset adding to new registration coordinates the origin of the bounds rectangle. This will work because I drew the square centered on movieclip origin.</p>
<p>Once I found the offset, it&#8217;s just a matter of adding the offset to square position and subtracting from all its children&#8217;s (the square shape) position.</p>
<p>And that&#8217;s it:</p>
<p><embed src="/wp-content/uploads/2010/08/registration1.swf" allowscriptaccess="always" menu="false" quality="high" width="500" height="200" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>Obviously you can set the new registration point anywhere you want, and it will work anyway.</p>
<p><a href="/wp-content/uploads/2010/08/registration1.zip">Download the source code</a>.</p>
<p>Just a quick question: what game prototype I am going to show you using this feature?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/04/changing-a-movieclip-registration-point-on-the-fly-with-as3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
