Training and Consulting | ||
| Home |
| Previous Page - The Player | Current Page - 17 - Controlling the player | Next Page - Shots |
| Return to the index of free Java tutorials |
Controlling the playerOur previous version of the game showed the player's spaceship but did not allow to control it. In this version we will add this possibility. The ship will be controlled using the cursor keys. Pressing (and holding) the "left" key for example will make the ship move leftwards, in adition to any vertical movement that it might already have. This means that if the user presses simultaneously up and left, the ship will move both upwards and leftwards. Although we could handle all this in the Invaders, it is more appropriate and more flexible to delegate the event handling to the Player class. The approach we will be following will thus be:
We'll define two new methods in Player called keyPressed(...) and keyReleased(...). These methods will mirror the standard keyboard event methods. So now we have our main class as follows:
1 package version17; 2 /** 3 * Curso B?sico de desarrollo de Juegos en Java - Invaders 4 * 5 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n 6 * 7 * http://www.planetalia.com 8 * 9 */ 10 11 12 import java.awt.Canvas; 13 import java.awt.Color; 14 import java.awt.Dimension; 15 import java.awt.Graphics2D; 16 import java.awt.event.KeyEvent; 17 import java.awt.event.KeyListener; 18 import java.awt.event.WindowAdapter; 19 import java.awt.event.WindowEvent; 20 import java.awt.image.BufferStrategy; 21 import java.util.ArrayList; 22 23 import javax.swing.JFrame; 24 import javax.swing.JPanel; 25 And the player class becomes:
1 /** 2 * Curso B?sico de desarrollo de Juegos en Java - Invaders 3 * 4 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n 5 * 6 * http://www.planetalia.com 7 * 8 */ 9 package version17; 10 11 import java.awt.event.KeyEvent; 12 13 public class Player extends Actor { 14 protected static final int PLAYER_SPEED = 4; 15 protected int vx; 16 protected int vy; 17 private boolean up,down,left,right; 18 19 20 public Player(Stage stage) { 21 super(stage); 22 setSpriteNames( new String[] {"nave.gif"}); 23 } 24 25 public void act() { 26 super.act(); 27 x+=vx; 28 y+=vy; 29 if (x < 0 || x > Stage.WIDTH) 30 vx = -vx; 31 if (y < 0 || y > Stage.HEIGHT) 32 vy = -vy; 33 } 34 35 public int getVx() { return vx; } 36 public void setVx(int i) {vx = i; } 37 public int getVy() { return vy; } 38 public void setVy(int i) {vy = i; } 39 40 We've defined also a constant called PLAYER_SPEED, so that the cursor keys only change the direction of the player, but not the speed. |
||||||||||||||
Do you want to be notified when new tutorials or lessons are published? Press here
| Actor.java | Invaders.java | Monster.java | Player.java |
| SpriteCache.java | Stage.java |
| Previous Page - The Player | Current Page - 17 - Controlling the player | Next Page - Shots |
| Return to the index of free Java tutorials |
(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted