3
\$\begingroup\$

In my game, when the users clicks an image button, the value of a boolean changes and then the game is closed. I mean, if I want to restart the game, I click this image button, the game is closed and immediately is opened. However this doesn't happen because the value of the boolean is false and I don't know why at all.

Here is the code of the image-button:

    playagain.addListener(new ClickListener()
    {
        @Override
        public void clicked(InputEvent event, float x, float y)
        {
            interfaccia.rigioca();
            Gdx.app.exit();
        }
    });

And this is the code of the MainActivity:

public class MainActivity extends Activity implements MyGdxGame.GestioneClick, Serializable
{
MainActivity activity;
boolean start;
@Override
public void onCreate(final Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.layout);
    activity = this;
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            startActivity();
        }
    });

}

@Override
public void onResume()
{
    super.onResume();
    if(start)
        startActivity();
}

@Override
public void menu()
{
    start=false;
}

@Override
public void rigioca()
{
    start=true;
}

public void startActivity()
{
    Intent intent = new Intent(MainActivity.this,AndroidLauncher.class);
    intent.putExtra("MainActivity",activity);
    activity.startActivity(intent);
}

}

As you can see, rigioca method is called and start sould be true. I checked ALL with logs and we can say that the value is true. Then resume method is called but start value is false! How is it possible? I also took away start=false; in menu method, but that isn't the problem. Why is this variable false!?

Note: onDestroy and onCreate aren't called but the activity is destroyed.

\$\endgroup\$
  • \$\begingroup\$ Surely there's a better way to do this than to exit the entire application then starting it again? Or do you need to close the application for some reason? \$\endgroup\$ – Charanor Jun 16 '17 at 20:18
  • \$\begingroup\$ @Charanor I think there is a better way, but I used it because it seems be easy and effective \$\endgroup\$ – Curio Jun 16 '17 at 20:24
  • \$\begingroup\$ Well it seems a whole lot more complicated than just resetting some variables. It is also a whole lot more cumbersome for your players. Can't you just reset the variables your game depends on back to their original state? \$\endgroup\$ – Charanor Jun 16 '17 at 20:31
  • \$\begingroup\$ @Charanor what do you mean for "Can't you just reset the variables your game depends on back to their original state" ? \$\endgroup\$ – Curio Jun 16 '17 at 20:35
  • \$\begingroup\$ Well for example, say you have a turn-based game. You might have a variable like currentTurn, an array of enemies, and so on. Instead of completely restarting your game just set currentTurn = 0 and do enemies.clear() to reset them to their original state. \$\endgroup\$ – Charanor Jun 16 '17 at 20:42
2
\$\begingroup\$

(from chat)

You should not be restarting your entire program when you wish to restart your game. Instead reset the variables that control your game to their original state.

Take this game as an example: example game image

In this example I reset the player's health (top left), the level (top right), and the abilities (bottom right) whenever the player pushes my 'reset' button.

The code looks something like this:

gameOver = false; 
handler.clear(); 
director.reset(); 
level = 0; 
timer = 0; 
enemies = null;
shields = 0;
teleports = 0;

I simply reset all of the variables to what they were when the game started :)

\$\endgroup\$
  • \$\begingroup\$ But why is the activity destroyed? \$\endgroup\$ – Curio Jun 22 '17 at 13:12
  • \$\begingroup\$ @Curio Probably Probably one of two reasons: 1) Your start activity code is in onResume but should probably be in onPause so try putting it there. 2) It could be a life-cycle problem where Gdx.app.exit() simply closes the app no matter what. Also - you're using LibGDX. Why aren't you using the standard LibGDX android launcher activity? \$\endgroup\$ – Charanor Jun 22 '17 at 13:35
  • \$\begingroup\$ Is there one? I didn't know this \$\endgroup\$ – Curio Jun 22 '17 at 21:01
  • \$\begingroup\$ @Curio Yes. When you create a LibGdx project from their setup launcher you can chose to automatically set up Android for LibGdx. This will automatically create an android activity that you can fully customize. \$\endgroup\$ – Charanor Jun 22 '17 at 21:03
  • \$\begingroup\$ There are two folders: android and core. I create this activity in android folder. If it is so, then I know it. \$\endgroup\$ – Curio Jun 22 '17 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.