<?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>Benoit Freslon&#039;s blog  - Game Design, Flash games, ActionScript tutorials ... &#187; ActionScript3 as3</title>
	<atom:link href="http://www.benoitfreslon.com/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benoitfreslon.com</link>
	<description>Indie Game developer, learn Flash &#38; ActionScript with tutorials ...</description>
	<lastBuildDate>Mon, 16 Jan 2012 16:50:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>ActionScript: Protect your Flash games on the web, sitelock all your games.</title>
		<link>http://www.benoitfreslon.com/actionscript-protect-your-flash-games-on-the-web-sitelock-all-your-games</link>
		<comments>http://www.benoitfreslon.com/actionscript-protect-your-flash-games-on-the-web-sitelock-all-your-games#comments</comments>
		<pubDate>Fri, 13 Jan 2012 15:29:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[protection]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[sitelock]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1877</guid>
		<description><![CDATA[If you want to submit your games on one game portals DO NOT UPLOAD YOUR GAMES WITHOUT A SITELOCK SCRIPT. WHY ? Your games can be &#8220;stolen&#8221; by game portals. Or if you didn&#8217;t finish your game it&#8217;s a really a &#8220;pain in the ass&#8221; to update your games spread on all the web. I [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-protect-your-flash-games-on-the-web-sitelock-all-your-games&amp;title=ActionScript%3A%20Protect%20your%20Flash%20games%20on%20the%20web%2C%20sitelock%20all%20your%20games." id="wpa2a_2"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>If you want to submit your games on one game portals DO NOT UPLOAD YOUR GAMES WITHOUT A SITELOCK SCRIPT.</p>
<p><strong>WHY ?</strong></p>
<p>Your games can be &#8220;stolen&#8221; by game portals. Or if you didn&#8217;t finish your game it&#8217;s a really a &#8220;pain in the ass&#8221; to update your games spread on all the web.</p>
<p>I tried lot of code for sitelocking my games but some of them are in ActionScript 2.0 or totally out of date. Some of them can&#8217;t work with the &#8220;https&#8221; or without the &#8220;www&#8221; characters.<br />
Another example when you to want to test your game on your local computer you have to comment the script. And you can forget to uncomment the sitelock script when you submit your game on the web.</p>
<p>So I decided to create my own script.</p>
<p>This is a short version of my script.<br />
But this one is perfect for all uses.</p>
<p><strong>Installation</strong></p>
<p>Copy and Paste this code and add your domains into the array &#8220;[ ]&#8221; separated by commas :</p>
<p>["kongregate.com", ..., ...]</p>
<p>You can lock the local mode by adding a second parameter : false<br />
The default value is set to true.</p>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sitelock<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;kongregate.com&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;benoitfreslon.com&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #3f5fbf;">/**<br />
&nbsp;* Sitelock your swf file on domains<br />
&nbsp;* <br />
&nbsp;* @param &nbsp; arrDomain &nbsp; List of all authorized domains<br />
&nbsp;* @param &nbsp; local &nbsp; &nbsp; &nbsp; This swf can be played on local computer ?<br />
&nbsp;*/</span><br />
<span style="color: #339966; font-weight: bold;">function</span> sitelock<span style="color: #000000;">&#40;</span>arrDomain<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000066; font-weight: bold;">,</span> local<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> isAut<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">root</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">loaderInfo</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> arr<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #004993;">root</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">loaderInfo</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">.</span><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: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">domain</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = arr<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><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: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// localmode</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;file://&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>local<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//trace(&quot;Local mode is autorized&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &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: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #0033ff; font-weight: bold;">in</span> arrDomain<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; arr = <span style="color: #004993;">domain</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">split</span><span style="color: #000000;">&#40;</span>arrDomain<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>arr<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isAut = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">domain</span> = arrDomain<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//trace(&quot;*** Sitelock: Domain found&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">break</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span> isAut<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//trace(&quot;*** Sitelock: Not authentified&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; destroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #3f5fbf;">/**<br />
&nbsp;* This function is called when the game is locked.<br />
&nbsp;*/</span><br />
<span style="color: #339966; font-weight: bold;">function</span> destroy<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Domain error</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;*** Domain Error ***&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">root</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-protect-your-flash-games-on-the-web-sitelock-all-your-games/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Good practices in Flash developement</title>
		<link>http://www.benoitfreslon.com/actionscript-good-practices-in-flash-developement</link>
		<comments>http://www.benoitfreslon.com/actionscript-good-practices-in-flash-developement#comments</comments>
		<pubDate>Tue, 15 Nov 2011 11:19:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1770</guid>
		<description><![CDATA[Here some tips and a list of good practices in Flash developement. These tips are usefull for coders and designers. Prepare your layers When you create a new .fla file prepare your layers like this : Add a new layers &#8220;Actions&#8221; on the top of the list. This layer will be dedicated to ActionScript. Then [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-good-practices-in-flash-developement&amp;title=ActionScript%3A%20Good%20practices%20in%20Flash%20developement" id="wpa2a_4"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>Here some tips and a list of good practices in <strong>Flash developement.<br />
</strong>These tips are usefull for coders <strong>and</strong> designers.<strong><br />
</strong></p>
<h2>Prepare your layers</h2>
<p>When you create a new .fla file prepare your layers like this :</p>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/11/layers.jpg"><img class="size-full wp-image-1771 alignnone" title="layers" src="http://www.benoitfreslon.com/wp-content/uploads/2011/11/layers-e1321294045943.jpg" alt="" width="219" height="95" /></a></p>
<p>Add a new layers &#8220;Actions&#8221; on the top of the list. This layer will be dedicated to ActionScript.<br />
Then lock this layer to avoid adding  graphic elements here.</p>
<h2>Lock the main script</h2>
<p>In order to keep in sight your main script, I recommend to lock this script by clicking on the pin on the bottom of the Actions pannel like this :</p>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/11/pin-script.jpg"><img class="size-full wp-image-1773 alignnone" title="pin script" src="http://www.benoitfreslon.com/wp-content/uploads/2011/11/pin-script.jpg" alt="" width="571" height="405" /></a></p>
<h2>Permit debugging</h2>
<p>Enabled the Flash debugger to display more efficient error messages.</p>
<p>File&gt; Publish settings&#8230; &gt; Permit debugging</p>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/11/debugging.jpg"><img class="alignnone size-full wp-image-1774" title="debugging" src="http://www.benoitfreslon.com/wp-content/uploads/2011/11/debugging.jpg" alt="" width="519" height="686" /></a></p>
<p>You can find this kind of message if you got an execution error : (My Flash player is in french language)</p>
<div class="codecolorer-container text default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">TypeError: Error #1009: Il est impossible d'accéder à la propriété ou à la méthode d'une référence d'objet nul.<br />
&nbsp; &nbsp; at Untitled_fla::MainTimeline/frame1()[Untitled_fla.MainTimeline::frame1:3]</div></td></tr></tbody></table></div>
<p>This error is located on the 1st frame on line 3. So useful !</p>
<p>However you must uncheck this option before publishing your swf on the web.</p>
<h2>The library</h2>
<p>Order you symbols and your medias in folders. Name your folders with explicit names.</p>
<p>Name your folders by using the prefix &#8220;_&#8221;.<br />
This tip can keep all your folders in the top of the list.<br />
And you can name a symbol or a media like your foler.</p>
<p>Example:  _Ball Folder own the Ball symbol.</p>
<p>The library orders elements and folders by alphabetical order.<br />
Your library is now more lisible.</p>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/11/LIbrary1.jpg"><img class="alignnone size-full wp-image-1777" title="LIbrary" src="http://www.benoitfreslon.com/wp-content/uploads/2011/11/LIbrary1.jpg" alt="" width="330" height="332" /></a></p>
<h2>Name your symbols and your medias correctly</h2>
<p>When you name your symbols or you media, particularly MovieClips, Istrongly recommand using a capital letter on the first character.</p>
<p><strong>Why ?</strong></p>
<p>When flash adds a new MovieClip with ActionScript he uses the class name exported for ActionScript.<br />
By convention a class name has always a capital letter on the first character.</p>
<pre>var mc = new BlueScreenOfTheDeath();
addChild(mc)</pre>
<p>However the symbol name match with the class name when you link the symbol for ActionScript. So keep the same name.</p>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/11/class1.jpg"><img class="alignnone size-full wp-image-1779" title="class" src="http://www.benoitfreslon.com/wp-content/uploads/2011/11/class1.jpg" alt="" width="539" height="636" /></a></p>
<h2>Naming convention</h2>
<p>ActionScript looks like Java so use the Java naming convention.</p>
<p><a href="http://en.wikipedia.org/wiki/Naming_convention_(programming)#Language-specific_conventions">http://en.wikipedia.org/wiki/Naming_convention_(programming)#Language-specific_conventions</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-good-practices-in-flash-developement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Tutorial How to create a gravity like a Mario platformer</title>
		<link>http://www.benoitfreslon.com/actionscript-tutorial-how-to-create-a-gravity-like-a-mario-plateformer</link>
		<comments>http://www.benoitfreslon.com/actionscript-tutorial-how-to-create-a-gravity-like-a-mario-plateformer#comments</comments>
		<pubDate>Wed, 02 Feb 2011 14:21:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[acceleration]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bounce]]></category>
		<category><![CDATA[gravity]]></category>
		<category><![CDATA[jump]]></category>
		<category><![CDATA[mario]]></category>
		<category><![CDATA[physic]]></category>
		<category><![CDATA[plateformer]]></category>
		<category><![CDATA[sonic]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1391</guid>
		<description><![CDATA[In this little tutorial you will learn how to create a gravity like a Mario platformer. Let&#8217;s start! Get files Save these 2 sprites (Right click &#62; Save as) Open Flash and create a new ActionScript 3.0 document. Document properties: 24 fps, 550&#215;400 by default. Create the Hero MovieClip Create a new Symbol: Insert &#62; [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-tutorial-how-to-create-a-gravity-like-a-mario-plateformer&amp;title=ActionScript%3A%20Tutorial%20How%20to%20create%20a%20gravity%20like%20a%20Mario%20platformer" id="wpa2a_6"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>In this little tutorial you will learn how to create a gravity like a Mario platformer.</p>
<p>Let&#8217;s start!</p>
<h3>Get files</h3>
<ul>
<li>Save these 2 sprites (Right click &gt; Save as)</li>
</ul>
<p style="text-align: center;"><img class="size-full wp-image-1392  aligncenter" title="mario_stand" src="http://www.benoitfreslon.com/wp-content/uploads/2011/02/mario_stand.png" alt="" width="52" height="64" /></p>
<p style="text-align: center;"><img class="size-full wp-image-1393  aligncenter" title="mario_jump" src="http://www.benoitfreslon.com/wp-content/uploads/2011/02/mario_jump.png" alt="" width="64" height="64" /></p>
<ul>
<li>Open Flash and create a new<strong> ActionScript 3.0 document.</strong></li>
<li>Document properties: <strong>24 fps, 550&#215;400 by default</strong>.</li>
</ul>
<h3>Create the Hero MovieClip</h3>
<ul>
<li>Create a new Symbol: <strong>Insert &gt; New symbol &gt; MovieClip</strong></li>
<li>Symbol name: <strong>Hero</strong></li>
<li>Check<strong> Export for ActionScript</strong></li>
<li>And Click<strong> OK</strong></li>
</ul>
<p><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/02/create_movieclip.jpg"><img class="size-full wp-image-1395 aligncenter" title="create_movieclip" src="http://www.benoitfreslon.com/wp-content/uploads/2011/02/create_movieclip.jpg" alt="" width="461" height="356" /></a></p>
<ul>
<li>Now you are in you Hero symbol! Perfect!</li>
<li>Import the first sprite: <a href="/wp-content/uploads/2011/02/mario_stand.png" target="_blank">mario_stand.png</a> on the first frame:</li>
<li><strong>File &gt; Import &gt; Import to stage &gt; mario_stand.png</strong></li>
<li>Set the sprite just on the top of the little black cross (pivot point)</li>
<li>Now create an empty keyframe on the timeline:</li>
<li><strong>Timeline &gt; 2nd frame on the timeline &gt; Right Click &gt; Create empty Keyframe</strong></li>
<li>Import the second sprite: <a href="/wp-content/uploads/2011/02/mario_jump.png" target="_blank">mario_jump.jpg</a> on the second keyframe</li>
<li>Then create a new layer on the top and set 2 labels names</li>
<li><strong>Insert &gt; Timeline &gt; New layer</strong></li>
<li>And add 2 labels names:</li>
<li>On the timeline <strong>click on the first key frame</strong> with the mario_stand.png sprite and go to the <strong>Properties pannel &gt; Label &gt; Name &gt; stand</strong></li>
<li>On the timeline <strong>click on the second key frame</strong> with the mario_jump.png sprite and go to the<strong> Properties pannel &gt; Label &gt; Name &gt; jump</strong></li>
</ul>
<p style="text-align: center;"><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/02/movieclip_mario.jpg"><img class="size-full wp-image-1406 aligncenter" title="movieclip_mario" src="http://www.benoitfreslon.com/wp-content/uploads/2011/02/movieclip_mario.jpg" alt="" width="310" height="504" /></a></p>
<h3>Add the jump script</h3>
<ul>
<li>Back to the main scene: <strong>Click on the Scene 1</strong></li>
<li>Add a new layer named &#8220;Actions&#8221;: <strong>Insert &gt; Timeline &gt; New layer</strong></li>
<li>And open the Actions pannel: <strong>Timeline &gt; Actions Layer &gt; Keyframe &gt; Right click &gt; Actions</strong></li>
<li><strong>Finally copy and paste this code:</strong></li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// The game gravity</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> gravity = <span style="color: #000000; font-weight:bold;">0.8</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// The floor position (y)</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">floor</span> = <span style="color: #000000; font-weight:bold;">300</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add the hero MovieClip</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> hero = <span style="color: #0033ff; font-weight: bold;">new</span> Hero<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Set the x position</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">550</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Set the y position on the floor</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">floor</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Set the hero state to &quot;stand&quot;</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;stand&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add hero on the display list</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>hero<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Create and set the speedY property to 0</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span>speedY = <span style="color: #000000; font-weight:bold;">0</span><br />
<span style="color: #009900; font-style: italic;">// Create and set the jump impulsion to 10</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span>impulsion = <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Add an enter frame event</span><br />
hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> heroEnterFrame<span style="color: #000000;">&#41;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> heroEnterFrame <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// On each frames...</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set the world gravity</span><br />
&nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span>speedY <span style="color: #000066; font-weight: bold;">+</span>= gravity<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Move the hero with his speedY</span><br />
&nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= hero<span style="color: #000066; font-weight: bold;">.</span>speedY<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the y property is larger than the floor position</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #004993;">floor</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set the hero on the floor</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">floor</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Cancel the current speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span>speedY = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Change the state to stand</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;stand&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add a mouse event on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">mouseDown</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// When the user click on the stage...</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the hero is standing</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentLabel</span> == <span style="color: #990000;">&quot;stand&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set the new speedY with the hero impulsion</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span>speedY = <span style="color: #000066; font-weight: bold;">-</span>hero<span style="color: #000066; font-weight: bold;">.</span>impulsion<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// And change the state to jump</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hero<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">gotoAndStop</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;jump&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer_792352770"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/wp-content/uploads/2011/02/www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/wp-content/uploads/2011/02/www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer.swf"
			name="fm_www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer_792352770"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><strong>Download sources:</strong> <a href="http://www.benoitfreslon.com/wp-content/uploads/2011/02/www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer.zip">www.benoitfreslon.com_Create_gravity_like_a_mario_plateformer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-tutorial-how-to-create-a-gravity-like-a-mario-plateformer/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ActionScript: Move an object with keyboard with absolute controls</title>
		<link>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-absolute-controls</link>
		<comments>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-absolute-controls#comments</comments>
		<pubDate>Tue, 01 Feb 2011 11:29:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[actionscrip]]></category>
		<category><![CDATA[car]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1361</guid>
		<description><![CDATA[In this tip I will show you how to move an object with keyboard with absolute controls. See also how to move an object with keyboard relative-camera control. What is the absolute controls? The direction of your object will be calculated depending the object orientation. (Biohazard, Micro Machines, Gears of War). This code is very [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-move-an-object-with-keyboard-with-absolute-controls&amp;title=ActionScript%3A%20Move%20an%20object%20with%20keyboard%20with%20absolute%20controls" id="wpa2a_8"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>In this tip I will show you how to move an object with keyboard with absolute controls.</p>
<p><a href="http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-absolute-controls">See also how to move an object with keyboard relative-camera control.</a></p>
<p><strong>What is the absolute controls?</strong></p>
<p>The direction of your object will be calculated depending the object orientation. (Biohazard, Micro Machines, Gears of War). This code is very usefull if you design a car game.</p>
<p><strong>How to make an absolute control in Flash?</strong></p>
<ul>
<li>Get the arrow key status (key is up/key is down);</li>
<li>Move or rotate the object on enter frame with the new velocity (Left and Right keys: rotate, Up key: forward, Down key: backward);</li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// Assign 4 booleans for the 4 arrow keys</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> keyLeft = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> keyRight = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add the keyboard event (KEY_DOWN) on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> pressKey<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> pressKey<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If an arrow key is down, switch the value to true to the assigned variable</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyLeft = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyRight = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">// Add the keyboard event (KEY_UP) on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_UP</span><span style="color: #000066; font-weight: bold;">,</span> releaseKey<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> releaseKey<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the arrow key is up, switch the value to false to the assigned variable</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">37</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyLeft = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">39</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyRight = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Set the velocity of the object</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speed = <span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// And the rotation speed</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> rotationSpeed = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add an enter frame event on the moving object</span><br />
myCircle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> circleEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> circleEnterFrame<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set the default velocity to 0 if no key is pressed</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> velocity = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">keyUp</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the key up is pressed set the new velocity to the speed value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; velocity = speed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">keyDown</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the key down is pressed set the new velocity to the half speed value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; velocity = <span style="color: #000066; font-weight: bold;">-</span>speed<span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>keyLeft<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// rotate the object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> <span style="color: #000066; font-weight: bold;">-</span>= rotationSpeed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>keyRight<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// rotate the object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> <span style="color: #000066; font-weight: bold;">+</span>= rotationSpeed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Convert the degreeAngle to the radian angle</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> angleRadian = pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">180</span> <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Move the object with the radian angle and the object speed</span><br />
&nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>angleRadian<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> velocity<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>angleRadian<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> velocity<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls_1991610463"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2011/02/www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2011/02/www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls.swf"
			name="fm_www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls_1991610463"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p>Download source: <a href="http://www.benoitfreslon.com/wp-content/uploads/2011/02/www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls1.zip">www.benoitfreslon.com_Move_an_object_with_keyboard_with_absolute_controls</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-absolute-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Move an object with keyboard with camera relative control</title>
		<link>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-camera-relative-control</link>
		<comments>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-camera-relative-control#comments</comments>
		<pubDate>Tue, 04 Jan 2011 16:22:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[arrows]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[move]]></category>
		<category><![CDATA[relative]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1287</guid>
		<description><![CDATA[In this tip I will show you how to move an object with keyboard with camera relative control. What is a camera relative control? The direction of your object will be calculated depending the camera position. (Mario 64, Uncharted, GTA 3, etc.). Eg: If press the right button the character will go on the right. [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-move-an-object-with-keyboard-with-camera-relative-control&amp;title=ActionScript%3A%20Move%20an%20object%20with%20keyboard%20with%20camera%20relative%20control" id="wpa2a_10"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>In this tip I will show you how to move an object with keyboard with camera relative control.</p>
<p><strong>What is a camera relative control? </strong></p>
<p>The direction of your object will be calculated depending the camera position. (Mario 64, Uncharted, GTA 3, etc.).<br />
Eg: If press the right button the character will go on the right.</p>
<p><strong>How to make a camera relative control? </strong></p>
<ul>
<li>Get the arrow key status (key is up/key is down)</li>
<li>Set the direction vector</li>
<li>Move the object on enter frame with the direction vector</li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// Assign 4 booleans for the 4 arrow keys</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> keyLeft = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> keyRight = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add the keyboard event (KEY_DOWN) on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> pressKey<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> pressKey<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If an arrow key is down, switch the value to true to the assigned variable</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyLeft = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyRight = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">// Add the keyboard event (KEY_UP) on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_UP</span><span style="color: #000066; font-weight: bold;">,</span> releaseKey<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> releaseKey<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the arrow key is up, switch the value to false to the assigned variable</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">38</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyUp</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">40</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">keyDown</span> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyLeft = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <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>pEvent<span style="color: #000066; font-weight: bold;">.</span><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><br />
&nbsp; &nbsp; &nbsp; &nbsp; keyRight = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Set the velocity of the object</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speed = <span style="color: #000000; font-weight:bold;">6</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add an enter frame event on the moving object</span><br />
myCircle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> circleEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> circleEnterFrame<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Create a 2D vector in order to get the object direction according the arrow keys</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> vector = <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: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">keyUp</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If keyUp is true add the -1 value on the y axis</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">keyDown</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If keyDown is true add the 1 value on the y axis</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>keyLeft<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If keyLeft is true add the -1 value on the x axis</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>keyRight<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If keyRight is true add the 1 value on the x axis</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Get the radian angle of the direction</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span>vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If vector lenght is not null</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; 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><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Move the object with the angle and the object speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</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: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</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: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>[kml_flashembed publishmethod="static" fversion="10.0.0" movie="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Move-an-objet-with-keyboard-with-camera-relative-control.swf" width="550" height="400" targetclass="flashmovie"]</p>
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object><br />
Donwload source: <a href="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Move-an-objet-with-keyboard-with-camera-relative-control.zip">www.benoitfreslon.com-Move-an-objet-with-keyboard-with-camera-relative-control.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-move-an-object-with-keyboard-with-camera-relative-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Move an object to coordinates with smoothing</title>
		<link>http://www.benoitfreslon.com/actionscript-move-an-object-to-coordinates-with-smoothing</link>
		<comments>http://www.benoitfreslon.com/actionscript-move-an-object-to-coordinates-with-smoothing#comments</comments>
		<pubDate>Mon, 03 Jan 2011 15:53:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[3]]></category>
		<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[move]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[smooth]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1271</guid>
		<description><![CDATA[There is a simple formula to move an object to coordinates with smoothing. The speed will decrease with smooth during the time. speed = distance_between_objects / smoothing The distance will decrease because the distance will decrease too. If I set values I got: object.x = 50 targetX = 100 speedX = (targetX &#8211; object.x) / [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-move-an-object-to-coordinates-with-smoothing&amp;title=ActionScript%3A%20Move%20an%20object%20to%20coordinates%20with%20smoothing" id="wpa2a_12"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>There is a simple formula to move an object to coordinates with smoothing.<br />
The speed will decrease with smooth during the time.</p>
<blockquote><p><strong>speed = distance_between_objects / smoothing</strong></p></blockquote>
<p>The distance will decrease because the distance will decrease too.</p>
<p>If I set values I got:</p>
<blockquote><p>object.x = 50<br />
targetX = 100</p></blockquote>
<blockquote><p><strong>speedX</strong> = (targetX &#8211; object.x)  / 5<br />
<strong><br />
On the first frame: speed = 10<br />
</strong>object.x += (100 &#8211; 50) /5 &gt; 50/5 &gt; <strong>10<br />
&gt;</strong> object.x = 50+10 = 60</p>
<p><strong>On the second frame: speed = 8</strong><br />
object.x += (100 &#8211; 60) /5 &gt; 40/5 = <strong>8<br />
&gt;</strong> object.x = 60 + 8 = 68</p>
<p><strong>On the third frame: speed = 6,4<br />
</strong>object.x += (100 &#8211; 68) / 5 &gt; 32/5 = <strong>6,4<br />
&gt;</strong> object.x = 68 + 6,4 = 74,4</p>
<p>etc.</p>
<p><strong>On the 100th frame: speed = 0<br />
</strong>object.x += (100 &#8211; 100) / 5 &gt; 0 /5 = <strong>0</strong><br />
<strong> &gt;</strong> object.x = 100 + 0 = 100</p></blockquote>
<p><strong>Source:</strong></p>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// Add a mouse event on the stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">click</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">click</span><span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// On click set the new x and y coordinates</span><br />
&nbsp; &nbsp; myBall<span style="color: #000066; font-weight: bold;">.</span>targetX = <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseX</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; myBall<span style="color: #000066; font-weight: bold;">.</span>targetY = <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">mouseY</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Set default coordinates</span><br />
myBall<span style="color: #000066; font-weight: bold;">.</span>targetX = <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">;</span><br />
myBall<span style="color: #000066; font-weight: bold;">.</span>targetY = <span style="color: #000000; font-weight:bold;">200</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add an enter frame event on the ball</span><br />
myBall<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> ballEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> ballEnterFrame<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// On enter frame move the ball to the target coordinates</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">b</span> = pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>targetX <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>targetY <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Exemple:</strong></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.benoitfreslon.com-Move-an-object-to-coordinates-with-smoothing_518538470"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Move-an-object-to-coordinates-with-smoothing.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Move-an-object-to-coordinates-with-smoothing.swf"
			name="fm_www.benoitfreslon.com-Move-an-object-to-coordinates-with-smoothing_518538470"
			width="550"
			height="400">
	<!--<![endif]-->
		<br />
<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><strong>Download source: </strong><a href="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Move-an-object-to-coordinates-with-smoothing.zip">www.benoitfreslon.com Move an object to coordinates with smoothing.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-move-an-object-to-coordinates-with-smoothing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Launch a function after a delay with setTimeout</title>
		<link>http://www.benoitfreslon.com/actionscript-launch-a-function-after-a-delay-with-settimeout</link>
		<comments>http://www.benoitfreslon.com/actionscript-launch-a-function-after-a-delay-with-settimeout#comments</comments>
		<pubDate>Mon, 03 Jan 2011 14:35:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[clearTimeout]]></category>
		<category><![CDATA[clock]]></category>
		<category><![CDATA[delay]]></category>
		<category><![CDATA[pause]]></category>
		<category><![CDATA[setTimeout]]></category>
		<category><![CDATA[timer]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1241</guid>
		<description><![CDATA[There are different ways to launch a function after a delay: With the enter frame event; With the Timer class; With a motion tween; With the enter frame event and the getTimer() method; And with the setTimeout function (See setTimeout help on Adobe); How to master the setTimeout ? First of all you must create a [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-launch-a-function-after-a-delay-with-settimeout&amp;title=ActionScript%3A%20Launch%20a%20function%20after%20a%20delay%20with%20setTimeout" id="wpa2a_14"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>There are different ways to launch a function after a delay:</p>
<ul>
<li>With the enter frame event;</li>
<li>With the Timer class;</li>
<li>With a motion tween;</li>
<li>With the enter frame event and the getTimer() method;</li>
<li>And with the <strong>setTimeout</strong> function (<a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#setInterval()" target="_blank">See setTimeout help on Adobe</a>);</li>
</ul>
<p><strong>How to master the setTimeout ?</strong></p>
<ul>
<li>First of all you <strong>must </strong>create a variable assigned to the <strong>setTimeout</strong> identifier;</li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> intervalIndentifier</div></td></tr></tbody></table></div>
<ul>
<li>The setTimeout function returns an identifier (uint) when you call this function. Save this identifier into the assigned variable;</li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// The hello function will be launched in 2000 ms</span><br />
intervalIdentifier = <span style="color: #004993;">setTimeout</span><span style="color: #000000;">&#40;</span>hello<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">2000</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> hello<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;function launched&quot;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<ul>
<li><strong>This identifier must be clear </strong>with the clearTimeout function in order to stop the setTimeout;</li>
</ul>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">clearTimeout</span><span style="color: #000000;">&#40;</span>intervalIdentifier<span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>
<p><strong>NB:</strong></p>
<ul>
<li>If you want to launch a setInterval already launched don&#8217;t forget to clear the previous setInterval. In witch case the previous setInterval won&#8217;t be stopped;</li>
<li>The setTimeout function <strong>can&#8217;t be</strong> <strong>paused or resumed;</strong></li>
<li>Assign one variable by one setTimout;</li>
<li>The setTimeout function is <strong>based on clock time</strong> not on frames time;</li>
<li>When you clear the setTimeout the closure function is not launched;</li>
<li>You don&#8217;t have to clear the setTimeout when the setTimeout is already finished;</li>
</ul>
<p><strong>Add parameters</strong></p>
<p>The setTimeout function can receive one or several arguments. Those arguments are passed to the closure function.<br />
To add arguments just add your arguments after the second parameter into the setTimeout function:<br />
Obviously your closure function must have the same number of arguments.</p>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">intervalIdentifier = <span style="color: #004993;">setTimeout</span><span style="color: #000000;">&#40;</span>hello<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">2000</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;world&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;www.benoitfreslon.com&quot;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> hello<span style="color: #000000;">&#40;</span>arg1<span style="color: #000066; font-weight: bold;">,</span> arg2<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>arg1<span style="color: #000066; font-weight: bold;">,</span> arg2<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Exemple:</strong></p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout_1997928665"
			class="flashmovie"
			width="400"
			height="250">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout.swf"
			name="fm_www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout_1997928665"
			width="400"
			height="250">
	<!--<![endif]-->
		<br />
<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><strong>Download source:</strong> <a href="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout.zip">www.benoitfreslon.com Launch a function after a delay with setTimeout.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-launch-a-function-after-a-delay-with-settimeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript: Throw bullets to mouse direction</title>
		<link>http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction</link>
		<comments>http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction#comments</comments>
		<pubDate>Mon, 27 Dec 2010 12:50:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[angle]]></category>
		<category><![CDATA[bullet]]></category>
		<category><![CDATA[direction]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[radian]]></category>
		<category><![CDATA[shoot em up]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1225</guid>
		<description><![CDATA[After calculating the angle of 2 graphic objects, you can also use this angle to throw a bullet to the object direction with the trigonometry. How ? Calculating the angle (in radian) between 2 graphics objects with the Math.atan2 method (cf. Orient object to the mouse cursor). On click, display a bullet to the &#8220;tank&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-throw-bullets-to-mouse-direction&amp;title=ActionScript%3A%20Throw%20bullets%20to%20mouse%20direction" id="wpa2a_16"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>After calculating the angle of 2 graphic objects, you can also use this angle to throw a bullet to the object direction with the trigonometry.</p>
<p><strong>How ?</strong></p>
<ol>
<li>Calculating the angle (in radian) between 2 graphics objects with the <strong><a href="http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary451.html" target="_blank">Math.atan2</a></strong> method (cf. <a href="./orient-an-object-to-the-mouse-cursor" target="_blank">Orient object to the mouse cursor</a>).</li>
<li>On click, display a bullet to the &#8220;tank&#8221; position and add an &#8220;enter frame&#8221; event on this bullet.</li>
<li>And save the angle between the tank and the mouse cursor on the new  bullet.</li>
<li>Use this angle on the &#8220;enter frame&#8221; event to calculate the x and y speed with the cosinus and the sinus.</li>
</ol>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.benoitfreslon.com_Throw_bullets_to_mouse_direction_1264051346"
			class="flashmovie"
			width="640"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/12/www.benoitfreslon.com_Throw_bullets_to_mouse_direction.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/12/www.benoitfreslon.com_Throw_bullets_to_mouse_direction.swf"
			name="fm_www.benoitfreslon.com_Throw_bullets_to_mouse_direction_1264051346"
			width="640"
			height="400">
	<!--<![endif]-->
		<br />
<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MovieClip</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Code by Benoit Freslon.</span><br />
<span style="color: #009900; font-style: italic;">// Tutorials, Flash games:</span><br />
<span style="color: #009900; font-style: italic;">// http://www.benoitfreslon.com</span><br />
<br />
<span style="color: #009900; font-style: italic;">// This object will always look at the mouse cursor</span><br />
myTank<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> &nbsp;tankEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// This function will be launched every frame (25 times by seconds);</span><br />
<span style="color: #339966; font-weight: bold;">function</span> tankEnterFrame<span style="color: #000000;">&#40;</span>pEvt<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// pEvt.currentTarget: myTank</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> mc = pEvt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Get the radian angle between the tank and the cursor</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// You can also replace mouseX and mouseY by another coordinates</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> angleRadian = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseY</span> <span style="color: #000066; font-weight: bold;">-</span> mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">-</span> mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Convert the radian angle in dedree angle</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> angleDegree = angleRadian <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">180</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set the orientation</span><br />
&nbsp; &nbsp; mc<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> = angleDegree<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Display angle of rotation in degree</span><br />
&nbsp; &nbsp; txtAngle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">text</span> = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>angleDegree<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot;°&quot;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add a mouse down event on stage</span><br />
<span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">mouseDown</span><span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Create a new bullet</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">b</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Bullet<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Set his position to the tank position</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = myTank<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = myTank<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Save the randian angle between the mouse and the tank</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// This angle will set the direction of the bullet</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>angleRadian = <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">mouseY</span> <span style="color: #000066; font-weight: bold;">-</span> myTank<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #004993;">mouseX</span> <span style="color: #000066; font-weight: bold;">-</span>myTank<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Add an enter frame event on each bullet</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> bulletEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Add this display object on the display list</span><br />
&nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Velocity of each bullet</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speed = <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> bulletEnterFrame<span style="color: #000000;">&#40;</span>pEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Get the current object (Bullet)</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">b</span> = pEvent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Move this bullet on each frames</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// On X axis use the cosinus angle</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>angleRadian<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// On Y axis use the sinus angle</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;<span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>angleRadian<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> speed<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Orient the bullet to the direction</span><br />
&nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">rotation</span> = <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span>angleRadian <span style="color: #000066; font-weight: bold;">*</span> <span style="color: #000000; font-weight:bold;">180</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">PI</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// You have to remove each created bullet</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// So after every moves you must check bullet position</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the bullet is out of the screen</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">400</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">400</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Remove it from the display list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// /!\ AND REOMOVE HIS EVENT LISTER</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> bulletEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Download source: <a href="http://www.benoitfreslon.com/wp-content/uploads/2010/12/www.benoitfreslon.com_Throw_bullets_to_mouse_direction.zip" target="_self">www.benoitfreslon.com_Throw_bullets_to_mouse_direction.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-throw-bullets-to-mouse-direction/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript: Detect collision between 2 circles</title>
		<link>http://www.benoitfreslon.com/actionscript-detect-collision-between-2-circles</link>
		<comments>http://www.benoitfreslon.com/actionscript-detect-collision-between-2-circles#comments</comments>
		<pubDate>Fri, 03 Dec 2010 11:02:25 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[2 collision]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[pythagoeran]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1204</guid>
		<description><![CDATA[With the Pythagoeran theorem you can detect collision with 2 circles. You just have to get the rays of 2 circles with the width property divided by 2. Actually the width property returns the diameter of the circle. So, you can calculte the ray like this: ray = diamter / 2 ray = circle.width / [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-detect-collision-between-2-circles&amp;title=ActionScript%3A%20Detect%20collision%20between%202%20circles" id="wpa2a_18"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>With the Pythagoeran theorem you can detect collision with 2 circles.</p>
<p>You just have to get the rays of 2 circles with the width property divided by 2.<br />
Actually the width property returns the diameter of the circle. So, you can calculte the ray like this:</p>
<blockquote><p><strong>ray = diamter / 2<br />
ray = circle.width / 2</strong></p></blockquote>
<p>Therefore if you get the distance between the 2 circles you can detect the collision like this:</p>
<blockquote><p><strong>if distance &lt; ( circle1.width/2+ circle2.width/2 ) is true =&gt; Collision</strong></p></blockquote>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Detect_collision_with_2_circles_307121800"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/12/Detect_collision_with_2_circles.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/12/Detect_collision_with_2_circles.swf"
			name="fm_Detect_collision_with_2_circles_307121800"
			width="550"
			height="400">
	<!--<![endif]-->
		

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// Start enter frame event</span><br />
<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ENTER_FRAME</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">enterFrame</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">enterFrame</span><span style="color: #000000;">&#40;</span>pEvent<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Get distance between the 2 MovieClips</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> dist<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = getDistance<span style="color: #000000;">&#40;</span>myCircleBlu<span style="color: #000066; font-weight: bold;">,</span>myCircleRed<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Test the distance and compare with the 2 rays</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>dist <span style="color: #000066; font-weight: bold;">&lt;</span> myCircleRed<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000066; font-weight: bold;">+</span> &nbsp;myCircleBlu<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If collision is detected change the opacity</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myCircleRed<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myCircleBlu<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myCircleRed<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myCircleBlu<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> getDistance<span style="color: #000000;">&#40;</span>pObj1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span><span style="color: #000066; font-weight: bold;">,</span>pObj2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> distX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = pObj1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> pObj2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> distY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = pObj1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> pObj2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>distX <span style="color: #000066; font-weight: bold;">*</span> distX <span style="color: #000066; font-weight: bold;">+</span> distY <span style="color: #000066; font-weight: bold;">*</span> distY<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-detect-collision-between-2-circles/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ActionScript: Get distance between 2 MovieClips</title>
		<link>http://www.benoitfreslon.com/actionscript-get-distance-between-2-movieclips</link>
		<comments>http://www.benoitfreslon.com/actionscript-get-distance-between-2-movieclips#comments</comments>
		<pubDate>Thu, 25 Nov 2010 15:09:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Tip]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[2]]></category>
		<category><![CDATA[collision]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[movieclip]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[pythagore]]></category>

		<guid isPermaLink="false">http://www.benoitfreslon.com/?p=1195</guid>
		<description><![CDATA[If you want to get the distance in pixels between 2 graphic objects just use the Pythagorean theorem in ActionScript :). c² = a² + b² 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Get_Distance_1081624142"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/11/Get_Distance.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/11/Get_Distance.swf"
			name="fm_Get_Distance_1081624142"
			width="550"
			height="400">
	<!--<![endif]-->
		 
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object> Download source : Get_Distance.zip 12345function getDistance&#40;pObj1:MovieClip,pObj2:MovieClip&#41;:Number &#123; &#160; &#160; var distX:Number = pObj1.x - pObj2.x; &#160; &#160; var distY:Number = pObj1.y [...]]]></description>
			<content:encoded><![CDATA[<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.benoitfreslon.com%2Factionscript-get-distance-between-2-movieclips&amp;title=ActionScript%3A%20Get%20distance%20between%202%20MovieClips" id="wpa2a_20"><img src="http://www.benoitfreslon.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p><p>If you want to get the distance in pixels between 2 graphic objects just use the Pythagorean theorem in ActionScript :).</p>
<blockquote><p><strong>c² = a² + b²</strong></p></blockquote>
<p><strong></strong><br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Get_Distance_1655392854"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/11/Get_Distance.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/11/Get_Distance.swf"
			name="fm_Get_Distance_1655392854"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>Download source : <a href="http://www.benoitfreslon.com/wp-content/uploads/2010/11/Get_Distance.zip">Get_Distance.zip</a></p>
<div class="codecolorer-container actionscript3 default .codecolorer" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339966; font-weight: bold;">function</span> getDistance<span style="color: #000000;">&#40;</span>pObj1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span><span style="color: #000066; font-weight: bold;">,</span>pObj2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> distX<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = pObj1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> <span style="color: #000066; font-weight: bold;">-</span> pObj2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> distY<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = pObj1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">-</span> pObj2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">Math</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>distX <span style="color: #000066; font-weight: bold;">*</span> distX <span style="color: #000066; font-weight: bold;">+</span> distY <span style="color: #000066; font-weight: bold;">*</span> distY<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-get-distance-between-2-movieclips/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

