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}} ]; } }
|