This site requires JavaScript, please enable it in your browser!
Greenfoot back
TravJ.14
TravJ.14 wrote ...

2020/6/19

Random Spawning Mechanics

TravJ.14 TravJ.14

2020/6/19

#
I am creating a game similar to space invaders, where ships (or in my case balloons) fall down to the bottom and you have to shoot them. I want to randomly spawn different colored balloons (signifying different strength). Simply put, the game is a combination of space invaders and bloons tower defense. Each 60 seconds, the level goes up, which makes the game harder. For the first 4 levels, a new colored balloon should be added into the mix every level. However, the 3rd level difficult and 4th level don't ever join in. I have checked the code and the first 2 levels look pretty much identical to 3 and 4 (with some exceptions to delay the entrance until the 3rd level). Here is a look at the yellow (which is coded in the MyWorld class): public void yellow() { if(level==3) { yellowTimer++; if(yellowTimer==270) { yellowRandom= Greenfoot.getRandomNumber(4); if(yellowRandom==1) { addObject(new yellow(), Greenfoot.getRandomNumber(800),0); } if(yellowRandom==2) { addObject(new red(), Greenfoot.getRandomNumber(800),0); addObject(new red(), Greenfoot.getRandomNumber(800),0); } yellowTimer=0; } } } I have quite a lot of code in the first page, and I don't want to post all of it here, so if you think you may have a fix, leave a comment and I can email you the entire file.
danpost danpost

2020/6/19

#
You probably are calling a random number in all cases and you are probably adding more balloons per unit time as the level increases. I would suggest that the level value be used in both of these. Then a switch structure can be used for spawning. It would be something like the following which would be replacing all your color methods (like 'yellow' shown above):
public void spawning()
{
    if (timer == (240/level)) // faster spawning per level
    {
        int rand = 1+Greenfoot.getRandomNumber(level); // more balloon colors per level
        switch(rand)
        {
            case 4: /** add blue */ break;
            case 3: /** add yellow */ break;
            case 2: /** add red */ break;
            case 1: /** add green */ break;
        }
    }
    timer = 0; // timer reset
}
There are some tweaks that can be done to bias specific colored balloons to spawn more frequently than others, if wanted.
You need to login to post a reply.