<?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>Jimmy&#039;s Blog &#187; Accelerometer</title>
	<atom:link href="http://www.jimmy.sh/tag/accelerometer/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jimmy.sh</link>
	<description>Live in code! Inspired genuine trusted</description>
	<lastBuildDate>Fri, 09 Jul 2010 06:43:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>flash CS5开发iPhone应用［1.水平感应］</title>
		<link>http://www.jimmy.sh/flash/flash-cs5-iphone-accelerometer.html</link>
		<comments>http://www.jimmy.sh/flash/flash-cs5-iphone-accelerometer.html#comments</comments>
		<pubDate>Fri, 25 Dec 2009 13:57:22 +0000</pubDate>
		<dc:creator>Jimmy</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Accelerometer]]></category>
		<category><![CDATA[Flash CS5]]></category>

		<guid isPermaLink="false">http://www.jimmy.sh/?p=1476</guid>
		<description><![CDATA[在iPhone应用中，又很多游戏是使用水平感应（重力感应）来控制的，比较有意思的如Doodle Jump,Space Ball等，还有些赛车游戏直接把iPhone模拟成方向盘，转动你的iPhone来控制方向。 这些在Flash cs5中都可以轻松实现。 AS新增加类包 flash.sensors.Accelerometer 加速度传感器 Property： isSupported : Boolean －静态变量，返回机器是否支持加速度感应 Methods： Accelerometer() －创建加速度感应器 setRequestedUpdateInterval(interval:Number):void － 设置判断加速度的更新时间间隔 Events： update －加速度更新事件 update发生后返回的3个参数 accelerationX－x轴的加速度 accelerationY－Y轴的加速度 accelerationZ－Z轴的加速度(负数表示iPhone为正面朝上，正数则是反面朝上) 实例下载 原文件 ipa 加速度 Accelerometer Accelerometer.ipa 实例源代码 /* @author jimmy * www.jimmy.sh */ import flash.display.Sprite; import flash.events.AccelerometerEvent; import flash.events.Event; import flash.sensors.Accelerometer; import flash.text.TextField; import flash.text.TextFieldAutoSize; &#160; //3个方向的加速度 var accX:Number=0; [...]]]></description>
			<content:encoded><![CDATA[<p>在iPhone应用中，又很多游戏是使用水平感应（重力感应）来控制的，比较有意思的如Doodle<br />
Jump,Space Ball等，还有些赛车游戏直接把iPhone模拟成方向盘，转动你的iPhone来控制方向。</p>
<p>这些在Flash cs5中都可以轻松实现。</p>
<p>AS新增加类包 f<span style="color: #333399;">lash.sensors.Accelerometer</span> 加速度传感器</p>
<p>Property：<br />
<span style="color: #333399;"> isSupported</span> : <span style="color: #800000;">Boolean</span> －静态变量，返回机器是否支持加速度感应</p>
<p>Methods：</p>
<p><span style="color: #333399;">Accelerometer</span>() －创建加速度感应器</p>
<p><span style="color: #333399;">setRequestedUpdateInterval</span>(<span style="color: #800000;">interval:Number</span>):void － 设置判断加速度的更新时间间隔</p>
<p>Events：</p>
<p><span style="color: #333399;">update</span> －加速度更新事件</p>
<p>update发生后返回的3个参数</p>
<p><span style="color: #333399;">accelerationX</span>－x轴的加速度</p>
<p><span style="color: #333399;">accelerationY</span>－Y轴的加速度</p>
<p><span style="color: #333399;">accelerationZ</span>－Z轴的加速度(负数表示iPhone为正面朝上，正数则是反面朝上)</p>
<table style="background-color: #ffffff; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 124px; height: 14px; background-color: #f5fbff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; margin: 0px;"><span style="letter-spacing: 0px;">实例下载</span></p>
</td>
<td style="width: 124px; height: 14px; background-color: #f5fbff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; margin: 0px;"><span style="letter-spacing: 0px;">原文件</span></p>
</td>
<td style="width: 149px; height: 14px; background-color: #f5fbff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; margin: 0px;"><span style="letter-spacing: 0px;">ipa</span></p>
</td>
</tr>
<tr>
<td style="width: 124px; height: 14px; background-color: #ffffff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; margin: 0px;">加速度</p>
</td>
<td style="width: 124px; height: 14px; background-color: #ffffff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; color: #001fa9; margin: 0px;"><span style="text-decoration: underline;"><a href="http://www.jimmy.sh/wp-content/uploads/2009/12/Accelerometer.zip">Accelerometer</a></span></p>
</td>
<td style="width: 149px; height: 14px; background-color: #ffffff; padding: 5px;" valign="top">
<p style="line-height: 19px; font: normal normal normal 12px/normal Helvetica; color: #001fa9; margin: 0px;"><span style="text-decoration: underline;"><a href="http://www.jimmy.sh/wp-content/uploads/2009/12/Accelerometer.ipa.zip">Accelerometer.ipa</a></span></p>
</td>
</tr>
</tbody>
</table>
<p><a href="http://www.jimmy.sh/wp-content/uploads/2009/12/IMG_0308.PNG"><img style="border: 0px initial initial;" title="加速度" src="http://www.jimmy.sh/wp-content/uploads/2009/12/IMG_0308.PNG" alt="加速度" width="320" height="480" /></a><br />
实例源代码<br />
<span id="more-1476"></span></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/* @author jimmy
 * www.jimmy.sh
*/</span>
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.AccelerometerEvent;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> flash.sensors.Accelerometer;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextFieldAutoSize</span>;
&nbsp;
<span style="color: #009900;">//3个方向的加速度</span>
<span style="color: #6699cc; font-weight: bold;">var</span> accX<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> accY<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> accZ<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #000000; font-weight:bold;">0</span>;
<span style="color: #009900;">//更新加速度</span>
<span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span>AccelerometerEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	accX=e.accelerationX;
	accY=e.accelerationY;
	accZ=e.accelerationZ;
<span style="color: #000000;">&#125;</span>
<span style="color: #009900;">//更新现实</span>
<span style="color: #339966; font-weight: bold;">function</span> Show<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> ballX<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ball.<span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">+</span>accX<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">50</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> ballY<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ball.y<span style="color: #000000; font-weight: bold;">-</span>accY<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">50</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> dx<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ballX<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">160</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> dy<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=ballY<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">240</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> jd<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">atan2</span><span style="color: #000000;">&#40;</span>dy,dx<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sqrt</span><span style="color: #000000;">&#40;</span>dx<span style="color: #000000; font-weight: bold;">*</span>dx<span style="color: #000000; font-weight: bold;">+</span>dy<span style="color: #000000; font-weight: bold;">*</span>dy<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>d<span style="color: #000000; font-weight: bold;">&amp;</span>lt;=<span style="color: #000000; font-weight:bold;">120</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		ball.<span style="color: #004993;">x</span>=ballX;
		ball.<span style="color: #004993;">y</span>=ballY;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">d</span>=<span style="color: #000000; font-weight:bold;">120</span>;
		ball.<span style="color: #004993;">x</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">cos</span><span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">120</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">160</span>;
		ball.<span style="color: #004993;">y</span>=<span style="color: #004993;">Math</span>.<span style="color: #004993;">sin</span><span style="color: #000000;">&#40;</span>jd<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">120</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">240</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #009900;">//设定球的大小</span>
	ball.<span style="color: #004993;">width</span>=ball.<span style="color: #004993;">height</span>=<span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">120</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">30</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">10</span>;
	<span style="color: #009900;">//更新文字</span>
	textX.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;accelerationX: &quot;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>accX<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	textY.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;accelerationY: &quot;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>accY<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	textZ.<span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;accelerationZ: &quot;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span>accZ<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #009900;">//画线，画圈</span>
	<span style="color: #004993;">graphics</span>.<span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span>,0xFFFFFF<span style="color: #000000;">&#41;</span>;
	<span style="color: #004993;">graphics</span>.<span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">160</span>,<span style="color: #000000; font-weight:bold;">240</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span>ball.<span style="color: #004993;">x</span>,ball.<span style="color: #004993;">y</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">160</span>,<span style="color: #000000; font-weight:bold;">240</span>,<span style="color: #004993;">d</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #009900;">//新建一个加速感应器</span>
<span style="color: #6699cc; font-weight: bold;">var</span> acc<span style="color: #000000; font-weight: bold;">:</span>Accelerometer=<span style="color: #0033ff; font-weight: bold;">new</span> Accelerometer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//添加侦听</span>
acc.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>AccelerometerEvent.UPDATE,update<span style="color: #000000;">&#41;</span>;
<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>,Show<span style="color: #000000;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jimmy.sh/flash/flash-cs5-iphone-accelerometer.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
