【咸鱼教程】Egret实现摇一摇功能

教程目录
一 实现原理
二 代码
三 Demo下载


一 实现原理
监听设备旋转角度的变化,来判断用户是否摇动手机。

参考:
智能手机里陀螺仪和重力感应有何区别?
HTML5实现摇一摇的功能
Egret官方陀螺仪教程


二 代码
摇一摇工具类ShakeTool使用范例

[Actionscript3]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
private shakeTest(){
         this .shakeTool = new ShakeTool();
         this .shakeTool.addEventListener(egret.Event.CHANGE, this .onChange, this );
         this .shakeTool.start();
}
 
private onChange(e:egret.Event){
        var data = e.data;
        //用户大概晃动了手机2-3次
        if (data.shakeCount > 6 ){
                 egret.log( "摇一摇完成" );
                  this .shakeTool.stop();
         }
}




ShakeTool源码

[Actionscript3]  纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
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
/**
  * 摇一摇工具类
  * @author chenkai
  * [url=home.php?mod=space&uid=81950]@since[/url] 2017/4/20
  */
class ShakeTool extends egret.EventDispatcher{
         private orientation:egret.DeviceOrientation; //设备方向变化
         private xAngle: number = 0 ;     //设备绕x角度
         private yAngle: number = 0 ;     //设备绕y角度
         private zAngle: number = 0 ;     //设备绕z角度
         private last_x: number = 0 ;     //上一次绕x角度
         private last_y: number = 0 ;     //上一次绕y角度
         private last_z: number = 0 ;     //上一次绕z角度
         private shakeCount: number = 0 ; //摇动次数
         private lastTime:number = 0 ;    //上一次更新时间
         private shakeAngle:number = 45 ; //当晃动角度大于一定角度时,算摇动一次
 
         public constructor(){
                 super ();
         }
 
         /**开始 */
         public start(){
                 //重置数据
                 this .shakeCount = 0 ;
                 this .lastTime = 0 ;
                 this .last_x = 0 ;
                 this .last_y = 0 ;
                 this .last_z = 0 ;
 
                 //开始监听
                 this .orientation || ( this .orientation = new egret.DeviceOrientation());
                 this .orientation.addEventListener(egret.Event.CHANGE, this .onOrientation, this );
                 this .orientation.start();
         }
 
         /**停止 */
         public stop(){
                 if ( this .orientation){
                         this .orientation.removeEventListener(egret.Event.CHANGE, this .onOrientation, this );
                         this .orientation.stop();
                 }
         }
 
         private onOrientation(e: egret.OrientationEvent) {
                 var curTime:number = egret.getTimer();
 
                 //每100ms判断一次
                 if (curTime - this .lastTime > 100 ){
                         this .lastTime = curTime;
 
                         this .xAngle = e.beta;   //x轴
                         this .yAngle = e.gamma;  //y轴
                         this .zAngle = e.alpha ; //z轴
 
                         //旋转超过一定角度,则算摇动一次
                         if (Math.abs( this .last_x - this .xAngle)> this .shakeAngle ||
                                 Math.abs( this .last_y - this .yAngle)> this .shakeAngle ||
                                 Math.abs( this .last_z - this .zAngle)> this .shakeAngle){
                                         this .shakeCount ++;
                         }
                         this .last_x = this .xAngle;
                         this .last_y = this .yAngle;
                         this .last_z = this .zAngle;
                 }
 
                 //派发事件(应该在shakeCount++时派发一次,写在这里只是为了方便显示测试数据...)
                 this .dispatchEventWith(egret.Event.CHANGE, false , {x: this .xAngle,y: this .yAngle,z: this .zAngle,shakeCount: this .shakeCount});
         }
}



Demo下载

转载于:https://www.cnblogs.com/gamedaybyday/p/9219939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
固高Demo是一款用于测试一维比较的软件工具。以下是一个简单的测试步骤: 1. 准备测试样本:选择一组具有明确大小顺序的样本集合。例如,可以使用数字、字母或任何其他可排序的对象。确保样本集合中每个样本都有唯一的标识。 2. 安装和配置固高Demo:按照固高Demo的安装指南进行安装,并确保正确配置软件,使其能够测试一维比较。 3. 输入测试样本:将准备好的样本集合输入到固高Demo中。可以通过手动输入或导入文件的方式将样本集合输入到软件中。 4. 运行测试:运行固高Demo进行一维比较测试。软件会根据特定的算法和比较规则,对输入的样本集合进行排序。 5. 检查结果:检查固高Demo的输出结果,确认样本集合是否按照预期的顺序进行了排序。可以使用软件提供的可视化工具或导出结果文件进行方便的结果检查。 6. 调整和重新测试:如果结果与预期不符,可以对输入样本或配置参数进行调整,然后重新运行测试,直到得到满意的结果。 7. 记录和报告结果:将测试结果进行记录和报告。可以包括排序后的样本集合、排名的得分或其他相关的统计数据。 固高Demo是一个方便实用的工具,可以帮助开发人员和测试人员对一维比较进行有效的测试和验证。通过以上步骤,可以快速了解和评估一维比较算法的准确性和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值