2

Im writing a simple game that basically has two people control sprites and fight. I have written this program with a key listener but this is only allowing me to control one sprite at any given time. I am open to using a different method of keyboard input beside KeyListener although I'd prefer to stick with my KeyListener. I've heard of KeyBindings but I couldn't really figure out how to use them. One idea I had was multithreading but I'm not really comfortable with that concept. Here is my code: (Please leave an example of what my code should be. Also, the 2nd sprite is controlled with a separate class which is almost identical to this one but just uses different keys. )

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;

public class KeyHandler implements KeyListener {

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

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_UP) {
            Display.up = Display.up + 7;
        }

        if (key == KeyEvent.VK_DOWN) {
            Display.down = Display.down + 7;
        }

        if (key == KeyEvent.VK_RIGHT) {
            Display.right = Display.right + 7;
        }

        if (key == KeyEvent.VK_LEFT) {
            Display.left = Display.left + 7;
        }

        if (key == KeyEvent.VK_SPACE) {
            Display.fireMissile();
            Display.whichSpriteInt = 0;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • See [this](http://stackoverflow.com/a/18623318/4857909) and [this](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – Lukas Rotter Aug 20 '15 at 10:34
  • You need key 'polling'. Have a look at this [article](http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-keyboard-and-mouse-r2439). – Sjoerd222888 Aug 20 '15 at 10:37
  • @LuxxMiner ya the problem with KeyBinding is the way my code is formated. Currently its not controlling an object, its just painting a buffered image then changing the coordinates with the key input. ( I have a timer running which repaints ever 10 milseconds) I suppose I could change the format but it would require a lot of code reworking and I'd prefer to avoid it. – Ashwin Gupta Aug 20 '15 at 10:37

1 Answers1

0

Currently its not controlling an object, its just painting a buffered image then changing the coordinates with the key input

So you do an Action when a KeyEvent is generated.

Key Bindings work no differently. You map an Action to the KeyStroke. It doesn't change the way you write your code, only the way you handle the event.

Check out Motion Using the Keyboard. The first two examples (one using a KeyListener and the other using Key Binding) demonstrate this concept. Both classes define a move(...) method which is identical, the only difference is how the method is invoked.

I could change the format but it would require a lot of code reworking and I'd prefer to avoid it.

There is no time like the present to learn how to better structure/design your program. Swing was designed around the concept of creating Actions so the Action can be invoked by the Keyboard or the mouse.

Having said that you really want to look at the KeyboardAnimation.java example. This class does exactly what you want. It uses a Timer for animation and allows different KeyStrokes to move different objects.

camickr
  • 321,443
  • 19
  • 166
  • 288