mercredi 24 avril 2019

Generating a number to decide which intent to pick and move to said intent

I am currently trying to make a little minigame application in Android Studio. I have a problem - I need to generate a random number that is not the same as the previously generated number and I used said number to decide which intent I will move to. I use the code below to generate the random number which I have stored in a class where I keep things like the current score called globalVariablesGame, but when I call globalVariablesGame and ask for a random number, the lastrandomnumber for some reason doesn't change at all no matter how many are generated.

int getRandomNumber() {
randomNumber = random.nextInt(3);
while (randomNumber == lastRandomNumber){
         randomNumber = random.nextInt(3)}
lastRandomNumber = randomNumber;
return randomNumber;
}

To decide which intent I use

int nextgame = globalVariablesGame.getRandomNumber(); // Call this int to determine what the next MG  is going to be played
if (nextgame == 0) {
                    Intent myIntent = new Intent(context, Minigameone.class);
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    finish();
                    startActivity(myIntent);
                }
                if (nextgame == 1) {
                    Intent myIntent = new Intent(context, Minigametwo.class);
                    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    finish();
                    startActivity(myIntent);
                }

Obviously I need the lastrandomnumber to actually change when I generate a new number but it doesn't, any help?




Aucun commentaire:

Enregistrer un commentaire