Game Development Reference
In-Depth Information
We are now almost finished. All that is left to do is implement the listener methods
that are part of the
SensorEventListener
interface that we have derived the
Gyroscope
class from. Add the following code after the
GyroscopeGetZ
method.
public void onAccuracyChanged(Sensor aSensor, int aAccuracy)
{
}
public void onSensorChanged(SensorEvent aEvent)
{
if (aEvent.accuracy != SensorManager.SENSOR_STATUS_UNRELIABLE)
{
x = aEvent.values[0];
y = aEvent.values[1];
z = aEvent.values[2];
if (mUsesDegrees)
{
x = (x * 3.14159267f) / 180.0f;
y = (y * 3.14159267f) / 180.0f;
z = (z * 3.14159267f) / 180.0f;
}
}
}
The
onAccuracyChanged
method is left empty since it must be implemented to
satisfy the interface. The
onSensorChanged
method is important, though, as this
will receive the new gyroscope input values. We first check to see if the passed in
SensorEvent
contains reliable data (the device itself will determine what constitutes
reliable data); then, we just pull out the new gyroscope values and store them in our
member variables.
If we determined that the device is returning values in degrees per second, we
do a quick conversion to radians to ensure that our extension always returns
consistent values.
Building an Android extension
Our Android extension code is now ready to be built and this is even simpler than
it was with the Windows version. All we have to do is open Windows Explorer and
navigate to the
Gyroscope
directory, and double-click first the
Gyroscope_android_
java.mkb
file and then the
Gyroscope_android.mkb
file. The first MKB file will
build the Java code, while the second will build the C++ code that will be called from
our project code and that will in turn call the Java code.
Search WWH ::
Custom Search