-1

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) {}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. The problem with `KeyListener` is usually that either a) The component does not have focus or.. b) The component is not focusable, -- For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 14 '14 at 13:12
  • BTW - Is this using AWT or Swing based components? – Andrew Thompson Jan 14 '14 at 14:02

2 Answers2

0

What you are probably forgetting is in your main method is to add this line of code:

component.addKeyListener(keyListener)
Cœur
  • 37,241
  • 25
  • 195
  • 267
CoderMusgrove
  • 604
  • 8
  • 18
0

You have not registered your KeyListener. The KeyListener has to be added.

The java.awt.Component class contains the method addKeyListener(KeyListener l)

You will have to identify which java.awt.Component you wish to intercept the event. Once you have identified that Component, you will call this method using, PacRunner as the KeyListener argument :

component.addKeyListener(myPacRunner);

or, if that method is called from within the PacRunner instance :

component.addKeyListener(this);

In order to implement this pattern in your provided code, assuming that the class Grid extends Component, you would first instantiate your PacRunner, then add that PacRunner instance as a KeyListener to the Grid

public static void main (String[] args)
{
    PacRunner myPacRunner = new PacRunner(); // An Empty constructor inherited from Object
    gr = new Grid();
    gr.addKeyListener(myPacRunner);
    ...
}

For a more complete treatment of the topic. See How to Write a Key Listener in the Java Tutorials.

OYRM
  • 1,395
  • 10
  • 29
  • Ok, I can't add that to the static void main. Where would I add that in the program to make it work correctly? – user2958134 Jan 14 '14 at 14:18
  • Does Grid extend component ? If not, please provide some information regarding your GUI. – OYRM Jan 14 '14 at 20:17
  • I've edited the example to illustrate one possible method for using an Instance of PacRunner as a KeyListener within the static main method – OYRM Jan 15 '14 at 00:45