Here is a script I wrote to handle shaking the phone from side-to-side:
I've found a threshold of around 1.5 to be sufficient.
function watchForShake(threshold)
{
var axl = new Accelerometer();
axl.watchAcceleration
(
function (Accel)
{
if (true === Accel.is_updating)
{
return;
}
if (Accel.x >= threshold || Accel.x <= (0 - threshold))
{
// The user has shaken their device. Do something
// ...
}
}
, function(){}
, {frequency : 750}
);
}
Comments (6)
Jake Sankey said
at 6:01 pm on Apr 8, 2009
HI there! I am thrilled about all this stuff. I have created a few games in JS that I have ported to working iPhone apps using PhoneGap. One of these games, I would like the user to be able to shake the device to be able to start a new game. Currently there is just a button to do this. The button is an OnClick javascript:Init(true) ... I have been trying to do this using the Shake Handling example and I am failing. Could anyone help me out?
Gennady Potapov said
at 9:58 am on Apr 20, 2009
Jake Sankey,it's strange, because I'm using this code and it works perfect...
Jake Sankey said
at 2:29 pm on May 17, 2009
Could you tell me where exactly in this code, I am to put the 1.5 threshold value? do I actually replace the word 'threshold' where I see it? Also drop.flow.restart(); is the command that I currently use in my game as an OnClick, and I want it to be executed on a shake of the iphone. Can I please get bit of help on this?
blank said
at 1:36 pm on Jun 25, 2009
Hi everyone! I am new here and I have been searching for days and can't seem to find anything that works and I am close to losing my mind. How can I make my app refresh using this code? I have altered the code like below, using the javascript for "reload", but it isn't working for me when I shake. Does anyone have any tips? Thank you for your help!
function watchAccel() {
debug.log("watchAccel");
var suc = function(a){
document.getElementById('x').innerHTML = roundNumber(a.x);
document.getElementById('y').innerHTML = roundNumber(a.y);
document.getElementById('z').innerHTML = roundNumber(a.z);
};
var fail = function(){};
var opt = {};
opt.frequency = 100;
timer = navigator.accelerometer.watchAcceleration(suc,fail,opt);
}
function roundNumber(num) {
var dec = 3;
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
function preventBehavior(e) {
e.preventDefault();
};
PhoneGap.addConstructor(function(){
document.addEventListener("touchmove", preventBehavior, false);
deviceInfo();
document.addEventListener('orientationChanged', function(e) { debug.log("Orientation changed to " + e.orientation); }, false);
});
function watchForShake(threshold) {
var axl = new Accelerometer();
axl.watchAcceleration
(
function (Accel)
{
if (true === Accel.is_updating)
{
return;
}
if (Accel.x >= threshold || Accel.x <= (0 - threshold))
{
// The user has shaken their device. Do something
// ...
}
}
, location.reload(true){}
, {frequency : 750}
);
}
Joel said
at 1:35 pm on Sep 4, 2009
This seems to just track one strong movement of the phone in the x-axis, not a back-and-forth movement. I am new to Phonegap so maybe I'm missing something. What does Accel.is_updating do?
Jack Klink said
at 1:23 pm on Sep 22, 2009
Does anyone know how to stop watching for the shake?
I have a button that starts waiting for shakes. But I would like a button that stops watching for a shake.
You don't have permission to comment on this page.