1+ package laya.tbgame.mini
2+ {
3+ import laya.events.EventDispatcher ;
4+
5+ /**@private **/
6+ public class MiniAccelerator extends EventDispatcher
7+ {
8+
9+ public function MiniAccelerator ()
10+ {
11+
12+ }
13+ /**@private **/
14+ public static function __init__ ():void
15+ {
16+ try
17+ {
18+ var Acc:*;
19+ Acc = __JS__ ("laya.device.motion.Accelerator" );
20+ if (! Acc) return ;
21+ Acc[ "prototype" ][ "on" ] = MiniAccelerator[ "prototype" ][ "on" ];
22+ Acc[ "prototype" ][ "off" ] = MiniAccelerator[ "prototype" ][ "off" ];
23+ }catch (e:* )
24+ {
25+
26+ }
27+ }
28+ /**@private **/
29+ private static var _isListening : Boolean = false ;
30+ /**@private **/
31+ private static var _callBack : Function ;
32+ /**@private **/
33+ public static function startListen (callBack :Function ):void
34+ {
35+ _callBack = callBack;
36+ if (_isListening ) return ;
37+ _isListening = true ;
38+ try
39+ {
40+ TBMiniGameAdapter. window . my. onAccelerometerChange(MiniAccelerator. onAccelerometerChange);
41+ }catch (e:* ){}
42+
43+ }
44+
45+ /**@private **/
46+ public static function stopListen ():void
47+ {
48+ _isListening = false ;
49+ try
50+ {
51+ TBMiniGameAdapter. window . my. stopAccelerometer({});
52+ }catch (e:* ){}
53+
54+ }
55+ /**@private **/
56+ private static function onAccelerometerChange (res :Object ):void
57+ {
58+ var e: Object ;
59+ e = { };
60+ e. acceleration = res;
61+ e. accelerationIncludingGravity = res;
62+ e. rotationRate = { };
63+ if (_callBack != null )
64+ {
65+ _callBack (e);
66+ }
67+ }
68+
69+ /**
70+ * 侦听加速器运动。
71+ * @param observer 回调函数接受4个参数,见类说明。
72+ */
73+ override public function on (type :String , caller :* , listener :Function , args :Array = null ):EventDispatcher
74+ {
75+ super . on(type , caller, listener , args );
76+ startListen(this [ "onDeviceOrientationChange" ] );
77+ return this ;
78+ }
79+
80+ /**
81+ * 取消侦听加速器。
82+ * @param handle 侦听加速器所用处理器。
83+ */
84+ override public function off (type :String , caller :* , listener :Function , onceOnly :Boolean = false ):EventDispatcher
85+ {
86+ if (! hasListener(type ))
87+ stopListen();
88+
89+ return super . off(type , caller, listener , onceOnly);
90+ }
91+ }
92+
93+ }
0 commit comments