[]如何启用摇一摇
代码如下,拿去即用
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){'); 写的时候会有问题
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
cuixueying
赞同来自:
你好!你以上示例是有遇到什么问题?请您将您的报错信息详细描述下,我们一起看下是哪里的问题导致的。
摇一摇as3原生也有接口,就是重力感应之类的,该接口已经写好并支持,您可以使用as3 原生下的Accelerometer类实现并启动摇一摇的功能。
以下示例可最为参考:
Laya_XS
赞同来自:
/*[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]*/