Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

This section describes how to use the mobile device accelerometer to test for a shake event.  Perhaps you want to allow the user to clear the data on the screen just by shaking the device.

Accelerometer API

PhoneGap and the Profound UI Mobile Client provide the following API to monitor the accelerometer: 

watchId = accelerometer.watchAcceleration( accelerometerSuccess, accelerometerErr, accelerometerOptions )

The accelerometer is a motion sensor that detects the change (delta) in movement relative to the current position of the device. The accelerometer can detect 3D movement along the x, y, and z axis.  The accelerometer.watchAcceleration API gets the current acceleration of the device at regular intervals. Each time the acceleration is retrieved, the accelerometerSuccess callback function is executed. You can specify the interval in milliseconds via the frequency parameter in the acceleratorOptions object.

The returned watch ID references the accelerometer watch process. The watch ID can be used with accelerometer.clearWatch to stop watching the accelerometer.

Detecting Shaking

A shake event can be caught by measuring quick changes in the accelerometer. The sample code below shows how to accomplish this.  The code can be placed in the app.js file.  It looks for an element with the id of "shake" on the Profound UI Rich Display File screen and uses it to trigger the Shake action.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
  startWatch();
}

var watchID;
function startWatch() {
  var previousReading = {
    x: null,
    y: null,
    z: null
  }
  var options = { frequency: 250 };  // Update acceleration every quarter second
  watchID = navigator.accelerometer.watchAcceleration(function onSuccess(acceleration) {
    var changes = {},
    bound = 4;  // this controls the sensitivity for detecting the shake event
    if (previousReading.x !== null) {
      changes.x = Math.abs(previousReading.x, acceleration.x);
      changes.y = Math.abs(previousReading.y, acceleration.y);
    }
    if (changes.x > bound && changes.y > bound) {
      stopWatch();
      // We are relying on a hidden button with an ID of shake to be present on the Profound UI Rich Display File screen
      if (getObj("shake") != null) pui.click("shake");
      setTimeout(startWatch, 3000);
    }
    previousReading = {
      x: acceleration.x,
      y: acceleration.y,
      z: acceleration.z
    }
  }, function onError() {
    alert('Some problem has occurred in reading the accelerometer.');
  }, options);
}

function stopWatch() {
  if (watchID) {
    navigator.accelerometer.clearWatch(watchID);
    watchID = null;
  }
}

To utilize the above in a Profound UI RPG application, simply place a hidden button on the screen.  Assign the id of "shake" to the button and bind the response property to a named indicator called Shake.  Then, in your RPG code, simply test for the Shake event as follows:

If Shake = *On;
  // RPG logic, such as clearing screen data, would go here
EndIf;
  • No labels