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

2017/5/25

How to limit spawning?

Penlos Penlos

2017/5/25

#
Hi, I am currently programming a game, in which you shoot birds, who are spawning random on the screen and move around. But I want to limit the number of birds flying around to 15 at the same time. My problem is: Where should I put the code for it? The birds are spawned from the world and I already tried limiting their number in constructor and act in the bird actor class with getWorld().numberOfObjects()>15 , but in the constructor it doesn't work and in act they would stop their moving. I just want the program to stop spawning them, until there are less than 15 birds. Thanks in forward.
Trystar360 Trystar360

2017/5/25

#
i would do something like this in the world
    public int numOfBirds = 15;
    public void act()
    {
        if(numOfBirds > 0)
        {
            //your code to spawn the birds
            numOfBirds --;
        }
    }

then in the bird class
public void die()
    {
        MyWorld world = (MyWorld)getWorld();
        world.numOfBirds ++;
        world.removeObject(this);
    }
Super_Hippo Super_Hippo

2017/5/25

#
You should be able to limit it in the world's act method. But you should check for <15 and not >15 (in case that wasn't a typo).
Penlos Penlos

2017/5/25

#
Super_Hippo wrote...
You should be able to limit it in the world's act method.
My program spawns birds all the time, so how could I limit it in world's act method? In the worlds act method I can't access the number of birds in the world. Trystars360's code would be possible, but it doesn't work with my programming that well, so I would prefer it, if I just could check anywhere how many birds are flying around at the moment and then let the program stop spawning birds until there are less than 15 birds.
Trystar360 Trystar360

2017/5/25

#
in the world prepare you could do
for(int b = 15; b > 0; b --)
{
       // add bird
}
Trystar360 Trystar360

2017/5/25

#
but if you do the other one right it will keep track of how many birds are in the world. as long as whenever you remove a bird you add one to numOfBirds.
Super_Hippo Super_Hippo

2017/5/25

#
Why can't you get the number of birds in your world's act method?
public void act()
{
    if (getObjects(Bird.class).size()<15 && Greenfoot.getRandomNumber(50)==0)
    {
        addObject(new Bird(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
Penlos Penlos

2017/5/25

#
Trystar360 wrote...
but if you do the other one right it will keep track of how many birds are in the world. as long as whenever you remove a bird you add one to numOfBirds.
You are right, it's a good idea, but it would be a little bit boring, if always when you kill a bird immediately another spawns.
Penlos Penlos

2017/5/25

#
Super_Hippo wrote...
Why can't you get the number of birds in your world's act method?
public void act()
{
    if (getObjects(Bird.class).size()<15 && Greenfoot.getRandomNumber(50)==0)
    {
        addObject(new Bird(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
That is a good idea, but why do you use Greenfoot.getRandomNumber(50)==0)?
Super_Hippo Super_Hippo

2017/5/25

#
This way the next one won't immediately spawn.
Penlos Penlos

2017/5/25

#
Super_Hippo wrote...
This way the next one won't immediately spawn.
Ah, I see. Great idea. Works well, but now, if I do nothing and kill no birds I get a problem: java.lang.NullPointerException at Bird.act(Bird.java:41) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/5/25

#
Penlos wrote...
now, if I do nothing and kill no birds I get a problem: java.lang.NullPointerException at Bird.act(Bird.java:41)
Need to see the Bird class codes. Indicate which line is line 41.
You need to login to post a reply.