I'm working on a small game in my free time as a proof-of-concept to myself that I'm capable of learning Java, but I've been stumped on an issue for the last week or two involving the instance of KeyListener I created for my project. Basically, the game has a player moving a small block around the screen with the arrow keys while avoiding other moving blocks on the screen. It all seems to be working rather well, except for the fact that only the 'Up' and 'Left' arrow keys respond when pressed - 'Down' and 'Right' don't do anything.
The KeyListener is an anonymous adapter class:
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
switch (evt.getKeyCode())
{
case KeyEvent.VK_DOWN: pc.y += 10;
case KeyEvent.VK_UP: pc.y -= 10;
case KeyEvent.VK_RIGHT: pc.x += 10;
case KeyEvent.VK_LEFT: pc.x -= 10;
}
}
});
'pc' is an object of the PCBlock class which defines the player-controlled block:
private class PCBlock
{
int x, y;
PCBlock()
{
this.x = width/2;
this.y = height/2;
}
void update()
{
if ((x-15) < 0)
x = 15;
else if ((x+15) > width)
x = width - 15;
else if ((y-15) < 0)
y = 15;
else if ((y+15) > height)
y = height - 15;
}
void draw(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(x-15,y-15,30,30);
}
}
My assumption is that the problem I'm having lies in either of these two classes. Can anyone spot what I'm doing wrong? If more of the code is somehow necessary, I'll post it.