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

2014/12/12

How to spawn multiple objects with delay

AnthonioBanderas AnthonioBanderas

2014/12/12

#
Hi, For a game we need to make a system where objects need to spawn in left of the World and move to the right, and spawn with some delay. So an object spawns first, than another, and then again another, just until Gameover. But how am I able to do this? I am able to spawn a object on the left and I tried to make the spawn system work but it just doesn't. This was the basic code so far (for spawning a fly on the left):
1
2
3
4
5
6
7
8
9
10
public class PortWorld extends World
{
 
    public PortWorld()
    {   
        super(800, 600, 1);        
        addObject(new Fly(), 10, Greenfoot.getRandomNumber(135)+235);
    }
     
}
I saw a similar topic on this issue and I copied it in my code and it didn't work. This is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class PortWorld extends World
{
 
    private int spawnRate; 
 
    public PortWorld()
    {   
 
        super(800, 600, 1);        
        checkSpawning();
    }
     
    private void checkSpawning()
    {
        spawnRate = (spawnRate+1)%100;
        if (spawnRate == 0) addObject(new Fly(), 10, Greenfoot.getRandomNumber(135)+235);
    }
        
}
Can somebody help me with this issue? Thanks
davmac davmac

2014/12/12

#
You're calling the checkSpawning method from the world constructor, which itself only runs once, when the world is created. It sounds like you want to call it regularly, which usually means you should call it from the act() method.
AnthonioBanderas AnthonioBanderas

2014/12/12

#
davmac wrote...
You're calling the checkSpawning method from the world constructor, which itself only runs once, when the world is created. It sounds like you want to call it regularly, which usually means you should call it from the act() method.
Do you mean the act-method of the fly Actor?
danpost danpost

2014/12/12

#
AnthonioBanderas wrote...
Do you mean the act-method of the fly Actor?
No. Your fly population will take over the world if you did that. Each fly created would join in on creating more flies. One becomes two, two becomes four, four becomes eight -- it would not take long before your world was inundated with flies or you throw some kind of exception. This would definitely be a job for your PortWorld act method.
AnthonioBanderas AnthonioBanderas

2014/12/12

#
danpost wrote...
AnthonioBanderas wrote...
Do you mean the act-method of the fly Actor?
No. Your fly population will take over the world if you did that. Each fly created would join in on creating more flies. One becomes two, two becomes four, four becomes eight -- it would not take long before your world was inundated with flies or you throw some kind of exception. This would definitely be a job for your PortWorld act method.
Ok thank you, but the CheckSpawning is already there in the PortWorld act as you see in the 2nd code, right? How would the code look like if it would work? Could you send an example? Thankyou
danpost danpost

2014/12/12

#
AnthonioBanderas wrote...
Ok thank you, but the CheckSpawning is already there in the PortWorld act as you see in the 2nd code, right?
No. Not right. The checkSpawning method is being called from the constructor of the class, not the act method. At the moment, the act method of your PortWorld class is empty (inherited from the World class; see the documentation). You need to override it and call checkSpawning from it in your PortWorld class.
AnthonioBanderas AnthonioBanderas

2014/12/12

#
danpost wrote...
AnthonioBanderas wrote...
Ok thank you, but the CheckSpawning is already there in the PortWorld act as you see in the 2nd code, right?
No. Not right. The checkSpawning method is being called from the constructor of the class, not the act method. At the moment, the act method of your PortWorld class is empty (inherited from the World class; see the documentation). You need to override it and call checkSpawning from it in your PortWorld class.
Thank you so much. It works now
You need to login to post a reply.