In the SnakeGame class, I have declared a variable,
private Snake snake;
In the createGame method i instantiate a Snake object before calling drawScene.
private void createGame() {
Snake snake = new Snake(WIDTH / 2, HEIGHT / 2);
System.out.println("Snake constructed: " + snake);
drawScene();
}
The console confirms that there is a Snake object snake:
Snake constructed: com.codegym.games.snake.Snake@2f612909
But if I try to print out the snake object in the drawScene method, it does not seem to be available.
private void drawScene() {
for (int x = 0; x < WIDTH; ++x) {
for (int y = 0; y < HEIGHT; ++y) {
setCellColor(y, x, BACKGROUNDCOLOR);
}
}
System.out.println("Snake? : " + snake);
snake.draw(this);
}
In the console I see:
Snake? : null
I am a bit confused as to why snake is null from the drawScene method but available from the
createGame method.
I am probably missing something obvious here.