I'd like to create an app for iphone using phonegap + impact using accelerometer, someone did something similar in the past?
1 decade ago
by dominic
PhoneGap has some facilities to read data from the Accelerometer (
documentation), but I believe that's just for legacy purposes.
Newer iOS versions (>= 4.2) can directly access the accelerometer via JavaScript. Here's an
example and some hard to read
official documentation.
Because this event is only fired when motion is detected, you should save the acceleration data somewhere and then use it your game loop. E.g.:
// Do this once before you call ig.main()
window.ondevicemotion = function(event) {
// when ig.input is not ready, just ignore the event
if( !ig.input ) { return; }
ig.input.accel = {
x: event.accelerationIncludingGravity.x,
y: event.accelerationIncludingGravity.y,
z: event.accelerationIncludingGravity.z
};
}
// You should then be able to access the current acceleration data
// from anywhere in your code using ig.input.accel.x,
// ig.input.accel.y and ig.input.accel.z
I tried this but the last part of your post "you should then be able to access the current acceleration data from anywhere in your code" doesn't work if I put this in the update function of player.js:
console.log(ig.input.accel.x);
console.log(ig.input.accel.y);
console.log(ig.input.accel.z);
I get:
'ig.input.accel' [undefined] is not an object.
1 decade ago
by dominic
Did you check if the function passed to window.ondevicemotion
gets called at all?
Also, make sure you don&039;t access #ig.input.accel
before it ondevicemotion
hasn&039;t at least been called once - or set #ig.input.accel
to some default value to make sure it's defined.
I still have not been able to make this work. I have other tutorials where it works but I don't know where to put this code in Impact.
I am used to ActionScript and Objective C but I don't understand this formulation:
window.ondevicemotion = function(event) {
}
is window.ondevicemotion an event like a mouse movement or a click?
Why does that equal a function? I've seen that a lot in JavaScript but don't understand when something equals the actual function keyword right in the same line. Is it like creating a var inside the bigger function that will disappear as far as memory is concerned?
Sorry for these basic questions but no matter what I do I can't make this simple thing work (but I can in other tutorials that move a ball around). Even doing console.log (event.accelerationIncludingGravity.x); inside the function throws an "event" undefined error.
I'm bumping this post. Does anyone have an example of the accelerometer working with iOS Impact? I can't get the code to work. If I load some example of the accelerometer in mobile Safari, it works. But I can't make it work in iOS Impact, when the javascrip is compild as an app.
1 decade ago
by dominic
Quote from stahlmanDesign
But I can't make it work in iOS Impact, when the javascrip is compild as an app.
iOSImpact doesn't support the accelerometer at the moment. The example above only works in Mobile Safari (and the Android Browser!?).
I'll add support for the accelerometer in iOSImpact soon!
Well that explains it. At least now I know I'm not going crazy! Thanks for all your efforts. I love Impact.
I'll add support for the accelerometer in iOSImpact soon!
@dominic Is this still on the list of things to do? I want to control my player by moving the iPad around instead of using on-screen buttons. Alternatively, do any of the other options like AppMobi support accelerometer in JavaScript, to your knowledge?
Page 1 of 1
« first
« previous
next ›
last »