E: me@sabisin.com | T: +49.151.68651209

Device Shake – Accelerometer

Heres a small snippet to detect shakes on mobile devices with accelerometer:

var lastShake:Number = 0;
var shakeWait:Number = 600;
 
var acc:Accelerometer = new Accelerometer();
acc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate);
 
function onAccUpdate(evt:AccelerometerEvent):void
{
	if(getTimer() - lastShake > shakeWait && (evt.accelerationX >= 1.5 || evt.accelerationY >= 1.5 || evt.accelerationZ >= 1.5))
	{
		sHakeiT();
		lastShake = getTimer();
	}
}
 
function shakeIt()
{
	trace("shakeIt();");
}

  1. Hi,This is great but have you experienced reserve gravity if you have the iPad rotated a certain way on load?The address bar is at the top but the gravity is reserved. Is there a way of detecting this / reversing the calculations?CheersPete

Leave a Reply