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

2023/3/28

how to spawn zombies with different stats after a set amount of time

envxity envxity

2023/3/28

#
im looking on how to spawn different zombies that after a certain time frame it will become random if they are the speed zombies or normal zombies then again after the 2nd period of time to spawn a tank heres the code i have already:
if(time == 60)
        {
            randomZombie = Greenfoot.getRandomNumber(2);
            switch(randomZombie)
            {
                case 0: zombie.speed = 1; zombie.health = 5;
                case 1: zombie.speed = 5; zombie.health = 3;
            }
        }
        if(time == 120)
        {
            randomZombie = Greenfoot.getRandomNumber(3);
            switch(randomZombie)
            {
                case 0: zombie.speed = 1; zombie.health = 5;
                case 1: zombie.speed = 5; zombie.health = 3;
                case 2: zombie.speed = 1; zombie.health = 10;
            }
        }
Spock47 Spock47

2023/3/28

#
The current code modifies an already existing zombie. I guess you want to spawn a *new* zombie, right? A new zombie can be created by calling the constructor, presumably "new Zombie();". This zombie then has to be placed into the world (addObject method). Assuming the code you showed is in method spawnZombie of your world class, you can try this:
    private void spawnZombie() {
        int zombieType = -1;
        switch (time) {
            case 60: zombieType = Greenfoot.getRandomNumber(2); break;
            case 120: zombieType = Greenfoot.getRandomNumber(3); break;
            default: return;
        }
        final Zombie zombie = new Zombie();
        switch (zombieType) {
            case 0: zombie.speed = 1; zombie.health = 5; break;
            case 1: zombie.speed = 5; zombie.health = 3; break;
            case 2: zombie.speed = 1; zombie.health = 10; break;
        }
        addObject(zombie, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight())); // change position as you need.
    }
Other assumptions: - you already increase the time variable with every act-step. - you already call spawnZombie from act method. This code will spawn exactly two zombies: - after one second one normal or speed zombie - after two seconds one normal or speed or tank zombie.
You need to login to post a reply.