I have a Keylistener
class for a game I am making an something has bpne wrong with it. I can't seem to get the pacman move with the key clicks. It seems as if the Keylistener
method isn't ever being called. I'm relatively new to Java and GUI interfaces so forgive me if my coding is a bit rusty.
import java.awt.event.*;
public class PacRunner implements ActionListener, KeyListener
{
private static Grid gr;
public static void main (String[] args)
{
gr = new Grid();
gr.addGhost(new Location(4,11));
gr.movePac(new Location(6,11));
gr.show();
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP) gr.move(Location.UP);
else if(e.getKeyCode() == KeyEvent.VK_DOWN) gr.move(Location.DOWN);
else if(e.getKeyCode() == KeyEvent.VK_LEFT) gr.move(Location.LEFT);
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) gr.move(Location.RIGHT);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
@Override
public void actionPerformed(ActionEvent e) {}
}