jack
jack

Reputation: 39

My Keylistener dosn't work and I want to know why

When I run the program everything shows up but when i try the movement keys nothing happens if anyone knows why this is or how to fix it, it will be much appreciated.

My Main Game Window Class

public class GameWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    public int WIDTH = 160, HEIGHT = WIDTH / 12 * 9, SCALE = 3;
    public boolean running = false;
    BackGround bg = new BackGround();
    Ranger R = new Ranger();
    TimerClass T = new TimerClass();
    public static void main(String[] args) {
        new GameWindow();
    }
    public GameWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH * SCALE, HEIGHT * SCALE);
        setResizable(false);
        running = true;
        add(bg);
        bg.add(R);
        bg.setFocusable(true);
        R.setFocusable(true);
        setFocusable(true);
        setVisible(true);
        bg.repaint();
        run();
    }
    public void run() {
        while (running) {
            render();
        }
    }
    public void render() {
        bg.setLocation(Ranger.bgX, Ranger.bgY);
        R.setLocation(Ranger.X, Ranger.Y);
        R.setIcon(Ranger.rangerA[Ranger.I]);
        R.repaint();
        bg.repaint();
    }
}

My Ranger (the sprite I want to move) Class

public class Ranger extends JLabel implements KeyListener {
    private static final long serialVersionUID = 1L;
    public static int X, Y, dX, dY, bgX, bgY, I = 0, jumpTime = 100;
    public static boolean moving = false, movingLeft = false, movingRight = false, onFloor = false, jumping = false, movingUp = false, movingDown = false;
    public int totalImages = 6;
    public BufferedImage ranger1, ranger2, ranger3, ranger4, ranger5, ranger6;
    public static ImageIcon[] rangerA;
    static TileMap TileMap = new TileMap();
    public Ranger() {
        try {
            ranger1 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger0.png"));
            ranger2 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger1.png"));
            ranger3 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger2.png"));
            ranger4 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger3.png"));
            ranger5 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger4.png"));
            ranger6 = ImageIO.read(getClass().getResource("/Images/Sprites/ranger/Ranger5.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        array();
    }
    public void array() {
        rangerA = new ImageIcon[6]; {
            rangerA[0] = new ImageIcon(ranger1);
            rangerA[1] = new ImageIcon(ranger2);
            rangerA[2] = new ImageIcon(ranger3);
            rangerA[3] = new ImageIcon(ranger4);
            rangerA[4] = new ImageIcon(ranger5);
            rangerA[5] = new ImageIcon(ranger6);
        }
    }
    public static void move() {
        X += dX;
        Y += dY;
        System.out.println("X =" + X + " Y= " + Y + " dX = " + dX + " dY= " + dY + " jumpTime= " + jumpTime + " ");
        dX = 0;
        dY = 0;
        if (movingRight || movingLeft) {
            moving = true;
        }
    }@Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            movingRight = true;
        }
        if (keyCode == KeyEvent.VK_LEFT) {
            movingLeft = true;
        }
        if (keyCode == KeyEvent.VK_UP) {
            jumping = true;
            movingUp = true;
        }
        if (keyCode == KeyEvent.VK_DOWN) {
            movingDown = true;
        }
        if (keyCode != KeyEvent.VK_RIGHT) {
            movingRight = false;
        }
        if (keyCode != KeyEvent.VK_LEFT) {
            movingLeft = false;
        }
        if (keyCode != KeyEvent.VK_UP) {
            jumping = false;
            movingUp = false;
        }
        if (keyCode != KeyEvent.VK_DOWN) {
            movingDown = false;
        }
        if (keyCode == KeyEvent.VK_RIGHT) dX += 1;
        if (movingRight) bgX -= 1;
        if (keyCode == KeyEvent.VK_LEFT) dX += -1;
        if (movingLeft) bgX += 1;
        if (keyCode == KeyEvent.VK_DOWN) dY += 1;
        if (movingUp) bgY += 1;
        if (keyCode == KeyEvent.VK_UP) dY += -1;
        if (movingDown) bgY += -1;
    }@Override
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent arg0) {}
}

I will provide all the classes if they are requred and thanks for any help even if it is on programing conventions.

Upvotes: 1

Views: 65

Answers (1)

Cameron K
Cameron K

Reputation: 428

Not sure if this will work, but try adding addKeyListener(this); to GameWindow() and have it implement KeyListener. Then move all the KeyListeners from Ranger to the GameWindow class.

Also, make sure the KeyCode is correct. You can do this by adding System.out.println(e.getKeyCode); System.out.println(KeyEvent.VK_Whatever); under KeyPressed, and pressing the key, and make sure they match.

Upvotes: 1

Related Questions