I'm pretty new to Java (C# programmer) and I have here a problem that I dont understand. Sorry for the crappy english. =)
I'm working on a simple Snake game, I have different classes, enums etc.. My problem is, if I call the JFrame with the game from the Menu class, it doesnt do anything. I can debug through the code but it doesnt draw anything. If I start the Game class direct from the Main method, there is no problem.
I think that its a simple error and im just to stupid to see it.
Here the code:
Mouse Event:
else if (mouseX > 75 && mouseX < 175 && mouseY > 260
&& mouseY < 285) {
startGame();
startGame method in the Menu class:
public void startGame(){
this.setVisible(false);
game = new Game();
}
Game Constructor:
public Game() {
super("Snake V 0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(940, 620);
setLocation(200, 200);
addKeyListener(keyListener);
setVisible(true);
//this.createBufferStrategy(2);
zeichnung.addFigur(new Field(0, 0));
snake = new Snake(40, 300, 1);
zeichnung.addFigur(snake);
zeichnung.addFigur(new Diamond());
this.getContentPane().add(jpanel);
this.repaint();
this.Intervall();
}
Intervall:
public void Intervall() {
while (true) {
try {
snake.direction = keyListener.getDirection();
zeichnung.checkCollision(snake);
snake.move();
this.repaint();
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
As i said before. If I start the Game class direct from Main method (simple with Game game = new Game();) there is no problem.
I hope I dont forgot any important information.