dimanche 21 juin 2020

Is there a way I can create a random number that spawns a heart?

I have created code for a game that spawns a heart as an extra life continuously, however, I only want it to have a chance of happening sometimes, I have decided the best approach would be to generate a random number and if the number exceeds a certain value it will spawn a heart.

random = new Random();

    double randHeartNum = random.nextDouble();

    while (isPlaying == true) {

        if (randHeartNum < 0.050) {
            for (int i = 0; i < 1; i++) {
                Bird heart = new Bird(getResources());
                hearts[i] = heart;
            }
            randHeartNum = new Random().nextDouble();
        }
    }

the for loop spawns in the heart successfully but when it is surrounded by my random number generator it does not work.




Aucun commentaire:

Enregistrer un commentaire