Egret 摇一摇功能

一  什么是重力感应

二 什么是陀螺仪

三 Egret中的摇一摇是利用什么原理实现的

四 摇一摇代码

一 什么是重力感应

二 什么是陀螺仪

三 Egret中的摇一摇是利用什么原理实现的

大概是利用手机陀螺仪,获取手机在x,y,z轴上的旋转角度变化。  -_-!!

四 摇一摇代码

Egret官方教程:http://developer.egret.com/cn/github/egret-docs/Engine2D/multimedia/gyro/index.html

摇一摇简单示例,网上有大量示例可参考

var orientation = new egret.DeviceOrientation();
orientation.addEventListener(egret.Event.CHANGE,this.onOrientation,this);
orientation.start();

private xPos: number = 0;
private yPos: number = 0;
private zPos: number = 0;
private last_x: number = 0;
private last_y: number = 0;
private last_z: number = 0;
private shakeCount: number = 0;  //摇动次数
private speed:number = 30;
private onOrientation(e: egret.OrientationEvent) {
    this.xPos = e.beta;   //x轴
    this.yPos = e.gamma;  //y轴
    this.zPos = e.alpha ; //z轴
    if(Math.abs(this.last_x - this.xPos)>speed || Math.abs(this.last_y - this.yPos)>speed || Math.abs(this.last_z - zPos)>speed){
        this.last_x = this.xPos;
        this.last_y = this.yPos;
        this.last_z = this.zPos;
        this.shakeCount ++;
    }
}
原文地址:https://www.cnblogs.com/gamedaybyday/p/6099771.html