0

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 :)

Ashish
  • 1,121
  • 2
  • 15
  • 25
Josh
  • 95
  • 1
  • 9

2 Answers2

0

I suspect the problem is this block:

if(key == KeyEvent.VK_SHIFT){
    missiles.add(new Missile(x, 400, "bottom"));
}

Because VK_SHIFT is used to ask the top paddle to fire, shouldn't the "bottom" be replaced with "top" in the call to the Missile constructor for this block?

Bobulous
  • 12,967
  • 4
  • 37
  • 68
  • You're right, wierd that I didn't notice it. Fixed it and it still doesn't work, must also be another problem. Any ideas? – Josh Dec 29 '13 at 17:13
  • You will need to reveal the code which handles the missile animation to give us a chance of solving that problem. – Bobulous Dec 29 '13 at 17:16
0

In the Missile constructor:

  1. Did you check if the code is being executed? Did you check the value of dy? If the value of dx and dy is zero you won't have any movement even if the code is executed.

  2. Don't use "==" for Object (String) comparisons. Use the equals(...) method. My guess is the code to set the value of dy never gets executed.

In the keyPressed() code did you check to see if the VK_SHIFT key event is being handled? Did you use a debugger to follow the logic flow or add println() statements?

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for trying to help, as I posted above it was just a silly bug I made. Cheers :) – Josh Dec 29 '13 at 17:22