Hi, I'm creating a version of Pong in java, but I have a problem. When I hold down the buttons to move the left and right paddles at the same time, only the one that I hit most recently is registered. I was wondering how I could get them to work simultaneously. Thanks for the help.
private class ReboundListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x += moveX; // moves ball
y += moveY;
Rectangle rectL = new Rectangle(LRecX, LRecY, REC_WIDTH, REC_HEIGHT);
Rectangle rectR = new Rectangle(RRecX, RRecY, REC_WIDTH, REC_HEIGHT);
Rectangle imageRect = new Rectangle(x, y, IMAGE_SIZE, IMAGE_SIZE);
if(rectL.intersects(imageRect)) // reverses direction of ball if it hits left paddle
{
if (moveX < 0)
moveX -= .3;
else
moveX += .3;
if (moveY < 0)
moveY -= .3;
else
moveY += .3;
moveX = moveX * -1;
}
if(rectR.intersects(imageRect)) // reverses direction of ball if it hits right paddle
{
if (moveX < 0)
moveX -= .3;
else
moveX += .3;
if (moveY < 0)
moveY -= .3;
else
moveY += .3;
moveX = moveX * -1;
}
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE) // bounces of top and bottom of screen
{
moveY = moveY * -1;
}
if (x < 0) // starts over if right scores
{
scoreR += 1;
x = 50;
y = 50;
moveX = 4;
moveY = 4;
}
if (x > WIDTH-IMAGE_SIZE) // starts over if left scores
{
scoreL += 1;
x = 50;
y = 50;
moveX = 4;
moveY = 4;
}
repaint();
}
}
private class DirectionListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode()) // moves paddles
{
case KeyEvent.VK_UP:
RRecY -= MOVE; // up and down for right paddle
break;
case KeyEvent.VK_DOWN:
RRecY += MOVE;
break;
case KeyEvent.VK_A:
LRecY -= MOVE; // a and z for left paddle
break;
case KeyEvent.VK_Z:
LRecY += MOVE;
break;
}
repaint();
}
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
01-19-2007, 11:24 PM
Red_Jester
Make a boolean for the movekeys that is set to true for keyPressed and set to false for keyReleased