javascript - Move a object (this case a ball) with acclerometer in JS and Cordova -


i'm trying move ball accelerometer. i'm stuck getting ball moved.

i values of accelerometer, how combine them ball can move?

waiting accelerometer...

<div id="heading">waiting heading...</div>  <div id="ball"></div>  <script src="cordova.js"></script> <script src="static/js/app.js"></script> 

    #ball {     display: block;     border-radius: 50%;     width: 50px;     height: 50px;     background-color: rgb(0,0,0);     color: rgb(0,0,0); }   //self-invoking anonymous function 

(function () { 'use strict';

// watch id references current `watchacceleration` & 'watchheading' var watchid = null;  // initialize app controller object literal var app = {     // init method, cordova ready used     init: function () {         accelerometer.begin();         compas.begin();     },   }  var accelerometer = {     begin: function () {         // update acceleration every 100 of second         var options = {              frequency: 100          };          watchid = navigator.accelerometer.watchacceleration(this.success, debug.fail, options);     },     // stop watching acceleration     stop: function () {         if (watchid) {             navigator.accelerometer.clearwatch(watchid);             watchid = null;         }     },     // onsuccess: snapshot of current acceleration     success: function (acceleration) {         var element = document.getelementbyid('accelerometer');         element.innerhtml = 'acceleration x: ' + acceleration.x + '<br />' +                             'acceleration y: ' + acceleration.y + '<br />' +                             'acceleration z: ' + acceleration.z + '<br />' +                             'timestamp: '      + acceleration.timestamp + '<br />';     } }  var ball = {     object: function () {         var element = document.getelementbyid('ball');     },      update: function () {         game.clear();         newdx = accelerometer.begin.acceleration.x();         newdy = accelerometer.begin.acceleration.y();         newdx *= -5;         newdy *= -5;         object.setdx(newdx);         object.setdy(newdy);          ball.update();     } }  var compas = {     begin: function () {         // update acceleration every 100 of second         var options = {              frequency: 100          };          watchid = navigator.compass.watchheading(this.succes, debug.errormsg, options);     },     // stop watching heading     stop: function () {         if (watchid) {             navigator.compass.clearwatch(watchid);             watchid = null;         }         },     // onsuccess: snapshot of current heading     success: function (heading) {         var element = document.getelementbyid('heading');         element.innerhtml = 'heading: ' + heading.magneticheading;     } }  var debug = {     fail: function () {         alert('onerror!');     },     errormsg: function (compasserror) {          alert('compass error: ' + compasserror.code);     }  }  document.addeventlistener("deviceready", app.init, false); 

})();

try out this tutorial. it's pretty simple.

(note: change phonegap.js cordova.js)


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -