<?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; Tutorial</title>
	<atom:link href="http://www.benoitfreslon.com/category/actionscript/tutorial/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: 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_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>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_975461733"
			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_975461733"
			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_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>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_1831466906"
			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_1831466906"
			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_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 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_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>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_1384600707"
			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_1384600707"
			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: 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_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>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_1673715280"
			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_1673715280"
			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_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>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_1636057630"
			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_1636057630"
			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_2121658229"
			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_2121658229"
			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_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>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_1281546060"
			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_1281546060"
			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>
		<item>
		<title>ActionScript: Orient an object to the mouse cursor</title>
		<link>http://www.benoitfreslon.com/orient-an-object-to-the-mouse-cursor</link>
		<comments>http://www.benoitfreslon.com/orient-an-object-to-the-mouse-cursor#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:17:14 +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[actionscript 3]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[orientation]]></category>

		<guid isPermaLink="false">http://www.thisisgameplay.com/?p=725</guid>
		<description><![CDATA[How to orient an objet to an other ? Just put those lines and change the instance name myTank. 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Orienter_un_objet_par_rapport_%C3%A0_un_autre1_2133549731"
			class="flashmovie"
			width="300"
			height="300">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/01/Orienter_un_objet_par_rapport_%C3%A0_un_autre1.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/01/Orienter_un_objet_par_rapport_%C3%A0_un_autre1.swf"
			name="fm_Orienter_un_objet_par_rapport_%C3%A0_un_autre1_2133549731"
			width="300"
			height="300">
	<!--<![endif]-->
		 
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object> 12345678910111213141516// This object will always look at the mouse cursor myTank.addEventListener&#40;Event.ENTER_FRAME, tankEnterFrame&#41;; // This function will be launched every frame (25 times by seconds); function tankEnterFrame&#40;pEvt&#41; &#123; &#160; [...]]]></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%2Forient-an-object-to-the-mouse-cursor&amp;title=ActionScript%3A%20Orient%20an%20object%20to%20the%20mouse%20cursor" 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>How to orient an objet to an other ? Just put those lines and change the instance name myTank. 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_Orienter_un_objet_par_rapport_%C3%A0_un_autre1_1287828182"
			class="flashmovie"
			width="300"
			height="300">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2010/01/Orienter_un_objet_par_rapport_%C3%A0_un_autre1.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2010/01/Orienter_un_objet_par_rapport_%C3%A0_un_autre1.swf"
			name="fm_Orienter_un_objet_par_rapport_%C3%A0_un_autre1_1287828182"
			width="300"
			height="300">
	<!--<![endif]-->
		 <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> 
	<!--[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 /></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;">// 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> 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>mouseY<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>mouseX<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</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></div></td></tr></tbody></table></div>
<p>Download the source code here : <a href="http://www.benoitfreslon.com/orient-an-object-to-the-mouse-cursor/www-thisisgameplay-com_orient_object_to_mouse_cursor/" rel="attachment wp-att-749">[www.thisisgameplay.com]_Orient_object_to_mouse_cursor.fla</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/orient-an-object-to-the-mouse-cursor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial: How to make a game like Jetman – step 1 – The Jetman</title>
		<link>http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman</link>
		<comments>http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman#comments</comments>
		<pubDate>Fri, 18 Dec 2009 17:06:36 +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[actionscript 3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[jetman]]></category>

		<guid isPermaLink="false">http://www.thisisgameplay.com/?p=543</guid>
		<description><![CDATA[In my first tutorial I will show you how to make a flash game like Jetman. A simple very famous casual game on facebook and very addictive. Play Jetman 1. First of all you have to download the Adobe Flash IDE: Adobe Flash CS3 or Adobe Flash CS4 We will code the game with ActionScript [...]]]></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%2Ftutorial-how-to-make-a-game-like-jetman-%25e2%2580%2593-step-1-%25e2%2580%2593-the-jetman&amp;title=Tutorial%3A%20How%20to%20make%20a%20game%20like%20Jetman%20%E2%80%93%20step%201%20%E2%80%93%20The%20Jetman" 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>In my first tutorial I will show you how to make a flash game like Jetman.<img class="alignleft" title="Jetman" src="http://photos-g.ak.fbcdn.net/photos-ak-sf2p/v43/106/4243149646/app_1_4243149646_252.gif" alt="" width="53" height="53" /><br />
A simple very famous casual game on facebook and very addictive.</p>
<p><a href="http://apps.facebook.com/playjetman/challenges.php?landing=true" target="_blank">Play Jetman</a></p>
<p><object width="425" height="344" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/ZWvvJc09jzs&amp;hl=fr_FR&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed width="425" height="344" type="application/x-shockwave-flash" src="http://www.youtube.com/v/ZWvvJc09jzs&amp;hl=fr_FR&amp;fs=1&amp;" allowFullScreen="true" allowscriptaccess="always" allowfullscreen="true" /></object></p>
<p><strong>1. First of all you have to download the Adobe Flash IDE: Adobe Flash CS3 or Adobe Flash CS4<br />
</strong></p>
<p>We will code the game with ActionScript 3.0 (the programming language of Flash) so you need Adobe Flash CS3 at least.<br />
You can download the free trial version of Adobe Flash here (Mac or Windows) : <a href="https://www.adobe.com/cfusion/tdrc/index.cfm?loc=en&amp;product=flash" target="_blank">https://www.adobe.com/cfusion/tdrc/index.cfm?loc=en&amp;product=flash</a></p>
<p><strong>2. Create your document<br />
</strong></p>
<ul>
<li>Open Flash.</li>
<li>Create a new ActionScript 3.0 document.</li>
<li>On the properties set the<strong> framerate to</strong> <strong>25fps.</strong></li>
<li>And set the <strong>background color </strong>to <strong>black.<br />
</strong></li>
</ul>
<p><strong>3. Create Jetman MovieClip</strong></p>
<p>Flash uses graphic objects called MovieClip. So we have to create the Jetman&#8217;s MovieClip.</p>
<ul>
<li>Click on the <strong>Rectangle tool</strong> on the tools bar. (on the left)</li>
</ul>
<p style="padding-left: 60px;"><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/create_box-2/" rel="attachment wp-att-552"><img class="size-full wp-image-552 alignnone" title="create_box" src="http://www.thisisgameplay.com/wp-content/uploads/2009/11/create_box1.jpg" alt="create_box" width="29" height="32" /></a></p>
<ul>
<li>Then drag and drop on the stage to <strong>create a rectangle</strong> about <strong>width:50px</strong> and <strong>height:15px</strong> (you can modify your shape dimentions later).</li>
<li>Select<strong> all your shape including the border</strong>. You can also use a<strong> double click.</strong></li>
<li>Do a right click on your selected shape and click on <strong>Convert to Symbol</strong>.</li>
<li>Write this symbol&#8217;s<strong> name</strong> : <strong>Jetman</strong> (not jetman or JETMAN).</li>
<li>Then click <strong>OK.</strong></li>
</ul>
<p>Now you can see your Symbol <strong>Jetman </strong>into your Library  (on the right). If you don&#8217;t see the Library Press F11 key.<br />
You have 1 instance of the Jetman symbol on your stage. Good. <strong>We just created a MovieClip</strong>.</p>
<p>In order to interact with the jetman MovieClip with ActionScript, we have the rename the instance&#8217;s name on the stage.</p>
<ul>
<li>Click on the Jetman&#8217;s instance on the stage.</li>
</ul>
<p style="padding-left: 60px;"><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/instance/" rel="attachment wp-att-554"><img class="alignnone size-full wp-image-554" title="instance" src="http://www.thisisgameplay.com/wp-content/uploads/2009/11/instance.jpg" alt="instance" width="82" height="53" /></a></p>
<ul>
<li>Then go to the properties on the bottom and set the instance&#8217;s name to <strong>jetman </strong>(not Jetman or JETMAN) it&#8217;s very important.</li>
</ul>
<p><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/instance_name/" rel="attachment wp-att-555"><img class="alignnone size-full wp-image-555" title="instance_name" src="http://www.thisisgameplay.com/wp-content/uploads/2009/11/instance_name.jpg" alt="instance_name" width="346" height="128" /></a></p>
<p>It&#8217;s done! Now we can move the instance with ActionScript.</p>
<p><strong>4. Starting to code</strong></p>
<p>You have differents ways to code with ActionScript. We will use the easier. (Coding into layers)</p>
<ul>
<li><strong>Create a new layer</strong> on the timeline (Click on the button on the left bottom corner).</li>
<li><strong>Put the layer on the top:</strong> drag and drop the layer.</li>
<li>You can <strong>rename</strong> the layer with a double click or Right Click then rename.</li>
<li>Then select the<strong> first frame</strong> (here in black).</li>
</ul>
<p><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/layer/" rel="attachment wp-att-556"><img class="alignnone size-full wp-image-556" title="layer" src="http://www.thisisgameplay.com/wp-content/uploads/2009/11/layer.jpg" alt="layer" width="188" height="94" /></a></p>
<ul>
<li>Finally go to the windows menu &gt; Actions or press the F9 key.</li>
</ul>
<p>A new windows is opening : The Actions window. We will to write all our code here.</p>
<ul>
<li>Copy and paste this Actionscript 3 code (After codinf into your layer you should see a little <strong>a</strong> into this frame):</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 /></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;">// Please don't remove this comment.</span><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.thisisgameplay.com</span><br />
<span style="color: #009900; font-style: italic;">//</span><br />
<br />
<span style="color: #009900; font-style: italic;">////// Game balance</span><br />
<span style="color: #009900; font-style: italic;">// Jetpack's boost speed</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speedBoost = <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Gravity</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> gravity = <span style="color: #000066; font-weight: bold;">.</span>8<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Speed limit</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speedMax = <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;">////// Global variables don't touch those one</span><br />
<span style="color: #009900; font-style: italic;">// Jetman is using his jetpack ?</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> boost = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// Jetman's current speed</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> speed = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// 25 times by seconds this function will be launched</span><br />
<span style="color: #339966; font-weight: bold;">function</span> jetmanEnterFrame<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;">// If boost == true</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>boost<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// The speed changes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed = speedBoost<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; <span style="color: #009900; font-style: italic;">// Else the gravity decrease jetman's speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;gravity<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the current speed is to hi</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>speed <span style="color: #000066; font-weight: bold;">&gt;</span> speedMax<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Limit the speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed = speedMax<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If the current speed is to low</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>speed <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000066; font-weight: bold;">-</span>speedMax<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Limit the speed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speed = &nbsp;<span style="color: #000066; font-weight: bold;">-</span> &nbsp;speedMax<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Every frame jetman will move vertically. (y axis)</span><br />
&nbsp; &nbsp; jetman<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> <span style="color: #000066; font-weight: bold;">+</span>= &nbsp;speed<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// If jetman is out of the screen</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>jetman<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;">450</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jetman<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</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 a event listener to launch the jetmanEnterFrame function every frames</span><br />
jetman<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> jetmanEnterFrame<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// If the mouse is pressed</span><br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">mouseDown</span><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;">// Set boost to true</span><br />
&nbsp; &nbsp; boost = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">// Id the mouse is released</span><br />
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">mouseUp</span><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;">// Set boost to false</span><br />
&nbsp; &nbsp; boost = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">// Detecting the mouse inputs (MOUSE_DOWN and MOUSE_UP) on the entire &quot;stage&quot;</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 />
<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_UP</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">mouseUp</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<ul>
<li>Finally test your game. (hit CTRL+ENTER)</li>
</ul>
<p>You should see that:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_www.thisisgameplay.com_Jetman_step1_856678318"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="http://www.benoitfreslon.com/wp-content/uploads/2009/12/www.thisisgameplay.com_Jetman_step1.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.benoitfreslon.com/wp-content/uploads/2009/12/www.thisisgameplay.com_Jetman_step1.swf"
			name="fm_www.thisisgameplay.com_Jetman_step1_856678318"
			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 the source file here:</p>
<p><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/www-thisisgameplay-com_jetman_step1/" rel="attachment wp-att-694">[www.thisisgameplay.com]_Jetman_step1.fla</a></p>
<p>Download the compiled file here:</p>
<p><a href="http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/www-thisisgameplay-com_jetman_step1-2/" rel="attachment wp-att-703">[www.thisisgameplay.com]_Jetman_step1.swf</a></p>
<p>Next step the level design&#8230; ;)</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">http://photos-g.ak.fbcdn.net/photos-ak-sf2p/v43/106/4243149646/app_1_4243149646_252.gif</div>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/tutorial-how-to-make-a-game-like-jetman-%e2%80%93-step-1-%e2%80%93-the-jetman/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

