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

2019/10/26

How can I spawn actors for a 5 second and then removed them and replace with others

BestMannCZ BestMannCZ

2019/10/26

#
Hi, I need help. I create actor called Vosa and I want spawn 15x Vosa on random position and after 5 seconds I wanna removed them and spawend another 15x Vosa, And after this will countinue 60 second and than it will stopped the game. can you help me pls ? public class MyWorld extends greenfoot.World { public void pridejVosu() // pridej = add { Vosa v = new Vosa(); int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); this.addObject(v, x, y); } public MyWorld() { super(1177, 768, 1); for(int i=0; i<15; i++) { this.pridejVosu(); } }
Super_Hippo Super_Hippo

2019/10/27

#
If no other Vosa are in the world (so you want to remove all Vosas from the world when creating 15 new ones), then you can use this to remove all before adding the new ones:
removeObjects(getObjects(Vosa.class));
To make it happen after 5 seconds, you can use an int variable as a timer. Create an act method to increase the int variable each act cycle. If you have the speed slider in the middle (speed = 50), you will probably have around 55 act cycles per second (55 ACS).
private final int ACS = 55;
private int timer = 0;

public void act()
{
    timer++;
    if (timer % (5*ACS) == 0) //every 5 seconds
    {
        //remove all Vosas
        //spawn 15 new Vosas
        if (timer == 60*ACS) //after 60 seconds
        {
            Greenfoot.stop();
        }
    }
}
BestMannCZ BestMannCZ

2019/10/29

#
Thank you very much
You need to login to post a reply.