Platform game player doesn't respond to keyboard
I started learning java, and tried to make a game, i almost finished it but then i realized that it doesn't work, so i copied the project and started dumping code from my game to find what did i do wrong, but i just cant figure it out. Here is the main class:
Code java:
import javax.swing.JFrame;
public class Main extends JFrame {
// KONSTRUKTOR
public Main() {
super("Plavko glupavko");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setResizable(false);
setVisible(true);
add(new Igra());
}
// MAIN METOD
public static void main(String[] args) {
new Main();
}
}
here is igra class:
Code java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Igra extends JPanel implements ActionListener {
private Timer tajmer;
private Tabla tabla;
private Igrac igrac;
private int igracV;
private int igracDx;
// KONSTRUKTOR
public Igra() {
setSize(800, 600);
setBackground(Color.WHITE);
addKeyListener(new Slusalac());
setFocusable(true);
tabla = new Tabla(1);
igrac = tabla.getIgrac();
igracV = 2;
igracDx = 0;
tajmer = new Timer(50, this);
tajmer.start();
}
// ISCRTAVANJE
public void paint(Graphics g) {
super.paint(g);
// Igrac
g.drawImage(igrac.getSlika(), igrac.getX(), igrac.getY(), null);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
// GLAVNA PETLJA
public void actionPerformed(ActionEvent e) {
pomeriIgracaX();
repaint();
}
// Kretanje igraca
public void pomeriIgracaX() {
int duhx = igrac.getXBezPomeraja() + igracDx;
igrac.setX(duhx);
}
// KLASA ZA TASTATURU
private class Slusalac extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int kod = e.getKeyCode();
// pomeraj desno
if (kod == KeyEvent.VK_RIGHT) {
igracDx = igracV;
}
// pomeraj levo
if (kod == KeyEvent.VK_LEFT) {
igracDx = -igracV;
}
}
public void keyReleased(KeyEvent e) {
int kod = e.getKeyCode();
if (kod == KeyEvent.VK_RIGHT) {
if (igracDx > 0)
igracDx = 0;
}
if (kod == KeyEvent.VK_LEFT) {
if (igracDx < 0)
igracDx = 0;
}
}
}
}
here is tabla class:
Code java:
import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Tabla {
private Image pozadina;
// ove liste ce da sadrze objekte
private Igrac igrac;
public Tabla(int tabla) {
Scanner s;
String imefajla = "table/tabla"+tabla+".tbl";
File fajl = new File(imefajla);
try {
s = new Scanner(fajl);
int brreda = 0;
while (s.hasNextLine()) {
String red = s.nextLine();
for (int i = 0; i < red.length(); i++) {
if (red.charAt(i) == 'i') {
igrac = new Igrac(i, brreda);
}
}
brreda++;
}
s.close();
} catch (FileNotFoundException e) {}
}
// GETERI
public Igrac getIgrac() {
return igrac;
}
}
and here is igrac class:
Code java:
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Igrac {
private Image slika;
private final int SIRINA = 30;
private final int VISINA = 30;
private int x;
private int y;
// pomeraj za kameru
public static int kpx = 0;
public static int kpy = 0;
// KONSTRUKTOR
public Igrac(int x, int y) {
this.x = x * 30;
this.y = y * 30;
String fajl = "slike/igrac.png";
slika = new ImageIcon(fajl).getImage();
}
// GETERI
public Image getSlika() {
return slika;
}
public Rectangle getGranice() {
return new Rectangle(x, y, SIRINA, VISINA);
}
public int getXBezPomeraja() {
return x;
}
public int getYBezPomeraja() {
return y;
}
public int getX() {
return x + kpx;
}
public int getY() {
return y + kpy;
}
// SETERI
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
Re: Platform game player doesn't respond to keyboard
Did you ever have a Key Listener?
Also, I'm not very familiar with those and could only possibly do things that weren't combinations, like Shift+F or something.
Re: Platform game player doesn't respond to keyboard
Yes, as you can see in Igra class i added key listener and made a private class (Slusalac) to serve as one, when i create Igrac object directly in my Igra class it all works, but when i create Igrac object within the Tabla class and then pass it to Igra class it doesn't work. I tried debugging but i get no errors.
Re: Platform game player doesn't respond to keyboard
jesamjasem,
You might want to consider which window has focus for the key listener, I'd try making the JPanel request focus :)
whilst you have set it to focusable you may need to request it
also do actionPerformed ever get called, which is your position update function
Chris
Re: Platform game player doesn't respond to keyboard
Yes that's it, requestFocus() did it. Thank you i would never figure this out by myself.