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

2022/12/22

spawn villains

Magzium Magzium

2022/12/22

#
I want to know how i can spawn one villain after another. It would be nice if there was a cooldown of about 3 seconds.
danpost danpost

2022/12/22

#
Magzium wrote...
I want to know how i can spawn one villain after another. It would be nice if there was a cooldown of about 3 seconds.
Probably the best way is to use a timer for your world act method:
// in world class
private int spawnTimer;

public void act()
{
    spawnTimer = (spawnTimer+1)%180;
    if (spawnTimer == 0) addObject(new Villian(), 0, 0);
    // other act stuff, if any
}
The 0, 0 can be replaced with whatever values you feel suit your scenario.
Magzium Magzium

2022/12/22

#
Sorry if i sound rude. But do you also know how i can get a SpawnTimer. Or can you change the code so my SimpleTimer can be used?
danpost danpost

2022/12/22

#
Magzium wrote...
do you also know how i can get a SpawnTimer. Or can you change the code so my SimpleTimer can be used?
The SimpleTimer support class uses real time, which is not the best for this purpose. Any lag in your system will cause inconsistent timing of spawns. Just use a simple Actor object to display the spawnTimer value:
private Actor spawnTimerDisplay = new Actor(){};

private void updateSpawnTimerDisplay()
{
    String caption = "Villian spawns in "+(3-spawnTimer/60)+" seconds"; // or "2-", if that suits you better
    GreenfootImage img = new GreenfootImage(caption, 28, Color.BLACK, new Color(0, 0, 0, 0));
    spawnTimerDisplay.setImage(img);
}

public void act()
{
    spawnTimer = (spawnTimer+1)%180;
    if (spawnTimer%60 == 0) updateSpawnTimerDisplay();
    if (spawnTimer == 0) addObject(new Villian(), 0, 0);
}
I prefer to create a new class extending Actor called either SimpleActor or Aktor as follows:
public class SimpleActor extends greenfoot.Actor {}
That one line IS the entire class code. You can then use it to create most, if not all, of your GUI type actors. For the above, just change line 1 to:
private Actor spawnTimerDisplay = new SimpleActor();
You can add a line to your world constructor to initialize the timer display:
updateSpawnTimerDisplay();
You need to login to post a reply.