Using Sensor Data

To incorporate sensor data into the logic of a game and its updates, you need to pass the data into the game’s View class. First add this to the MainActivity class:

GameView mGameView;

You must also add the code shown in Listing 3-19 to the onCreate() method:

Listing 3-19. GameView Instance

mGameView = new GameView(this); setContentView(mGameView);

You now have a GameView instance from which you can call various methods. Next, in GameView, you need to add a new function to which you pass your orientation data to. Listing 3-20 shows the call to add within onSensorChanged().

Listing 3-20. Sending Sensor Data

@Override public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ ...

Get Beginning Android Tablet Games Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.