-1

in the class GameScreen I wrote this code for a score:

if (Gdx.input.justTouched()&& executed==true) {
  MyGdxGame.camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
  for (int i=0;i<4;i++) {
    if (sprite[zahl[i]].getBoundingRectangle().contains(touchPoint.x, touchPoint.y) && zahl[4] == zahl[i]) {
      int scoreValue = Integer.parseInt(score);
      scoreValue++;
      score = String.valueOf(scoreValue);
      executed= false;
    }
    if (sprite[zahl[i]].getBoundingRectangle().contains(touchPoint.x, touchPoint.y) && zahl[4] != zahl[i]){
      this.dispose();
      game.setScreen(new GameOverScreen(game));
      return;
    }
  }
}

The second if refers to the class GameOverScreen. So if a user loses the game, the GameOverScreen would be shown. Now I want to show the reached score on the GameOverScreen. Therefore I have to use the changing variable score from the GameScreen class in the GameOverScreen class.

My question is: How can I use variables through different classes?

DaveyDaveDave
  • 7,459
  • 11
  • 57
  • 68
user8340536
  • 189
  • 7
  • You need to ask a specific question, not a broad 'how do I do this?'. I'd strongly suggest reading [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask), then coming back and editing your question to take account of the suggestions there. – DaveyDaveDave Jul 31 '17 at 09:53
  • 1
    I edited my question. I hope I can now take account of the suggestions. – user8340536 Jul 31 '17 at 10:06
  • Simply the question is: How to pass variables from one screen to another. – salih kallai Aug 20 '17 at 22:32
  • possible duplicate of https://stackoverflow.com/questions/42176696/libgdx-how-to-pass-a-variable-value-from-one-screen-to-another – salih kallai Aug 20 '17 at 22:33

2 Answers2

1

You could create a constructor that takes the score as the parameter in your GameOverScreen.

e.g:

game.setScreen(new GameOverScreen(game, TheScore));
Kuffs
  • 34,562
  • 10
  • 74
  • 91
-2

You can save the variable on Android sharedpreferences. See the example here

Kharda
  • 1,070
  • 11
  • 18