Home > Net >  Is there an alternative code for wait() in java?
Is there an alternative code for wait() in java?

Time:02-28

I am a beginner in coding and I am creating a pokemon game for practice. I have three buttons; attack, run, and magic. When I click any of the buttons, I want it to implement its respective function (e.g. when I click the attack button, it will attack the enemy and then pause for a few seconds, then the enemy will be the one to attack after the user's turn.)

public void onClick (View view) {
    txtAttackDetails.findViewById(R.id.attackDetails);

    Random random= new Random();
    int herodamage= random.nextInt(heroMaxDamage-heroMinDamage) heroMinDamage;
    int monsterdamage= random.nextInt(monsterMaxDamage-monsterMinDamage) monsterMinDamage;

    txtHeroHP.setText(String.valueOf(heroHealth));
    txtMonsterHP.setText(String.valueOf(monsterHealth));

    switch (view.getId()){
        case R.id.slash:
            monsterHealth=Math.max(0,monsterHealth-herodamage); //hero deals damage
            txtAttackDetails.setText(String.valueOf("Hero dealt " herodamage "damage."));
            txtMonsterHP.setText(String.valueOf(monsterHealth));

            if(monsterHealth<0) {
                txtAttackDetails.setText("Hero wins!");
            }

            // pause for a while

            heroHealth=Math.max(0,heroHealth-monsterdamage); // monster deals damage
            txtAttackDetails.setText(String.valueOf("Monster dealt" monsterdamage "damage."));
            txtHeroHP.setText(String.valueOf(heroHealth));

            if (heroHealth<0)
                txtAttackDetails.setText("Hero lost! Try again?");
            break;

Also, for some reason when I try to run this program, it crashes. MonsterHealth and HeroHealth does not change. I am thinking it is because of the buttons. Does anyone know how to fix this as well?

CodePudding user response:

Looking at the question title and not seeing // pause for a while having appropriate code it seems you want to know how to make the code pause for a while. Object.wait() does not serve that purpose - you likely have figured out that already.

Use Thread.sleep(); instead.

Regarding the crash: You have not supplied enough information. Anyway it is a better pattern to address one problem per question.

CodePudding user response:

Calling wait() does not do what you expect. It actually block the current thread until it gets a notify from another thread. That is not the behavior that you need here. Furthermore, you can only call wait() when the current thread is holding the object lock on the object you are waiting on. If you don't hold it, you get an IllegalMonitorStateException ... and there is a good chance that is what caused your app to crash.

(If you are new to Android App development, you should read the Android documentation on Debugging your App. That include instructions on how to find and then read the stacktraces that crashes produce. This is a vital skill.)

The Thread.sleep() idea proposed in this answer is not the solution either. Sure, it will cause the current thread to wait. However the current thread is the event notification thread that handles screen events. If you call sleep() on that thread, you will freeze the app's user interface.

What you actually need to do (for an Android app) is to implement the code that you want to run after the delay as a Runnable (or a lambda) and then schedule it to run later on another thread. Then you let the current thread continue.

See How to pause / sleep thread or process in Android? for more information.

Note that some of the simpler answers on that question have a risk of a memory leak ... as noted.

  • Related