O'Reilly logo

Beginning Android Tablet Games Programming by Jeremy Kerfs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required