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

2013/9/27

Is this how I spawn objects while having an initial amount in the World?

JasonZhu JasonZhu

2013/9/27

#
    public void addAthids(int a)
    {
        for(int athid=0; athid<a; athid++) {
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject( new Athid(), x, y);
        }
        athidSpawn++;
        if (athidSpawn==50){           
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());            
            addObject( new Athid(), x, y);
            athidSpawn=0;        
        }            
    }
Because it only creates the initial amount, but none spawns. Thanks for any help I get in advance!
JasonZhu JasonZhu

2013/9/27

#
I have a feeling it has something to do with the "int a", however, woulding that only limit the value of "a" athids allowed in the World even with the spawn?
Zamoht Zamoht

2013/9/27

#
What is it that you want? I know this may seem like an odd question, but I'm not sure what your goal is exactly.
JasonZhu JasonZhu

2013/9/27

#
How do I spawn objects when the program is running while when compiling the program, have an initial amount the object in the world?
Zamoht Zamoht

2013/9/27

#
Oh I see. Your code above can be used for adding the initial amount.
public void addAthids(int a)  
{  
    for(int athid=0; athid<a; athid++) {  
        int x = Greenfoot.getRandomNumber(getWidth());  
        int y = Greenfoot.getRandomNumber(getHeight());  
        addObject( new Athid(), x, y);  
    }         
}  
Use this method in the world constructor. I will show you two ways of spawning objects while the program is running. First the constant time based. For this you will need a counter in your world:
private int spawnCounter;
Then add the act method to your world:
public void act()
{
    if (spawnCounter++ > 200) //Use whatever constant you want instead of 200
    {
        addObject(new Athid(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        spawnCounter = 0;
    }
}
The other thing you can do is the random time based spawning. For this you do not need any counter variable.
public void act()
{
    if (Greenfoot.getRandomNumber(100) < 30) //This will give a 30% chance to spawn an object every act
    {
        addObject(new Athid(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
}
danpost danpost

2013/9/28

#
To retain a specific number of athids in the world, use this for your addAthids method and call it from your act method:
public void addAthids(int a)
{
    if (getObjects(Athid.class).size() < a)
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Athid(), x, y);
    }
}
You could use 'while' instead of 'if' if you like; but there will be little difference in performance. And you do not have to initially spawn any in the constructor.
JasonZhu JasonZhu

2013/9/28

#
I understand now, thanks guys and sorry for the late post.
You need to login to post a reply.