As most of my previous questions have noted, I'm working on a simple 2D game in Java. It's a "Pong" clone.
Currently I have on the screen two blue 'paddles' (one on the top of the screen and one on the bottom), controlled by the left and right arrow keys and the 'a' and 'd' keys to move on the x axis.
There's also the "ping pong" ball flying around, and reacting to collisions with the paddles and the walls.
Now I'm trying to make the paddles shoot a missile, whenever a button is pressed. The bottom paddle shoots a missile when VK_SPACE is pressed, and the top paddle shoots a missile whenever VK_SHIFT is pressed.
I succeded with the bottom paddle. It indeed shoots a missile when the space button is pushed.
But it won't happen with the top paddle. Here's some code:
In the Board
class, which runs most of the game. It implements KeyListener:
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT || key==KeyEvent.VK_RIGHT) bPaddle.keyPressed(e);
if(key==KeyEvent.VK_A || key==KeyEvent.VK_D) tPaddle.keyPressed(e);
if(key == KeyEvent.VK_SPACE) bPaddle.keyPressed(e);
if(key == KeyEvent.VK_SHIFT) tPaddle.keyPressed(e);
}
In the Paddle
class
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
// Some code that happens when the arrow keys or a/d keys are pushed.
// ..........
// ..........
if(key == KeyEvent.VK_SPACE){
missiles.add(new Missile(x, 300, "bottom"));
}
if(key == KeyEvent.VK_SHIFT){
missiles.add(new Missile(x, 400, "bottom"));
}
}
The Constructor of the Missile
class:
public Missile(int x, int y, String type){
this.x = x;
this.y = y;
image = new ImageIcon(this.getClass().getResource("sprites/missile.png")).getImage();
width = image.getWidth(null);
height = image.getHeight(null);
dx = 0;
if(type=="top")dy=3;
if(type=="bottom")dy=-3;
visible = true;
}
What could be the problem? (If I need to post here more of the code, let me know). Thanks :)