<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Benoit Freslon&#039;s blog  - Game Design, Flash games, ActionScript tutorials ... &#187; ActionScript3 as3</title>
	<atom:link href="http://www.benoitfreslon.com/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benoitfreslon.com</link>
	<description>Indie Game developer, learn Flash &#38; ActionScript with tutorials ...</description>
	<lastBuildDate>Mon, 16 Jan 2012 16:50:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>ActionScript: 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_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 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>

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

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

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

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

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><strong>Download source:</strong> <a href="http://www.benoitfreslon.com/wp-content/uploads/2011/01/www.benoitfreslon.com-Launch-a-function-after-a-delay-with-setTimeout.zip">www.benoitfreslon.com Launch a function after a delay with setTimeout.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/actionscript-launch-a-function-after-a-delay-with-settimeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash CS5 builds Iphone applications</title>
		<link>http://www.benoitfreslon.com/flash-cs5-builds-iphone-applications</link>
		<comments>http://www.benoitfreslon.com/flash-cs5-builds-iphone-applications#comments</comments>
		<pubDate>Tue, 06 Oct 2009 09:58:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://www.thisisgameplay.com/?p=523</guid>
		<description><![CDATA[Flash Professional CS5 will enable you to build applications for iPhone and iPod touch using ActionScript 3. These applications can be delivered to iPhone and iPod touch users through the Apple App Store.* A public beta of Flash Professional CS5 with prerelease support for building applications for iPhone is planned for later this year. Sign [...]]]></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%2Fflash-cs5-builds-iphone-applications&amp;title=Flash%20CS5%20builds%20Iphone%20applications" 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>Flash Professional CS5 will enable you to build applications for iPhone and iPod touch using ActionScript 3. These applications can be delivered to iPhone and iPod touch users through the Apple App Store.*</p>
<p>A public beta of Flash Professional CS5 with prerelease support for building applications for iPhone is planned for later this year. <a href="http://www.adobe.com/go/flashprobetanotify">Sign up to be notified when the beta starts</a>.</p>
<p>At MAX 2009, Adobe showed a number of applications and games for iPhone that have been built using a prerelease version of Flash Professional CS5. These applications are available now for you to download from the Apple App Store.</p>
<p>Go to the official article on labs.adobe.com: <a href="http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/#divider" target="_blank">http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/#divider</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/flash-cs5-builds-iphone-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New game, new gameplay, Roll and Jump</title>
		<link>http://www.benoitfreslon.com/new-game-new-gameplay-roll-and-jump</link>
		<comments>http://www.benoitfreslon.com/new-game-new-gameplay-roll-and-jump#comments</comments>
		<pubDate>Thu, 03 Sep 2009 16:47:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[ActionScript3 as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[skill game]]></category>

		<guid isPermaLink="false">http://www.thisisgameplay.com/?p=450</guid>
		<description><![CDATA[My new game, Roll and Jump, was designed for iphone and touch phone. The game is easy to learn but hard to master.]]></description>
			<content:encoded><![CDATA[<p><a href="./wp-content/uploads/2009/09/screenshot_02.jpg"><img class="size-full wp-image-455 alignright" title="roll_and_jump_screenshot" src="http://www.thisisgameplay.com/wp-content/uploads/2009/09/screenshot_02.jpg" alt="roll_and_jump_screenshot" width="116" height="174" /></a></p>
<p><img class="alignleft size-full wp-image-454" title="roll_n_jump_logo_mini_120_120" src="http://www.thisisgameplay.com/wp-content/uploads/2009/09/roll_n_jump_logo_mini_120_120.jpg" alt="roll_n_jump_logo_mini_120_120" width="94" height="94" />Roll and Jump is a skill game where the goal is to climb as high as possible by jumping from a rolling circle to another.</p>
<p>Clim and do the highest score as possible.<br />
You can do a wall jump, a double jump or activate slow motion!</p>
<p>The game will be released in few days on the web i hope to bring it on iphone as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benoitfreslon.com/new-game-new-gameplay-roll-and-jump/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

