<?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 &#187; Actionscript 3</title>
	<atom:link href="http://www.emanueleferonato.com/category/actionscript-3/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>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>
		<item>
		<title>Create a Flash racing game &#8211; Flex version</title>
		<link>http://www.emanueleferonato.com/2010/07/30/create-a-flash-racing-game-flex-version/</link>
		<comments>http://www.emanueleferonato.com/2010/07/30/create-a-flash-racing-game-flex-version/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 15:12:51 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Game design]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3191</guid>
		<description><![CDATA[Hamilton Lombardi is a brazilian Flex programmer who decided to rewrite the code you can find in the original post for Flex 4. Moreover he added a splash screen, as you can see: The source code is clearly formatted and commented as you can see from Level class: 1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Hamilton Lombardi</strong> is a brazilian Flex programmer who decided to rewrite the code you can find in the <a href="http://www.emanueleferonato.com/2007/05/15/create-a-flash-racing-game-tutorial/">original post</a> for Flex 4.</p>
<p>Moreover he added a splash screen, as you can see:</p>
<p><embed src="/wp-content/uploads/2010/07/FlexRacer.swf" allowscriptaccess="always" menu="false" quality="high" width="510" height="410" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></p>
<p>The source code is clearly formatted and commented as you can see from <code>Level</code> class:<span id="more-3191"></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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
</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.geom</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.media</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> mx.containers.Canvas;
	<span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
	<span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Label;
	<span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Text;
	<span style="color: #0033ff; font-weight: bold;">import</span> mx.core.<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> Level
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> static <span style="color: #6699cc; font-weight: bold;">var</span> instance<span style="color: #000000; font-weight: bold;">:</span>Level = <span style="color: #0033ff; font-weight: bold;">null</span>;	
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #6699cc; font-weight: bold;">var</span> player<span style="color: #000000; font-weight: bold;">:</span>Player = <span style="color: #0033ff; font-weight: bold;">null</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">track</span><span style="color: #000000; font-weight: bold;">:</span>Track = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
		<span style="color: #009900;">//this is the max number of laps</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> const totalLaps<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">3</span>;
&nbsp;
		<span style="color: #009900;">// Moment the race started</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> initialTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span>;
		<span style="color: #009900;">// Current lap time</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> lapTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span>;
		<span style="color: #009900;">// Current fastest lap</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> bestLap<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;">// Current lap number</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> currentLap<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #009900;">// Current checkpoint - 1 Start Line , 2 - Mid track</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> currentCheckpoint<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		<span style="color: #009900;">// History of all the lap times</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> raceHistory<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> Instance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Level
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> instance == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				instance = <span style="color: #0033ff; font-weight: bold;">new</span> Level<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> instance;
		<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> Level<span style="color: #000000;">&#40;</span>caller<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span> = <span style="color: #0033ff; font-weight: bold;">null</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> Level.instance <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;Only one Singleton instance should be instantiated&quot;</span> <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> startup<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: #009900;">// Start all clock timers</span>
			startupTimeClocks<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Start time display</span>
			startupTimeDisplay<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Creates the race track</span>
			<span style="color: #004993;">track</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Track<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">track</span>.startupTrack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	
&nbsp;
			<span style="color: #009900;">// Creates the racer</span>
			player = <span style="color: #0033ff; font-weight: bold;">new</span> Player<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			player.startupPlayer<span style="color: #000000;">&#40;</span><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> shutdown<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: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">enterFrame</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: #009900;">// Check if the lap had been completed</span>
			checkPoints<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">// Update total race time display</span>
			setTimes<span style="color: #000000;">&#40;</span><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> setTimes<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>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> timeElapsed<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: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">milliseconds</span><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: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">seconds</span><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: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">minutes</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> minutesTXT<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: #6699cc; font-weight: bold;">var</span> secondsTXT<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: #6699cc; font-weight: bold;">var</span> tensTXT<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> totalTXT<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> totalTimeLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
			<span style="color: #6699cc; font-weight: bold;">var</span> timeCanvas<span style="color: #000000; font-weight: bold;">:</span>Canvas;
&nbsp;
			<span style="color: #009900;">// Only starts the timer if the car cross the start line for the first time</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentLap <span style="color: #000000; font-weight: bold;">!</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;">//we calculate the time elapsed from the moment the race started in millisecond</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> thisTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Date</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				timeElapsed = <span style="color: #000000;">&#40;</span>thisTime.<span style="color: #004993;">getTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">-</span> initialTime.<span style="color: #004993;">getTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
				<span style="color: #009900;">//we calculate the minutes, seconds and tens of seconds and set them to their respective variables</span>
				<span style="color: #004993;">milliseconds</span> = timeElapsed;
				<span style="color: #004993;">seconds</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">milliseconds</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">minutes</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">seconds</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span><span style="color: #000000;">&#41;</span>;
				minutesTXT = <span style="color: #004993;">minutes</span>;
				secondsTXT = seconds<span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">minutes</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
				tensTXT = <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>milliseconds<span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">seconds</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">//if the minutes, seconds or the tens of seconds number has only one character we add a &quot;0&quot; before it - that's just because we want the time to look good ;)</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>minutesTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> totalTXT = <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> minutesTXT;
				<span style="color: #0033ff; font-weight: bold;">else</span> totalTXT = <span style="color: #990000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> minutesTXT;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>secondsTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> totalTXT = totalTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> secondsTXT;
				<span style="color: #0033ff; font-weight: bold;">else</span> totalTXT = totalTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> secondsTXT;
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>tensTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> totalTXT = totalTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> tensTXT;
				<span style="color: #0033ff; font-weight: bold;">else</span> totalTXT = totalTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> tensTXT;
&nbsp;
				<span style="color: #009900;">//Update total time label </span>
				timeCanvas = FlexGlobals.topLevelApplication.timeDisplay;
				totalTimeLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;totalTimeTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
				totalTimeLabel.<span style="color: #004993;">text</span> = totalTXT;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//and the second function</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setBestLap<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>
&nbsp;
			<span style="color: #009900;">//this function does the exact same thing as the first one, only here we will use the time elapsed from the last time the car has passed the finish line</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> currentTime<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Date</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentLap<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>;
			currentLap = currentTime.<span style="color: #004993;">getTime</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;">this</span>.lapTime.<span style="color: #004993;">getTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">seconds</span><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: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">minutes</span><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: #6699cc; font-weight: bold;">var</span> minutesTXT<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: #6699cc; font-weight: bold;">var</span> secondsTXT<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: #6699cc; font-weight: bold;">var</span> tensTXT<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: #6699cc; font-weight: bold;">var</span> bestLapTXT<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> bestLapLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
			<span style="color: #6699cc; font-weight: bold;">var</span> timeCanvas<span style="color: #000000; font-weight: bold;">:</span>Canvas;			
&nbsp;
			<span style="color: #004993;">seconds</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span>currentLap<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">minutes</span> = <span style="color: #004993;">Math</span>.<span style="color: #004993;">floor</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">seconds</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">60</span><span style="color: #000000;">&#41;</span>;
			minutesTXT = <span style="color: #004993;">minutes</span>;
			secondsTXT = seconds<span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">minutes</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">60</span>;
			tensTXT = <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>currentLap<span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">seconds</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//if the minutes, seconds or the tens of seconds number has only one character we add a &quot;0&quot; before it - that's just because we want the time to look good ;)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>minutesTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> bestLapTXT = <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> minutesTXT;
			<span style="color: #0033ff; font-weight: bold;">else</span> bestLapTXT = <span style="color: #990000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> minutesTXT;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>secondsTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> bestLapTXT = bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> secondsTXT;
			<span style="color: #0033ff; font-weight: bold;">else</span> bestLapTXT = bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> secondsTXT;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>tensTXT<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span> bestLapTXT = bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> tensTXT;
			<span style="color: #0033ff; font-weight: bold;">else</span> bestLapTXT = bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;.&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> tensTXT;
&nbsp;
			<span style="color: #009900;">//we don't calculate the lap time if the car passes the finish/start line for the first time</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.bestLap<span style="color: #000000; font-weight: bold;">&gt;</span>currentLap<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.bestLap == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">this</span>.bestLap = currentLap;
				<span style="color: #009900;">//Update best lap time label </span>
				timeCanvas = FlexGlobals.topLevelApplication.timeDisplay;
				bestLapLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;bestLapTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
				bestLapLabel.<span style="color: #004993;">text</span> = bestLapTXT;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">// Save race history</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory == <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory = <span style="color: #990000;">&quot;Lap: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #0033ff; font-weight: bold;">this</span>.currentLap <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot; - &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span>;
			<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory = <span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory  <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;Lap: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #0033ff; font-weight: bold;">this</span>.currentLap <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot; - &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> bestLapTXT <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span>; 
&nbsp;
			<span style="color: #009900;">//we set the initial time to the moment the car passed the finish line </span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.lapTime = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><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> checkPoints<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> currentLapTXT<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentLapLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
			<span style="color: #6699cc; font-weight: bold;">var</span> timeCanvas<span style="color: #000000; font-weight: bold;">:</span>Canvas;
			<span style="color: #6699cc; font-weight: bold;">var</span> levelEndCanvas<span style="color: #000000; font-weight: bold;">:</span>Canvas;
			<span style="color: #6699cc; font-weight: bold;">var</span> historyText<span style="color: #000000; font-weight: bold;">:</span>Text;
&nbsp;
			<span style="color: #009900;">//we check to see if the car &quot;touches&quot; the checkpoint1 - start/finish line)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>FlexGlobals.topLevelApplication.finishLine.<span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span>player.<span style="color: #004993;">position</span>.<span style="color: #004993;">x</span>, player.<span style="color: #004993;">position</span>.<span style="color: #004993;">y</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// </span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentLap == <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;">this</span>.initialTime = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">this</span>.lapTime = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
					<span style="color: #0033ff; font-weight: bold;">this</span>.currentLap = <span style="color: #000000; font-weight:bold;">1</span>;
				<span style="color: #000000;">&#125;</span>
				<span style="color: #009900;">//if the current checkpoint is the mid track line - increase the lap number				</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentCheckpoint === <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #009900;">//check if this is the best lap</span>
					<span style="color: #0033ff; font-weight: bold;">this</span>.setBestLap<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;					
					<span style="color: #009900;">//if this is the final lap, move to the &quot;finish&quot; frame </span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentLap == totalLaps<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						historyText = <span style="color: #0033ff; font-weight: bold;">new</span> Text<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
						historyText.<span style="color: #004993;">text</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory;
						historyText.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">10</span>; historyText.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">115</span>;	
						FlexGlobals.topLevelApplication.currentState = <span style="color: #990000;">&quot;LevelEnd&quot;</span>;
						FlexGlobals.topLevelApplication.levelEndCanvas.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>historyText<span style="color: #000000;">&#41;</span>;
&nbsp;
					<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 current checkpoint is the start line - increase the lap number					</span>
						<span style="color: #0033ff; font-weight: bold;">this</span>.currentLap<span style="color: #000000; font-weight: bold;">++</span>;
					<span style="color: #000000;">&#125;</span>					
					currentLapTXT = <span style="color: #0033ff; font-weight: bold;">this</span>.currentLap <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;/&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> totalLaps;
&nbsp;
					<span style="color: #009900;">//Update cuurent lap label </span>
					timeCanvas = FlexGlobals.topLevelApplication.timeDisplay;
					currentLapLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;currentLapTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
					currentLapLabel.<span style="color: #004993;">text</span> = currentLapTXT;
&nbsp;
					<span style="color: #009900;">//we set to checkpoint to be checked to the next checkpoint </span>
					<span style="color: #0033ff; font-weight: bold;">this</span>.currentCheckpoint = <span style="color: #000000; font-weight:bold;">1</span>;					
				<span style="color: #000000;">&#125;</span>				
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900;">//we check to see if the car &quot;touches&quot; the checkpoint2 - mid track line)</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>FlexGlobals.topLevelApplication.checkpoint.<span style="color: #004993;">hitTestPoint</span><span style="color: #000000;">&#40;</span>player.<span style="color: #004993;">position</span>.<span style="color: #004993;">x</span>, player.<span style="color: #004993;">position</span>.<span style="color: #004993;">y</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">//if the current checkpoint is the start line - reached the mid track point				</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentCheckpoint === <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;">//we set to checkpoint to be checked is the next checkpoint </span>
					<span style="color: #0033ff; font-weight: bold;">this</span>.currentCheckpoint = <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>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> startupTimeClocks<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;">this</span>.initialTime = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;			
			<span style="color: #0033ff; font-weight: bold;">this</span>.lapTime = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.bestLap = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.currentLap = <span style="color: #000000; font-weight:bold;">0</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.currentCheckpoint = <span style="color: #000000; font-weight:bold;">1</span>;			
			<span style="color: #0033ff; font-weight: bold;">this</span>.raceHistory = <span style="color: #990000;">&quot;&quot;</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> startupTimeDisplay<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>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> timeCanvas<span style="color: #000000; font-weight: bold;">:</span>Canvas;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentLapLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
			<span style="color: #6699cc; font-weight: bold;">var</span> bestLapLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
			<span style="color: #6699cc; font-weight: bold;">var</span> totalTimeLabel<span style="color: #000000; font-weight: bold;">:</span>Label;
&nbsp;
			<span style="color: #009900;">//Update time display labels </span>
			timeCanvas = FlexGlobals.topLevelApplication.timeDisplay;
			currentLapLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;currentLapTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
			currentLapLabel.<span style="color: #004993;">text</span> = <span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>.currentLap <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> <span style="color: #990000;">&quot;/&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> totalLaps;
			bestLapLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;bestLapTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
			bestLapLabel.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;00.00.00&quot;</span>;
			totalTimeLabel = <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#40;</span>timeCanvas.<span style="color: #004993;">getChildByName</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;totalTimeTXT&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;			
			totalTimeLabel.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;00.00.00&quot;</span>;			
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Really a nice conversion.</p>
<p><a href="/wp-content/uploads/2010/07/FlexRacer.zip">Download the full source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/07/30/create-a-flash-racing-game-flex-version/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Claytus Hood Tower Defense case study</title>
		<link>http://www.emanueleferonato.com/2010/07/26/claytus-hood-tower-defense-case-study/</link>
		<comments>http://www.emanueleferonato.com/2010/07/26/claytus-hood-tower-defense-case-study/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 21:54:45 +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=3180</guid>
		<description><![CDATA[I always loved games that revive an old concept with new features, and Claytus Hood Tower Defense is one of them. Based on the old &#8220;tower defense&#8221; theme, it introduces new strategy options with large, scrollable maps and the capability to block some paths to make enemies take a larger route to attack you base. [...]]]></description>
			<content:encoded><![CDATA[<p>I always loved games that revive an old concept with new features, and <a href="http://www.claytus-towerdefense.com/" target = "_blank">Claytus Hood Tower Defense</a> is one of them.</p>
<p><a href="http://www.claytus-towerdefense.com/" target = "_blank"><img src="/wp-content/uploads/2010/07/Screen-shot-2010-07-26-at-11.44.12-PM.jpg" /></a></p>
<p>Based on the old &#8220;tower defense&#8221; theme, it introduces new strategy options with large, scrollable maps and the capability to block some paths to make enemies take a larger route to attack you base.</p>
<p><strong>Roux Raphaël</strong>, the author, shares with us his experience:</p>
<p>&laquo; <strong>Why did I decide to work on Claytus?</strong></p>
<p>Before that, I used to be a developer for a web agency, coding online shops or institutional communication websites&#8230; not that fun!</p>
<p>After I lost my job at the end of 2009, I decided to increase my knowledges in AS3, to code funny stuff like games for example!</p>
<p>I always learnt programming by myself (the geekish way), with books, resources found on the web and personal experience&#8230;</p>
<p>I thus decided to read Keith Peters&#8217; book &#8220;<a href="http://www.amazon.com/Foundation-Actionscript-3-0-Animation-Making/dp/1590597915" target = "_blank">AS3 animation &#8211; Making Things Move</a>&#8221; to fill my gaps in maths, and I saw lots of quite useful things to code games I had forgotten.</p>
<p>After reading this book, I managed to learn AI and Pathfinding, I thus studied A* algorithm.</p>
<p>Here are the examples I inspired myself of to learn A* :<br />
<a href="http://www.remixtechnology.com/view/AStar-haXe" target = "_blank">http://www.remixtechnology.com/view/AStar-haXe</a>.</p>
<p>Well, I reviewed trigonometry, I know A*, what can I do with it? A Tower Defense game! Let&#8217;s code!<span id="more-3180"></span></p>
<p>Claytus is thus my very first game, and I learnt lots of things about AS3 best practices while developing it.</p>
<p>Here are a few technical informations about Claytus&#8217; realization and technics I used for its design :</p>
<p>- it&#8217;s a full AS3 project developed with Flex Builder 3, a great software that allows to gain plenty of time as soon as you master it (spelling, auto import, debugging, profiling, etc.).</p>
<p>- I had to establish a good workflow between Flash for graphic assets and Flex for AS3 coding (do you Flax??). With the Embed tag, I succeeded into compiling the game in a single swf and I didn&#8217;t have to load external assets.</p>
<p>- Setting up the AS3 project in FlexBuilder 3 so that the MochiAds works well is really very tricky, with the add of a Preloader class before the Main class and compiler arguments (-frame 2 Main) so that the Main class is added to stage only on the second frame!</p>
<p>- I managed to set up optimization strategies for AS3, thanks to G Skinner tricks and benchmarks. (<a href="http://gskinner.com/talks/quick/" target = "_blank">http://gskinner.com/talks/quick/</a>)</p>
<p>For the first level, the use of A* isn&#8217;t so blatant; it&#8217;s gonna be much more in the following levels where you can block your enemies&#8217; way.</p>
<p>Today, my challenge is to avoid cheaters on my leader boards :&#8217;(</p>
<p><strong>About the TD wannamake</strong></p>
<p>I didn&#8217;t use the easiest way to code a TD game, it&#8217;s thus quite hard to give you basic tips. Here is a list of a few basics for coding TD :</p>
<p>- The game takes place on a Tilemap, with a Boolean for each tile which holds isWalkable for the A* and isBuildable for the turret build.</p>
<p>- When an Enemy spwans, it asks its path to A* and I put its reference on an Array (enemyArray).</p>
<p>- The turrets go through the enemyArray in search of an enemy on its range and then open fire if their Boolean isReloaded == true.</p>
<p>- To estimate the distances and so check the range, I use Manhattan distance formula, less accurate but faster than Euclidian distance.</p>
<p>- The non-reloaded turrets reload during a few frames to simulate the firerate and then start again to look for a potential target.</p>
<p>Of course the process is far more complex, if you need more information about a precise point, don&#8217;t hesitate to ask me!</p>
<p>Easier methods than A* exist to build a TD, if the level has only a single way, no need for A* but to give an information about the direction to take when going out of the tile, such as hard coded waypoints.</p>
<p><strong>Claytus&#8217; graphism</strong></p>
<p>In the beginning, Claytus used to be quite ugly, as a game skeleton with circles for the turrets and triangles for enemies, as for any experimental game skeleton I guess!</p>
<p>I thus offered to friends of mine who are illustrators to work with me on the project, to give life to my circles and triangles ! They were just finishing their studies and I think Claytus has been a good opportunity for them to increase their knowledges in Flash and add a valuable project to their portfolio. They learnt a lot about technical constraints that represents the integration of a drawing into a video game.</p>
<p>All of the Claytus&#8217; artworks were so designed on a graphics tablet coupled to Photoshop. I didn&#8217;t wanna use vectorial drawing to economize resources and keep an artistic look. Only using bitmaps, I get very good performances on an old computer on which Bloons TD 4 freezes =D.</p>
<p><strong>Claytus&#8217; music</strong></p>
<p>The music for Claytus TD was exclusively composed by friends of mine! The musicians didn&#8217;t know video games field that well, and they brought their point of view and an acoustic soundtrack. I love it!</p>
<p><strong>How to give freshness to an old concept</strong></p>
<p>It&#8217;s true it&#8217;s difficult to create something new in TD game because lots of things have already been done.</p>
<p>Lots of TD only send lots of waves and lots of enemies with a huge arsenal&#8230;</p>
<p>Finally, this gameplay type is much more of a massacre timekiller that you win whatever you do!</p>
<p>I wanted to create a much more challenging game with Claytus TD. I agree it can raise an issue to some of the players, but the fact that is challenging makes others happy!</p>
<p>The game begins with a single way, and only three different turrets, which poses Claytus as a real strategy and positioning game, you have to think every move you make!</p>
<p>In the other levels, we bring each time a novelty in gameplay : stage with other possibilities as multiple ways possible, new turrets and new upgrades in addition to gameplay elements of the stage.</p>
<p>I wanted every turret unique with a precise goal, that&#8217;s why you can&#8217;t do anything if you wanna win.</p>
<p><strong>About the funding</strong></p>
<p>Claytus wasn&#8217;t made to raise money at first thought, it was more of a project to help me learning to code in AS3 and manage a project with high quality standards.</p>
<p>I&#8217;m thus just incoming into flash game field and Mochimedia helps broadcasting the game on tenths of the websites of their partners.</p>
<p><strong>Website</strong></p>
<p>Claytus TD have its own website at <a href="www.claytus-towerdefense.com">http://www.claytus-towerdefense.com</a>.</p>
<p>I made it to showcase the game in a nice looking website with drawn background.</p>
<p>The website introduce the team, the French version have a forum (I will add an english forum asap), and people can mail me via the contact form.</p>
<p>I also upload preview pictures of the next levels and the next soundtracks because Claytus TD intend to have six levels and more, but its takes a lot of time to create all of this stuff!</p>
<p>To stay tuned, players can become fan on Facebook and subscribe to my<br />
newsletter.</p>
<p><strong>Level Editor</strong></p>
<p>I have build a level editor to help me making level and learning tool programming, coded in Flex, MXML &#038; AS3, great technology to deploy rich applications (RIA).</p>
<p><img src="/wp-content/uploads/2010/07/editor.jpg" /></p>
<p>It&#8217;s not released for the public at this day, but if I have time i&#8217;ll code a friendly user version for the people can make their own level and why not create a level contest? &raquo;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/07/26/claytus-hood-tower-defense-case-study/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
