在最新版本的egret(5.0.12)中,已经支持了移动设备陀螺仪和运动状态的相关API。 egret. DeviceOrientation 可以监听设备方向的变化。 egret. Motion从用户设备读取运动状态信息并派发 CHANGE 事件。当设备移动时,传感器会检测到此移动并返回设备加速度,重力和旋转数据。 废话不多说,直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

class Main extends egret.DisplayObjectContainer {

private orientationLabel: egret.TextField;

private motionLabel: egret.TextField;

public constructor() {
super();

this.orientationLabel = new egret.TextField();
this.orientationLabel.width = 640;
this.orientationLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.orientationLabel);

this.motionLabel = new egret.TextField();
this.motionLabel.width = 640;
this.motionLabel.y = 300;
this.motionLabel.textAlign = egret.HorizontalAlign.CENTER;
this.addChild(this.motionLabel);

//创建 DeviceOrientation 类
var orientation = new egret.DeviceOrientation();
//添加事件监听器
orientation.addEventListener(egret.Event.CHANGE,this.onOrientation,this);
//开始监听设备方向变化
orientation.start();

//创建 Motion 类
var motion = new egret.Motion();
//添加事件监听器
motion.addEventListener(egret.Event.CHANGE,this.onMotion,this);
//开始监听设备方向变化
motion.start();
}

private onOrientation(e:egret.OrientationEvent){
this.orientationLabel.textFlow = >[
{text: "绕 Z 轴的角度:\n"}
, {text: e.alpha, style: {"textColor": 0xff0000}}
, {text: "\n\n绕 X 轴的角度:\n"}
, {text: e.beta, style: {"textColor": 0xff0000}}
, {text: "\n\n绕 Y 轴的角度:\n"}
, {text: e.gamma, style: {"textColor": 0xff0000}}
];
}

private onMotion(e:egret.MotionEvent){
this.motionLabel.textFlow = >[
{text: "X 轴方向的加速度\n"}
, {text: e.acceleration.x, style: {"textColor": 0xff0000}}
, {text: "\n\nY 轴方向的加速度:\n"}
, {text: e.acceleration.y, style: {"textColor": 0xff0000}}
, {text: "\n\nZ 轴方向的加速度:\n"}
, {text: e.acceleration.z, style: {"textColor": 0xff0000}}
, {text: "\n\nX 轴加速度(考虑重力加速度)\n"}
, {text: e.accelerationIncludingGravity.x, style: {"textColor": 0xff0000}}
, {text: "\n\nZ 轴方向的加速度:\n"}
, {text: e.accelerationIncludingGravity.y, style: {"textColor": 0xff0000}}
, {text: "\n\nZ 轴方向的加速度:\n"}
, {text: e.accelerationIncludingGravity.z, style: {"textColor": 0xff0000}}
, {text: "\n\n左右旋转速度(alpha轴向的角速度)\n"}
, {text: e.rotationRate.alpha, style: {"textColor": 0xff0000}}
, {text: "\n\n前后旋转速度(beta轴向的角速度)\n"}
, {text: e.rotationRate.beta, style: {"textColor": 0xff0000}}
, {text: "\n\n扭转速度(gamma轴向的角速度)\n"}
, {text: e.rotationRate.gamma, style: {"textColor": 0xff0000}}
];
}
}

首先创建了DeviceOrientation的实例orientation。并给他添加了一个事件侦听器,监听它的CHANGE事件。然后调用了他的start()方法来开始监听设备方向的变化。 当系统监听到了方向的变化后,将回调函数 onOrientation。然后通过egret. OrientationEvent事件的三个属性来获取设备的方向变化信息。 OrientationEvent的三个属性:alpha, beta和gamma。 alpha表示设备绕 Z 轴的角度,单位是 角度 beta 表示设备绕 X 轴的角度,单位是 角度 gamma 表示设备绕 Y 轴的角度,单位是 角度 同样,创建了Motion的实例motion。并给他添加了一个事件侦听器,监听它的CHANGE事件。然后调用了他的start()方法来开始监听设备方向的变化。 当系统监听到了方向的变化后,将回调函数 onMotion。然后通过egret. MotionEvent事件的三个属性来获取设备运动的具体信息。 acceleration 表示设备在 X Y Z 轴方将的加速度信息,单位是 m/s2,不包含重力。 acceleration 表示设备在 X Y Z 轴方将的加速度信息,单位是 m/s2,包含重力。 rotationRate 表示设备在 alpha、 beta 和 gamma 三个轴向的角速度信息,单位是 角度每秒。 最终的效果demo->传送门 或者手机扫一扫:

egret陀螺仪demo