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

Detecting Collisions

Although you haven’t worked with gathering input from the user, you can instill a certain amount of responsiveness in the game by dealing with simple collisions with the walls of the tablet. A quick and simple implementation is shown in Listing 2-16.

Listing 2-16. Collision Code

if (sprite.getX() >= getWidth()){         sprite.setMoveX(-2); } if (sprite.getX() <= 0){         sprite.setMoveX(2); }

Add this excerpt into the update method of the GameView.java file. It’s important to do this before the call to sprite.update() because you must handle any direction changes before you increment the position of the sprite.

You may notice that you reference a function that you haven’t yet created. To make this function work, the ...

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