November 2011
Beginner
196 pages
5h 3m
English
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 ...
Read now
Unlock full access