0

I want to create a game where you control your character with the arrow keys. When I tried it you it first moved the character into the direction, stopped for a moment and only then began to continously move the character until I stopped. Now I want to know if I can somehow bypass that moment where no actions take place.

Also, I have the problem where I try to use multiple keys. It works when I press both keys at the same time, but when I first press one and then the other key, It doesn't work.

I use Java awt and JFrames.

The code:

public class Main extends JPanel implements MouseListener, ActionListener, ComponentListener, Runnable, KeyListener
{
    private JFrame frame;
    private Timer timer = new Timer(20, this);
    private boolean draw = true;

    int pX = 0;
    int pY = 0;

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Main());
    }

    @Override
    public void run()
    {
        frame = new JFrame("Test");
        frame.setMinimumSize(new Dimension(1000, 750));
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(this);
        frame.addKeyListener(this);
        frame.setFocusable(true);
        frame.pack();
        frame.setVisible(true);
        timer.start();
    }

    protected void paintComponent(Graphics g)
    {
        if(draw)
        {
            //Draw the character
        draw = false;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        this.repaint();
        draw = true;
    }


    @Override
    public synchronized void keyTyped(KeyEvent e)
    {
        switch(e.getExtendedKeyCode())
        {
            case KeyboardCodes.DOWN:
                pY += 5;
                break;
            case KeyboardCodes.UP:
                pY -= 5;
                break;
            case KeyboardCodes.LEFT:
                pX -= 5;
                break;
            case KeyboardCodes.RIGHT:
                pX += 5;
        }
    }

    @Override
    public synchronized void keyPressed(KeyEvent e)
    {
        switch(e.getExtendedKeyCode())
        {
            case KeyboardCodes.DOWN:
                pY += 5;
                break;
            case KeyboardCodes.UP:
                pY -= 5;
                break;
            case KeyboardCodes.LEFT:
                pX -= 5;
                break;
            case KeyboardCodes.RIGHT:
                pX += 5;
        }
    }

    @Override
    public void keyReleased(KeyEvent e)
    {

    }
}
Keheck
  • 131
  • 1
  • 10
  • You didn't list what tech you are using, but I am guessing the idea is the same. – SedJ601 Nov 28 '18 at 14:30
  • I don't know actually. But isn't it driven by OS, which has setting time timeout, before it starts to emit repeat "key press" events (sorry for bad terminology)? Maybe you can handle repeats yourself. Meaning, that you track up/down state for each key a repeat as fast as you want. – Martin Mucha Nov 28 '18 at 14:31
  • You need to share more code so we can see what is happening – IbzDawg Nov 28 '18 at 14:32
  • @IbzDawg I now added all the code I have, I hope that helps. – Keheck Nov 28 '18 at 15:11
  • @MartinMucha How do I do that? – Keheck Nov 28 '18 at 15:12
  • @VinceEmigh thanks dude! – Keheck Nov 28 '18 at 15:40
  • @Keheck I'm not familiar sufficiently with gaming coding, but IIUC... If you do things as you describing them, then speed of game will be driven by repeat speed of your keyboard. Meaning, the game would be unplayable on my env ;) What you do instead, is you have some time-passing-by representation, and calculation what happened since last loop iteration. When you have that, you just need to check, that key wasn't lifted since last iteration, thus character has to move forward. But again, rather check some proper game architecture design. – Martin Mucha Nov 28 '18 at 16:10

0 Answers0