How to call non-static method from static?
I tried to call non-static drawScore() method from static calldrawScore(),
but I got the error "cannot find symbol constructor GameScreen" on line
GameScreen draw = new GameScreen(); in calldrawScore(). When I pass with
mouse over that line it says "GameScreen (Game) in GameScreen cannot be
applied to ()".
Function calldrawScore() must be static because I call it from another
java file in which I update score. It would be better to call drawScore()
directly from that another java file, but since it's non-static, and uses
game which can't be static, I think this it's better to first call
calldrawScore() from other function with score and then drawScore() from
it.
Here is the code:
public class GameScreen extends Screen {
enum GameState {
Ready, Running, Paused, GameOver
}
GameState state = GameState.Ready;
public static int score;
Paint paint;
public GameScreen(Game game) {
super(game);
paint = new Paint();
paint.setTextSize(20);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
}
if (state == GameState.Ready)
drawReadyUI();
if (state == GameState.Running)
drawRunningUI();
if (state == GameState.Paused)
drawPausedUI();
if (state == GameState.GameOver)
drawGameOverUI();
}
private void drawScore() {
Graphics g = game.getGraphics();
g.drawString("Score " + score, 100, 20, paint);
}
public static int calldrawScore(int updatedScore) {
score = updatedScore;
GameScreen d= new GameScreen();
d.drawScore();
return score;
}
private void drawReadyUI() {
Graphics g = game.getGraphics();
g.drawARGB(155, 0, 0, 0);
g.drawString("Start", 0, 100, paint);
}
private void drawRunningUI() {
Graphics g = game.getGraphics();
g.drawImage(Assets.pause, 0, 0, 0, 0, 20, 20);
}
private void drawPausedUI() {
Graphics g = game.getGraphics();
g.drawARGB(155, 0, 0, 0);
g.drawString("Resume", 0, 100, paint);
}
private void drawGameOverUI() {
Graphics g = game.getGraphics();
g.drawString("Game Over", 0, 100, paint);
g.drawString("Menu", 0, 200, paint);
}
}
No comments:
Post a Comment