使用记录25_微信监听罗盘和加速度计

转载自 https://blog.csdn.net/haibo19981/article/details/81224132

1.罗盘
1.1.微信小游戏罗盘的API
https://developers.weixin.qq.com/minigame/dev/document/device/compass/wx.onCompassChange.html

罗盘适用的场景:
地图软件中自己的方向。
以垂直于手机屏幕中心为轴,返回一个角度,此角度就是现实中的方向。如下:
      0
      北
270 西  东 90
      南
      180

1.2.代码
//监听罗盘数据  
compass:function(){  
   wx.startCompass()  
   wx.onCompassChange(function(res){  
     console.log("direction:"+res.direction)  
   })  
   //wx.stopCompass()  
},  

2.加速度计
2.1.微信小游戏加速度计的API
https://developers.weixin.qq.com/minigame/dev/document/device/accelerometer/wx.onAccelerometerChange.html

加速度计适用的场景:
用于检测手机的各种摆放位置,比如屏幕朝上、屏幕朝下、手机立起来、手机横放等。
接口返回了3个变量:x、y、z,对应3个坐标轴。
将手机正面平放到地面,x轴方向是从左到右,y轴是从下到上,z轴穿出手机屏幕。
当手机处于静止状态时,手机此时只受一个重力加速度(1g=9.8m/s2)的作用,加速度计返回的res.x、res.y、res.z的值
就是设备的三轴受到的加速度的值,取值范围从[-1g,1g]。

设备以不同方式放置时,x/y/z的值如下(这里只是理论值):
                x   y    z 
手机正面立起    0   -1   0
手机立起向右横  1   0    0
手机倒立        0   1    0
手机立起向左横  -1  0    0
手机平放朝上    0   0    -1
手机平放朝下    0   0    1

根据加速度计三轴的值计算姿态角:
(1)Pitch(绕X轴旋转的角度)
当手机绕着自身的Y轴旋转(表示手机顶部或尾部翘起的角度),该角度会发生变化,值的范围是-180到180度。
Pitch = atan2(Y,Z)*180/M_PI;
(2)Roll(绕Y轴旋转的角度)
当设备绕着自身Y轴旋转时(表示手机左侧或右侧翘起的角度),该角度值将会发生变化,取值范围是-90到90度。
Roll = atan2(-X,sqrt(Y*Y+Z*Z))*180/M_PI;

// 57.3 = 180 / Math.PI
const Roll = Math.atan2(-res.x, Math.sqrt(res.y * res.y + res.z * res.z)) * 57.3;
const Pitch = Math.atan2(res.y, res.z) * 57.3;

以上详见以下链接
https://www.2cto.com/kf/201802/719912.html

2.2.网上的代码
//判断手机是否移动  
wx.startAccelerometer();
wx.onAccelerometerChange(function (e) {  
  if (Math.abs(e.x) > 1.1 && Math.abs(e.y) > 1.1){  
    wx.showToast({ title: "摇一摇" })  
    that.data.move = 2;  
  }else if (Math.abs(e.x) > 0.07 && Math.abs(e.y) > 0.02) {  
    wx.showToast({ title: "移动" })  
    that.data.move = 1;  
  } else {  
    wx.showToast({ title: "静止" })  
    that.data.move = 0;  
  }     
});
wx.stopAccelerometer();

注意:wx.stopAccelerometer() 只是停止监听加速度数据,并没有销毁监听事件。
当退出当前页,再次进入页面时,页面会再次注册一个监听事件,加上之前的就有两次。
目前的解决方法是在监听加速度计数据时用一个全局变量记录状态为true,第2次开始监听时判断这个
全局变量的状态是否为true,若为true,就不再调用wx.startAccelerometer() 。

2.3.实际开发小游戏中的代码
Common.js---------------
module.exports = {
    isStartAccelerometer: 0, //是否监听加速度计中(0关,1开)
    accelerometerState: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0], //加速度计的状态(有14关)
    setAccelerometerState: function(index){ //设置当前关加速度计的状态
        for(var i = 0; i < this.accelerometerState.length; i++){
            if(index == i){
                this.accelerometerState[i] = 1;
            }else{
                this.accelerometerState[i] = 0;
            }
        }
    },
    addAccelerometerEvent: function(callback){ //添加加速度计监听
        if(cc.sys.platform === cc.sys.WECHAT_GAME){ //微信平台
            if(this.isStartAccelerometer == 0){
                this.isStartAccelerometer = 1;
                wx.startAccelerometer(); 
            }
            wx.onAccelerometerChange(callback);
        }
    },
    stopAccelerometerEvent: function(){ //停止加速度计监听
        if(cc.sys.platform === cc.sys.WECHAT_GAME){ //微信平台
            if(this.isStartAccelerometer == 1){
                this.isStartAccelerometer = 0;
                for(var i = 0; i < this.accelerometerState.length; i++){
                    this.accelerometerState[i] = 0;
                }
                wx.stopAccelerometer(); 
            }
        }
    },
};
level46.js---------------
var common = require('Common');
cc.Class({
    onLoad: function() {
        common.setAccelerometerState(0); 
        common.addAccelerometerEvent(function(res){
            if(common.accelerometerState[0] == 1){
                console.log("46:x="+res.x + ",y=" + res.y + ",z=" + res.z);
                if(res.x > 0.3 && res.x < 1 && res.y > -0.3 && res.y < 1 && res.z > -1 && res.z < 0){ //手机立起向右横
                    this.start = true;
                }else{
                    this.start = false;
                }
            }
        }.bind(this));
    },
    playRightAction: function(){ //显示成功
        common.stopAccelerometerEvent();
    },
});

其他的:
if(res.x > -0.4 && res.x < 0.4 && res.y > 0.1 && res.y < 1 && res.z > -1 && res.z < 0.1) //手机倒立
if(res.x > 0 && res.x < 1 && res.y > -1.1 && res.y < -0.2 && res.z > 0.2 && res.z < 0.9) //手机朝下(与水平面夹角30-80度)
if(Math.abs(res.y) > 0.99 || Math.abs(res.z) > 0.99) //手机上抛
if (Math.abs(res.x) > 0.99 || Math.abs(res.y) > 0.99 || Math.abs(res.z) > 0.99) //摇晃手机

注意:实际打印加速度计的x、y、z,其都是如0.0004566 这样的浮点数。且加速度计只能检测手机自身坐标系下的
    相对位置,其不能检测出拿手机的人所处的位置。

3.震动
Common.js---------------
module.exports = {
    addVibrate: function(){ //添加手机震动
        if(cc.sys.platform === cc.sys.WECHAT_GAME){ //微信平台
            wx.vibrateLong();
        }
    },
};

猜你喜欢

转载自blog.csdn.net/cui6864520fei000/article/details/89225243