Android:How to move Ball on maze using accelerometer?
Am developing game in android i did maze and ba开发者_开发知识库ll is moving on maze using keyboard but am tryng to move ball by using Accelerometer please help me.........
Thank You
SensorManager manager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
}
This will setup the Accelerometer and then...
public void onSensorChanged(SensorEvent event) {
accelX = event.values[0];
accelY = event.values[1];
accelZ = event.values[2];
}
Will listen for the changes you just need to make sure that the call you want to use implements SensorEventListener
.
There is a good tutorial on this site: http://www.anddev.org/accessing_the_accelerometer-t499.html
However I wouldn't use the accelerometer for this (otherwise you have to throw the device to move the ball) ;-) Have a look at this site: http://developer.android.com/reference/android/hardware/SensorListener.html Try the sensor orientation.
Using these imports and the tutorials below you should be able to get a jump on your problem.
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
A tutorial can be found here.
You can also check this old post.
精彩评论