Witam, piszę prostą grę w Javie i mam mały problem z obsługą klawiszy. Poniżej dodałem kody. Ma ktoś jakiś pomysł dlaczego nie działają przyciski RIGHT i LEFT ?
Klasa gry Game().
public Game(Frame container)
{
container.addKeyListener(new KeyAdapter()
{
public void KeyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_RIGHT) player.moveOnXAxis(10);
if(e.getKeyCode() == KeyEvent.VK_LEFT) player.moveOnXAxis(-10);
repaint();
}
});
Klasa z klasy Player()
public void moveOnXAxis(int speed)
{
Box.x += speed;
if(Box.x < 0) Box.x = 0;
if(Box.x > instence.getGameDimesion().width ) Box.x = instence.getGameDimesion().width;
}
i cała klasa Main()
import java.awt.Dimension;
import javax.swing.*;
public class Main {
public static JFrame frame;
public static Game game;
public static void main(String[] args)
{
frame = new JFrame("ARK");
frame.setSize(750,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game = new Game(frame);
frame.add(game);
Dimension tmp = frame.getSize();
frame.pack();
frame.setSize(tmp);
}
}