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

2015/1/15

The actors are spawning twice

Pe1per Pe1per

2015/1/15

#
I have a problem with spawning new Actors! At the start of the Game there are 6 Zombies in the world, after a while I want to spawn more zombies, but I only want that one Zombie is spawning in the map, but if there are 4 Zombies in the world, other 4 Zombies are spawning, if there are 8, also 8 Zombies are spawning. And if there are 0 Zombies in the world other Zombies will never spawn and I don't understand why
public void SpawnZombie() //sorgt dafür das neue Zombies erscheinen
    {
        Zcount++;
        Actor Tank = getOneIntersectingObject(tank.class);
        RNumber = Greenfoot.getRandomNumber(15);
        if (Zcount == 500 && tank.class != null && RNumber == 1){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 662, 738);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 2){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 115, 84);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 3){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 1515, 94);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 4){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 1511, 719);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 5){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 662, 738);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 6){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 1216, 387);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 7){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 535, 321);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 8){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 297, 458);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 9){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 927, 736);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 10){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 795, 185);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 11){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 1164, 119);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 12){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 386, 160);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 13){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 1041, 73);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 14){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 256, 620);
            Zcount = 0;
        }
        if (Zcount == 500 && tank.class != null && RNumber == 15){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            getWorld().addObject (new Zombie(), 828, 742);
            Zcount = 0;
        }
    }
There are a lot of different places in the world because I don't want to spawn them randomly... I hope you can help me with the problem :)
danpost danpost

2015/1/15

#
You probably placed the 'spawnZombies' method in the Zombie class, which is why you are getting that behavior of spawning (0 -->0; 1 -->2; 2-->4; 4-->8). Each zombie in the world is executing the code. Place the method in your World sub-class (called from the act method of the class). 'tank.class' refers to a Class object and not to the objects created from the class and will never be 'null'; so 'tank.class != null' will always be 'true'.
Pe1per Pe1per

2015/1/15

#
ok it works allmost, the problem now is that after a while there are no more zombies spawning.. I can't find the fault
public void act()
    {
        SpawnZombie();
        SpawnMunition();
    }
    
    public void SpawnZombie() //sorgt dafür das neue Zombies erscheinen
    {
        Zcount++;
        RNumber = Greenfoot.getRandomNumber(15);
        if (Zcount == 50){
        if (RNumber == 1){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 662, 738);
            Zcount = 0;
        }
        if (RNumber == 2){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 115, 84);
            Zcount = 0;
        }
        if (RNumber == 3){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 1515, 94);
            Zcount = 0;
        }
        if (RNumber == 4){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 1511, 719);
            Zcount = 0;
        }
        if (RNumber == 5){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 662, 738);
            Zcount = 0;
        }
        if (RNumber == 6){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 1216, 387);
            Zcount = 0;
        }
        if (RNumber == 7){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 535, 321);
            Zcount = 0;
        }
        if (RNumber == 8){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 297, 458);
            Zcount = 0;
        }
        if (RNumber == 9){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 927, 736);
            Zcount = 0;
        }
        if (RNumber == 10){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 795, 185);
            Zcount = 0;
        }
        if (RNumber == 11){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 1164, 119);
            Zcount = 0;
        }
        if (RNumber == 12){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 386, 160);
            Zcount = 0;
        }
        if (RNumber == 13){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 1041, 73);
            Zcount = 0;
        }
        if (RNumber == 14){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 256, 620);
            Zcount = 0;
        }
        if (RNumber == 15){ // Zombies können erscheinen wenn Zcount auf 500 ist und es nicht schon über 4 mal neue Zombies gab.
            Zombie zombie = new Zombie();
            addObject (new Zombie(), 828, 742);
            Zcount = 0;
        }
       }
    } 
Super_Hippo Super_Hippo

2015/1/15

#
It happens when RNumber is 0. Then no zombie is spawned and ZCount isn't resetted to 0. By the way, this does the same:
    public void spawnZombie() //sorgt dafür, dass neue Zombies erscheinen
    {
        Zcount++;
        
        if (Zcount == 50)
        {
            int x,y,RNumber = Greenfoot.getRandomNumber(15);
            switch(RNumber)
            {
                case 0: x= 662; y=738; break;
                case 1: x= 115; y= 84; break;
                case 2: x=1515; y= 94; break;
                case 3: x=1511; y=719; break;
                case 4: x= 662; y=738; break; //this is the same as case 0, should it be like that?
                case 5: x=1216; y=387; break;
                case 6: x= 535; y=321; break;
                case 7: x= 297; y=458; break;
                case 8: x= 927; y=736; break;
                case 9: x= 795; y=185; break;
                case 10:x=1164; y=119; break;
                case 11:x= 386; y=160; break;
                case 12:x=1041; y= 73; break;
                case 13:x= 256; y=620; break;
                case 14:x= 828; y=742; break;
            }
            addObject(new Zombie(), x,y);
            Zcount = 0;
        }
    }
Alternative:
private int[] x = {662,115,1515,1511,662,1216,535,297,927,795,1164,386,1041,256,828};
private int[] y = {738,84,94,719,738,387,321,458,736,185,119,160,73,620,742};

public spawnZombie()
{
    ZCount++;
    if (ZCount==50)
    {
        int RNumber = Greenfoot.getRandomNumber(15);
        addObject(new Zombie(),x[RNumber],y[RNumber]);
        ZCount=0;
    }
}
Another thing. If you want to upload the game here, the size of your game can be a problem.
Pe1per Pe1per

2015/1/15

#
Ok i'll try it and what means break behind the y coordinate? :) For the moment the game is only for a presentation in school so the size is no problem, but thank you for the information :)
Super_Hippo Super_Hippo

2015/1/15

#
'break' exits the 'switch' in this case. It can also exit loops.
Pe1per Pe1per

2015/1/15

#
Ok it works good thank you :) I have another problem. I want that my figure (it's a tank) have three lives. I have made a class for the lives and want to change the image of the live class every time my tank hits a zombie. There are no syntax errors but if I play the game there is a null pointer error every time I hit the zombie and I don't know why...
Pe1per Pe1per

2015/1/15

#
private void checkZombie() // überprüft ob Panzer mit Zombie kollidiert
    {
        Actor Zombie = getOneIntersectingObject(Zombie.class);
        if(Zombie != null && lives == 3 && ZombieContact == true){
           Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
           setImage("2Herzen.png");
           getWorld().removeObject(this);
           lives = 2;
           boolean ZombieContact = false;
        }
         if(Zombie != null && lives == 2 && ZombieContact == true){
           Leben leben = (Leben) getWorld().getObjects(Leben.class).get(0);
           setImage("1Herzen.png");
           getWorld().removeObject(this);
           lives = 1;
           boolean ZombieContact = false;
        }
        if(Zombie != null && lives == 1 && ZombieContact == true){ // wenn er kollidiert ist das Spiel vorbei 
           World myWorld = getWorld();
           GameOver gameover = new GameOver(); // Game Over Zeichen am Ende
           myWorld.addObject(gameover,800,400); // X und Y Koordinate des Game Over Zeichens
           Greenfoot.playSound("gameover.mp3"); // Endmusik
           getWorld().removeObject(this);
           DeathCount = 1; // wird benötigt um Abfrage zu beenden ob Panzer mit Munition kolidiert
           Greenfoot.stop(); // Stoppt das Spiel
        }
    }
Super_Hippo Super_Hippo

2015/1/15

#
In which class is this code?
Pe1per Pe1per

2015/1/15

#
The code is in the tank class and I want to change the image from the Leben class :)
Super_Hippo Super_Hippo

2015/1/15

#
Then you should use leben.setImage("2Herzen.png"); in line 6. (Similar for line 13) Do you want to remove the tank when it collides with the zombie?
davmac davmac

2015/1/15

#
Lines 9 and 16 are declaring new variables, which are then unused. You probably just need to remove the 'boolean' from these lines, to set the value of the existing variable instead.
Pe1per Pe1per

2015/1/15

#
ok thank you all you really helped me :) Now it works fine!
You need to login to post a reply.