How to make it so that when you change the settings, the scenes change and work without problems?

In the wallpaper in the settings you can select 2 scenes, with a tank and a bus. The class AndroidLauncher spelled

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); initialize(new AutoRender(), config); } 

and class AutoRender:

 class AutoRender implements ApplicationListener { float screenOffset; boolean settings_changed_flag = false; private Tank tank = new Tank(); private Bus bus = new bus(); int character; @Override public void create() { if (character == 1) tank.create(); if (character == 2) bus.create(); } @Override public void resize(int width, int height) { } @Override public void render() { if (character == 1) tank.render(); if (character == 2) bus.render(); } @Override public void pause() { } @Override public void resume() { } @Override public void dispose() { } } 

with this code, when switching scenes, an error pops up

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.badlogic.gdx.scenes.scene2d.Stage.act(float)' on a null object reference 

in the wallpaper itself in the preview goes the inscription that the application is stopped, but then the scene loads and everything works as it should. If you change the create () method of the AutoRender class:

 @Override public void create() { tank.create(); bus.create(); } 

then the scenes switch instantly without error, but the scene with the tank no longer responds to pressing.

And if you leave it as it was, and add

 @Override public void resume() { this.resume(); } 

then the wallpaper reboots when you call or block ..

How to make it so that when you change the settings, the scenes change and work without problems?

  • If I understood everything correctly, then the following happens: in the first case, when switching, you start rendering the scene without creating it, which is why the NullPointerException error occurs. In the second case, the scene with the tank does not respond to pressing, most likely because Gdx.input.setInputProcessor () is called in the create () method and it turns out that only bus will always respond to input. - Master Flomaster
  • This I understand, just not enough knowledge of which method to use. I think you need to prescribe something in the methods resume (), pause () ... - Rusl Mag
  • resume () and pause () have nothing to do with it. It is necessary to create the changeCharacter (int character) method in AutoRender and already in it, when changing the scene, release the resources of the current scene and create a new one. - Master Flomaster
  • It sounds simple and logical, but I can't do it. If possible, write at least a small piece of code, I did not find examples on the network. - Rusl Mag
  • one
    First, in Tank and Bas, you need to create the setInput () method and transfer all the code responsible for registering the input into it. I believe that you are using Stage, so you need to register Gdx.input.setInputProcessor (stage). Next, create the AutoRender.changeCharacter method (int newCharacter) {if (character == newCaracter) return; if (newCharacter == 1) {character = 1; tank.setInput (); } else if (newCharacter == 2) {character = 2; bus.setInput (); }}. And in your case you will have to create tank and bus right away, i.e. AutoRender.create () {tank.create (); bus.create (); }. I think it should be so, because I did not see the full code. - Master Flomaster

0