Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'll get to the point: I have a player in my game that you control with the keyboard yet the key methods in the player class and ActionListener w/ KeyAdapter in the Board class don't seem to fire.

So far I've tried adding these key methods into the JFrame, doesn't seem to let me move him even though other objects that I have (enemies) can move fine.

Here's part of the JFrame class with the event listeners:

    frm.addKeyListener(KeyBoardListener);

    public void mouseClicked(MouseEvent e) 
{
        nSound.play();
        StartB.setContentAreaFilled(false);
        cards.remove(StartB);
        frm.remove(TitleL);
        frm.remove(cards);
        frm.setLayout(new GridLayout(1, 1));
        frm.add(nBoard); //Add Game "Tiles" Or Content. x = 1200
        nBoard.setPreferredSize(new Dimension(1200, 420));
        cards.revalidate();
        frm.validate();
}

    public KeyListener KeyBoardListener = new KeyListener()
{
    @Override
    public void keyPressed(KeyEvent args0) 
    {
    int key = args0.getKeyCode();

    if(key == KeyEvent.VK_LEFT)
    {
        nBoard.S.vx = -4;

    }
    if(key == KeyEvent.VK_RIGHT)
    {
        nBoard.S.vx = 4;
    }
    if(key == KeyEvent.VK_UP)
    {
        nBoard.S.vy = -4;
    }
    if(key == KeyEvent.VK_DOWN)
    {
        nBoard.S.vy = 4;    
    }
    if(key == KeyEvent.VK_SPACE)
    {
        nBoard.S.fire();
    }

    }   

    @Override
    public void keyReleased(KeyEvent args0) 
    {
    int key = args0.getKeyCode();

    if(key == KeyEvent.VK_LEFT)
    {
        nBoard.S.vx = 0;
    }
    if(key == KeyEvent.VK_RIGHT)
    {   
        nBoard.S.vx = 0;
    }
    if(key == KeyEvent.VK_UP)
    {
        nBoard.S.vy = 0;
    }
    if(key == KeyEvent.VK_DOWN)
    {
        nBoard.S.vy = 0;
    }
    }

    @Override
    public void keyTyped(KeyEvent args0) {
        // TODO Auto-generated method stub

    }
    };
share|improve this question
    
Using the debugger would probably help you alot. Are you familiar with how to use it? –  Byte56 Jun 11 '12 at 19:48
    
please name variables with the first letter in lowercase, as Java standards –  integeruser Jun 11 '12 at 20:43
    
Could you perhaps point me in the right direction to a debugging tutorial? –  A13X Jun 12 '12 at 1:34

1 Answer 1

up vote 0 down vote accepted

It sounds like your JFrame doesn't have keyboard focus. Try this:

frm.requestFocus()

This question on StackOverflow may be useful as well.

share|improve this answer
    
I can't tell you how happy I am! I requested focus on the specific Board component (like the Stacks Exchange question suggested) and it worked! –  A13X Jun 12 '12 at 1:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.