Updating a display object position through accelerometer events
Developers can make use of the accelerometer sensor and continual updates provided by it for many purposes, including motion-detection games and updating the position of an object on the screen.
How to do it…
We will use the device's accelerometer sensor on continual update to move an element around the screen as a response to device movement:
- First, create a new PhoneGap project named
accelobject
by running the following command:phonegap create accelobject com.myapp.accelobject accelobject
- Add the device platform. You can choose to use Android, iOS, or both:
cordova platform add ios cordova platform add android
- Add the device motion plugin by running the following command:
phonegap plugin add org.apache.cordova.device-motion
- Open
www/index.html
and clean up unnecessary elements. Within thebody
tag create twodiv
elements. Set the first with theid
attribute equal todot
. This will be the element that will move around...