[]如何启用摇一摇

 
代码如下,拿去即用
package controller
{
import model.Model;

/**
* ...
* @author 偷心枫贼
*/
public class Controller_yao1yao
{

public function Controller_yao1yao(model:Model)
{
__JS__('_model = model');
__JS__('var SHAKE_THRESHOLD = 100');
__JS__('var last_update = 0');
__JS__('var x = y = z = last_x = last_y = last_z = 0;');
__JS__('var canShake = 1');
__JS__('if (window.DeviceMotionEvent){window.addEventListener("devicemotion", deviceMotionHandler, false)');
__JS__('}else{alert("你的设备不支持DeviceMotion事件");}');
__JS__('function deviceMotionHandler(eventData){var acceleration = eventData.accelerationIncludingGravity');
__JS__('var curTime = new Date().getTime()');
__JS__('if ((curTime - last_update) > 100 && canShake == 1){var diffTime = curTime - last_update');
__JS__('last_update = curTime');
__JS__('x = acceleration.x');
__JS__('y = acceleration.y');
__JS__('z = acceleration.z');
__JS__('var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 600');
__JS__('if (speed > SHAKE_THRESHOLD)_model.sendMoney = speed;//将得到的摇晃速度发送到 _model}}');
}
}

}
 
 
最后layabox会转换成如下 
 
//class controller.Controller_yao1yao
var Controller_yao1yao=(function(){
function Controller_yao1yao(model){
/*__JS__ */var _this=this;
/*__JS__ */_model=model;
/*__JS__ */var SHAKE_THRESHOLD=100;
/*__JS__ */var last_update=0;
/*__JS__ */var x=y=z=last_x=last_y=last_z=0;;
/*__JS__ */var canShake=1;
/*__JS__ */if (window.DeviceMotionEvent){window.addEventListener("devicemotion",deviceMotionHandler,false);
/*__JS__ */}else{alert("你的设备不支持DeviceMotion事件");};
/*__JS__ */function deviceMotionHandler(eventData){var acceleration=eventData.accelerationIncludingGravity;
/*__JS__ */var curTime=new Date().getTime();
/*__JS__ */if ((curTime-last_update)> 100 && canShake==1){var diffTime=curTime-last_update;
/*__JS__ */last_update=curTime;
/*__JS__ */x=acceleration.x;
/*__JS__ */y=acceleration.y;
/*__JS__ */z=acceleration.z;
/*__JS__ */var speed=Math.abs(x+y+z-last_x-last_y-last_z)/ diffTime *600;
/*__JS__ */if (speed > SHAKE_THRESHOLD)_model.sendMoney=speed;}};
}

__class(Controller_yao1yao,'controller.Controller_yao1yao',true);
return Controller_yao1yao;
})()
 
 
吐槽一下 __JS__ 后面建议不要自动补全 ';',否则当我这样 
__JS__('if (window.DeviceMotionEvent){'); 写的时候会有问题
已邀请:

cuixueying

赞同来自:

@govenlive 
  你好!你以上示例是有遇到什么问题?请您将您的报错信息详细描述下,我们一起看下是哪里的问题导致的。
  摇一摇as3原生也有接口,就是重力感应之类的,该接口已经写好并支持,您可以使用as3 原生下的Accelerometer类实现并启动摇一摇的功能。
 
以下示例可最为参考:
package
{
import flash.display.Sprite;
import flash.events.AccelerometerEvent;
import flash.sensors.Accelerometer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

public class Shake_A_Shake extends Sprite
{
private var acc1:Accelerometer;
private var oldAccX:Number;
private var oldAccY:Number;
private var oldAccZ:Number;
private var sum:Number;
private var txt:TextField;

public function Shake_A_Shake()
{
this.addChild(txt=new TextField);
txt.x=10;
txt.y=10;
txt.autoSize=TextFieldAutoSize.LEFT;

if(Accelerometer.isSupported)
{
oldAccX=0;
oldAccY=0;
oldAccZ=0;
sum=0;

acc1=new Accelerometer();
acc1.addEventListener(AccelerometerEvent.UPDATE,updata);
}
else
{
txt.text="当前设备不支持重力感应"
}
}

protected function updata(event:AccelerometerEvent):void
{
// TODO Auto-generated method stub
const k:Number=5;
var accX:Number=Math.round(event.accelerationX*k)/k;
var accY:Number=Math.round(event.accelerationY*k)/k;
var accZ:Number=Math.round(event.accelerationZ*k)/k;

sum+=Math.abs(accX-oldAccX);
sum+=Math.abs(accY-oldAccY);
sum+=Math.abs(accZ-oldAccZ);

oldAccX=accX;
oldAccY=accY;
oldAccZ=accZ;

txt.text=
"event.accelerationX"+event.accelerationX+"\n"+
"event.accelerationY"+event.accelerationY+"\n"+
"event.accelerationZ"+event.accelerationZ+"\n"+
"accX="+accX+"\n"+
"accY="+accY+"\n"+
"accZ="+accZ+"\n"+
"sum="+sum;
}
}
}

Laya_XS

赞同来自:

__JS__()方法可以配合html5的块编译宏使用,上面的写法看着有点累,可以参考下:
/*[IF-SCRIPT-BEGIN]
_model = model;
var SHAKE_THRESHOLD = 100;
var last_update = 0;
var x = y = z = last_x = last_y = last_z = 0;;
var canShake = 1;
if (__JS__('window.DeviceMotionEvent')){__JS__('window').addEventListener("devicemotion", deviceMotionHandler, false);
}else{alert("你的设备不支持DeviceMotion事件");};
function deviceMotionHandler(eventData){var acceleration = eventData.accelerationIncludingGravity;
var curTime = new Date().getTime();
if ((curTime - last_update) > 100 && canShake == 1){var diffTime = curTime - last_update;
last_update = curTime;
x = acceleration.x;
y = acceleration.y;
z = acceleration.z;
var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 600;
if (speed > SHAKE_THRESHOLD)_model.sendMoney = speed;//将得到的摇晃速度发送到 _model}};
[IF-SCRIPT-END]*/

要回复问题请先

商务合作
商务合作