Device Shake – Accelerometer


/ Published in: ActionScript 3
Save to your folder(s)

An easy way to detect shakes on mobile devices with equipped accelerometer.


Copy this code and paste it in your HTML
  1. var lastShake:Number = 0;
  2. var shakeWait:Number = 500;
  3.  
  4. var acc:Accelerometer = new Accelerometer();
  5. acc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate);
  6.  
  7. function onAccUpdate(e:AccelerometerEvent):void
  8. {
  9. if(getTimer() - lastShake > shakeWait && (e.accelerationX >= 1.5 || e.accelerationY >= 1.5 || e.accelerationZ >= 1.5))
  10. {
  11. shakeIt();
  12. lastShake = getTimer();
  13. }
  14. }
  15.  
  16. function shakeIt()
  17. {
  18. trace("device has been shaked");
  19. }

URL: http://blog.vamapaull.com/?p=671

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.