运动传感器,设备中有个板载的感应器,感应器中有个轴用来感应移动。如x轴,从左到右,y轴从下到上,z轴,从后到钱。
Accelerometer 类。
flash.sensors.Accelerometer 类是新增加的ActionScript 类,用来接收从感应发过来得数据。是EveDispatcher 类的子类。
flash.sensors.AccelerometerEvent 是一个新的事件,返回了感应器的更新的信息
//检查设备是否支持重力感应
import flash.sensors.Accelerometer;
if(Accelerometer.isSuppported==false)
{
return;
}
如果用户不启用重力感应,则Accelerometer 类的muted 属性为flase。
重力感应,需要设置初始化一个Accelerotmter 的一个对象,
//
private var accelerometer:Accelerometer;
accelerometer=new Accelerometer();
import flash.events.AccelerometerEvent;
accelerometer.addEventListener(AccelerometerEvent.UPDATE,onUpdate);
function onUpdate(event:AccelerometerEvent):void
{
trace(event.accelerationX)
trace(event.accelerationY)
trace(event.accelerationZ);
trace(event.timestamp);
}
//可视化的状态呈现
接下来的应用程序,我们将展示重力感应在x,y,z轴上的值。创建一个颜色的工具条,x是红色,y是绿色,z是蓝色。中间的垂直线代表了
0点或者是重置状态。
我们来制作一个简单的沿着x和y轴的动画。我们将会移动这个ball,随着设备的移动。
public class SimpleBall extends Sprite
{
private const MULTIPLIER:Number = 8.0;
private var _accelerometer:Accelerometer;
private var _ball:Shape;
public function SimpleBall()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
}
private function init():void
{
if (Accelerometer.isSupported)
{
_ball = new Shape();
var g:Graphics = _ball.graphics;
g.beginFill(0xFF9900);
g.drawCircle(0, 0, 50);
g.endFill();
_ball.x = stage.stageWidth * 0.5;
_ball.y = stage.stageHeight * 0.5;
_ball.cacheAsBitmap = true;
addChild(_ball);
_accelerometer = new Accelerometer();
_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
}
}
private function onUpdate(event:AccelerometerEvent):void
{
_ball.x -= event.accelerationX * MULTIPLIER;
_ball.y += event.accelerationY * MULTIPLIER;
}
}
更新和渲染屏幕
边界
保持你的动画在屏幕的边界以内。设置边界当球儿的位置更新是改变他们。
public class Boundaries extends Sprite
{
private const MULTIPLIER:Number = 20.0;
private var _accelerometer:Accelerometer;
private var _vx:Number = 0.0;
private var _vy:Number = 0.0;
private var _ball:Shape;
private var _radius:int;
private var _xBounds:int;
private var _yBounds:int;
private var _newX:Number = 0.0;
private var _newY:Number = 0.0;
public function Boundaries()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
}
private function init():void
{
if (Accelerometer.isSupported)
{
_ball = new Shape();
var g:Graphics = _ball.graphics;
g.beginFill(0xFF9900);
g.drawCircle(0, 0, 50);
g.endFill();
_ball.x = stage.stageWidth * 0.5;
_ball.y = stage.stageHeight * 0.5;
_ball.cacheAsBitmap = true;
addChild(_ball);
_radius = _ball.width;
_xBounds = stage.stageWidth - _radius;
_yBounds = stage.stageHeight - _radius;
_accelerometer = new Accelerometer();
_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
private function onUpdate(event:AccelerometerEvent):void
{
_vx = event.accelerationX * MULTIPLIER;
_vy = event.accelerationY * MULTIPLIER;
}
private function onEnterFrame(event:Event):void
{
_newX = _ball.x - _vx;
_newY = _ball.y + _vy;
if (_newX > _radius && _newX < _xBounds)
{
_ball.x = _newX;
}
if (_newY > _radius && _newY < _yBounds)
{
_ball.y = _newY;
}
}
围绕中心旋转:
public class RotateCenter extends Sprite
{
private const MULTIPLIER:Number = 10.0;
private var _accelerometer:Accelerometer;
private var _ball:Shape;
private var _radius:int;
private var _xBounds:int;
private var _yBounds:int;
private var _centerX:int;
private var _centerY:int;
private var _vx:Number = 0.0;
private var _vy:Number = 0.0;
private var _newX:Number = 0.0;
private var _newY:Number = 0.0;
public function RotateCenter()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
}
private function init():void
{
if (Accelerometer.isSupported)
{
_ball = new Shape();
var g:Graphics = _ball.graphics;
g.beginFill(0xFF3300);
g.drawCircle(0, 0, 30);
g.beginFill(0xFFFF00);
g.drawCircle(10, 10, 10);
g.endFill();
_ball.x = stage.stageWidth * 0.5;
_ball.y = stage.stageHeight * 0.5;
_ball.cacheAsBitmap = true;
addChild(_ball);
_centerX = stage.stageWidth*0.5;
_centerY = stage.stageHeight*0.5;
_radius = _ball.width;
_xBounds = stage.stageWidth - _radius;
_yBounds = stage.stageHeight - _radius;
_accelerometer = new Accelerometer();
_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
private function onUpdate(event:AccelerometerEvent):void
{
_vx = event.accelerationX * MULTIPLIER;
_vy = event.accelerationY * MULTIPLIER;
}
private function onEnterFrame(event:Event):void
{
_newX = _ball.x - _vx;
_newY = _ball.y + _vy;
if (_newX > _radius && _newX < _xBounds)
{
_ball.x = _newX;
}
if (_newY > _radius && _newY < _yBounds)
{
_ball.y = _newY;
}
var dx:int = _centerX - _ball.x;
var dy:int = _centerY - _ball.y;
var radians:Number = Math.atan2(dy, dx);
_ball.rotation = radians*180/Math.PI;
}
}
震动:
震动设备是一个很常见的交互方式。你可以使用他来确定用户的强度,假设是一个真实的对象。
震动可以被定义为一个强烈的运动,关联了一个很大的值。这个例子中,当织大于边界值2.0 ,我们认为移动是一个震动。
public class Shake extends Sprite
{
private const THRESHOLD:Number = 1.5;
private var _accelerometer:Accelerometer;
private var _isMeasuring:Boolean = false;
private var _isShaking:Boolean = false;
public function Shake()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
}
private function init():void
{
if (Accelerometer.isSupported)
{
trace("is supported");
_accelerometer = new Accelerometer();
_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
}
}
private function onUpdate(event:AccelerometerEvent):void
{
if (_isMeasuring) {
return;
}
_isMeasuring = true;
trace("x", event.accelerationX);
trace("y", event.accelerationY);
trace("z", event.accelerationZ);
if (Math.abs(event.accelerationX) > THRESHOLD)
{
_isShaking = true;
}
if (Math.abs(event.accelerationY) > THRESHOLD*2)
{
_isShaking = true;
}
if (Math.abs(event.accelerationZ) > THRESHOLD*3)
{
_isShaking = true;
}
if (_isShaking)
{
trace("we have a shake");
}
_isMeasuring = false;
}
平滑的值
转载:http://www.cnblogs.com/csharponworking/archive/2011/08/04/2127990.html