来吧,展示
主要属性函数定义
egret
主要方法
private orientation = new egret.DeviceOrientation();
this.orientation.addEventListener(egret.Event.CHANGE, this.onOrientation, this);
this.orientation.start();
this.orientation.stop();
private motion = new egret.Motion();
this.motion.addEventListener(egret.Event.CHANGE, this.onMotion, this);
this.motion.start();
this.motion.stop();
监听主要字段
onOrientation(e: egret.OrientationEvent){/*code*/}
e.beta
e.gamma
e.alpha
onMotion(e: egret.MotionEvent) {/*code*/}
const acc = e.acceleration;
acc.x
acc.y
acc.z
const accig = e.accelerationIncludingGravity;
accig.x
accig.y
accig.z
const rr = e.rotationRate;
rr.beta
rr.gamma
rr.alpha
创建按钮启动停止监听
- 在游戏场景中创建可滚动窗口并将定义的空间容器加入到滚动窗口中
private container: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();let stageW = this.stage.stageWidth;
let stageH = this.stage.stageHeight;const scrollView: egret.ScrollView = new egret.ScrollView();
scrollView.setContent(this.container);
scrollView.width = stageW;
scrollView.height = stageH;
this.addChild(scrollView);
- 创建按钮加入到
container
容器中,并配置点击事件
private createStartOrStopBtn(stageW) {const btnGroup: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();this.container.addChild(btnGroup);btnGroup.width = stageW;btnGroup.y = 800;const startBtn: egret.Shape = new egret.Shape();startBtn.graphics.beginFill(0x0000ff);startBtn.graphics.drawRect(0, 0, 150, 50);startBtn.graphics.endFill();startBtn.x = 50;btnGroup.addChild(startBtn);const startBtnText: egret.TextField = new egret.TextField();startBtnText.size = 30;startBtnText.text = "START";startBtnText.x = 50;startBtnText.width = 150;startBtnText.height = 50;startBtnText.textColor = 0xff0000;startBtnText.textAlign = egret.HorizontalAlign.CENTER;startBtnText.verticalAlign = egret.VerticalAlign.MIDDLE;btnGroup.addChild(startBtnText);startBtn.touchEnabled = true;startBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onChangeStart, this);const stopBtn: egret.Shape = new egret.Shape();stopBtn.graphics.beginFill(0x0000ff);stopBtn.graphics.drawRect(0, 0, 150, 50);stopBtn.graphics.endFill();stopBtn.x = 250;btnGroup.addChild(stopBtn);const stopBtnText: egret.TextField = new egret.TextField();stopBtnText.size = 30;stopBtnText.text = "STOP";stopBtnText.x = 250;stopBtnText.width = 150;stopBtnText.height = 50;stopBtnText.textColor = 0xff0000;stopBtnText.textAlign = egret.HorizontalAlign.CENTER;stopBtnText.verticalAlign = egret.VerticalAlign.MIDDLE;btnGroup.addChild(stopBtnText);stopBtn.touchEnabled = true;stopBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onChangeStop, this);
}
private orientation = new egret.DeviceOrientation();
private motion = new egret.Motion();
private onChangeStart() {this.orientation.addEventListener(egret.Event.CHANGE, this.onOrientation, this);this.orientation.start();this.motion.addEventListener(egret.Event.CHANGE, this.onMotion, this);this.motion.start();
}
private onChangeStop() {this.orientation.stop();this.motion.stop();
}
监听并输出事件返回参数
private orientationText: egret.TextField = new egret.TextField();
private motionText: egret.TextField = new egret.TextField();this.container.addChild(this.orientationText);
this.container.addChild(this.motionText);
private onOrientation(e: egret.OrientationEvent) {console.log(e);const beta = String(Math.floor(e.beta).toFixed(2));const gamma = String(Math.floor(e.gamma).toFixed(2));const alpha = String(Math.floor(e.alpha).toFixed(2));this.orientationText.textFlow = <Array<egret.ITextElement>>[{ text: "在alpha,beta和gamma三个轴向的角度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n(β)绕 X 轴的角度:" },{ text: beta, style: { "textColor": 0xff0000 } },{ text: "\n(γ)绕 Y 轴的角度:" },{ text: gamma, style: { "textColor": 0xff0000 } },{ text: "\n(α)绕 Z 轴的角度:" },{ text: alpha, style: { "textColor": 0xff0000 } },]}
private onMotion(e: egret.MotionEvent) {console.log(e);const acc = e.acceleration;const accig = e.accelerationIncludingGravity;const rr = e.rotationRate;this.motionText.textFlow = <Array<egret.ITextElement>>[{ text: "acceleration:", style: { "textColor": 0x3399ea } },{ text: "\n在 X Y Z 轴方向的加速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 m/s2,不包含重力", style: { "textColor": 0x58c7f1 } },{ text: "\nX 轴方向的加速度:" },{ text: Number(acc.x).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nY 轴方向的加速度:" },{ text: Number(acc.y).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nZ 轴方向的加速度:" },{ text: Number(acc.z).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n\naccelerationIncludingGravity:", style: { "textColor": 0x3399ea } },{ text: "\n在 X Y Z 轴方向的加速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 m/s2,包含重力", style: { "textColor": 0x58c7f1 } },{ text: "\nX 轴方向的加速度:" },{ text: Number(accig.x).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nY 轴方向的加速度:" },{ text: Number(accig.y).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nZ 轴方向的加速度:" },{ text: Number(accig.z).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n\nrotationRate:", style: { "textColor": 0x3399ea } },{ text: "\n在alpha,beta和gamma三个轴向的角速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 角度每秒", style: { "textColor": 0x58c7f1 } },{ text: "\n(β)绕 X 轴的角速度:" },{ text: Number(rr.beta).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n(γ)绕 Y 轴的角速度:" },{ text: Number(rr.gamma).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n(α)绕 Z 轴的角速度:" },{ text: Number(rr.alpha).toFixed(2), style: { "textColor": 0xff0000 } }];
}
完整代码
class Main extends egret.DisplayObjectContainer {private container: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();private orientationText: egret.TextField = new egret.TextField();private motionText: egret.TextField = new egret.TextField();private orientation = new egret.DeviceOrientation();private motion = new egret.Motion();public constructor() {super();this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);}private onChangeStart() {this.orientation.addEventListener(egret.Event.CHANGE, this.onOrientation, this);this.orientation.start();this.motion.addEventListener(egret.Event.CHANGE, this.onMotion, this);this.motion.start();}private onChangeStop() {this.orientation.stop();this.motion.stop();}private onOrientation(e: egret.OrientationEvent) {console.log(e);const beta = String(Math.floor(e.beta).toFixed(2));const gamma = String(Math.floor(e.gamma).toFixed(2));const alpha = String(Math.floor(e.alpha).toFixed(2));this.orientationText.textFlow = <Array<egret.ITextElement>>[{ text: "在alpha,beta和gamma三个轴向的角度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n(β)绕 X 轴的角度:" },{ text: beta, style: { "textColor": 0xff0000 } },{ text: "\n(γ)绕 Y 轴的角度:" },{ text: gamma, style: { "textColor": 0xff0000 } },{ text: "\n(α)绕 Z 轴的角度:" },{ text: alpha, style: { "textColor": 0xff0000 } },]}private onMotion(e: egret.MotionEvent) {console.log(e);const acc = e.acceleration;const accig = e.accelerationIncludingGravity;const rr = e.rotationRate;this.motionText.textFlow = <Array<egret.ITextElement>>[{ text: "acceleration:", style: { "textColor": 0x3399ea } },{ text: "\n在 X Y Z 轴方向的加速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 m/s2,不包含重力", style: { "textColor": 0x58c7f1 } },{ text: "\nX 轴方向的加速度:" },{ text: Number(acc.x).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nY 轴方向的加速度:" },{ text: Number(acc.y).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nZ 轴方向的加速度:" },{ text: Number(acc.z).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n\naccelerationIncludingGravity:", style: { "textColor": 0x3399ea } },{ text: "\n在 X Y Z 轴方向的加速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 m/s2,包含重力", style: { "textColor": 0x58c7f1 } },{ text: "\nX 轴方向的加速度:" },{ text: Number(accig.x).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nY 轴方向的加速度:" },{ text: Number(accig.y).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\nZ 轴方向的加速度:" },{ text: Number(accig.z).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n\nrotationRate:", style: { "textColor": 0x3399ea } },{ text: "\n在alpha,beta和gamma三个轴向的角速度信息", style: { "textColor": 0x58c7f1 } },{ text: "\n单位是 角度每秒", style: { "textColor": 0x58c7f1 } },{ text: "\n(β)绕 X 轴的角速度:" },{ text: Number(rr.beta).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n(γ)绕 Y 轴的角速度:" },{ text: Number(rr.gamma).toFixed(2), style: { "textColor": 0xff0000 } },{ text: "\n(α)绕 Z 轴的角速度:" },{ text: Number(rr.alpha).toFixed(2), style: { "textColor": 0xff0000 } }];}private onAddToStage(event: egret.Event) {egret.lifecycle.addLifecycleListener((context) => {context.onUpdate = () => {}})egret.lifecycle.onPause = () => {egret.ticker.pause();}egret.lifecycle.onResume = () => {egret.ticker.resume();}this.runGame().catch(e => {console.log(e);})}private async runGame() {this.createGameScene();}private createStartOrStopBtn(stageW) {const btnGroup: egret.DisplayObjectContainer = new egret.DisplayObjectContainer();this.container.addChild(btnGroup);btnGroup.width = stageW;btnGroup.y = 800;const startBtn: egret.Shape = new egret.Shape();startBtn.graphics.beginFill(0x0000ff);startBtn.graphics.drawRect(0, 0, 150, 50);startBtn.graphics.endFill();startBtn.x = 50;btnGroup.addChild(startBtn);const startBtnText: egret.TextField = new egret.TextField();startBtnText.size = 30;startBtnText.text = "START";startBtnText.x = 50;startBtnText.width = 150;startBtnText.height = 50;startBtnText.textColor = 0xff0000;startBtnText.textAlign = egret.HorizontalAlign.CENTER;startBtnText.verticalAlign = egret.VerticalAlign.MIDDLE;btnGroup.addChild(startBtnText);startBtn.touchEnabled = true;startBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onChangeStart, this);const stopBtn: egret.Shape = new egret.Shape();stopBtn.graphics.beginFill(0x0000ff);stopBtn.graphics.drawRect(0, 0, 150, 50);stopBtn.graphics.endFill();stopBtn.x = 250;btnGroup.addChild(stopBtn);const stopBtnText: egret.TextField = new egret.TextField();stopBtnText.size = 30;stopBtnText.text = "STOP";stopBtnText.x = 250;stopBtnText.width = 150;stopBtnText.height = 50;stopBtnText.textColor = 0xff0000;stopBtnText.textAlign = egret.HorizontalAlign.CENTER;stopBtnText.verticalAlign = egret.VerticalAlign.MIDDLE;btnGroup.addChild(stopBtnText);stopBtn.touchEnabled = true;stopBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onChangeStop, this);}private createGameScene() {let stageW = this.stage.stageWidth;let stageH = this.stage.stageHeight;const scrollView: egret.ScrollView = new egret.ScrollView();scrollView.setContent(this.container);scrollView.width = stageW;scrollView.height = stageH;this.addChild(scrollView);this.container.x = 10;this.container.y = 10;this.container.addChild(this.orientationText);this.container.addChild(this.motionText);this.orientationText.y = 0;this.motionText.y = 150;this.createStartOrStopBtn(stageW);}}