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

2020/5/5

Want to add a delay between when the green spawn.

scaps scaps

2020/5/5

#
Trying to add a delay between when they spawn so they all dont spawn on top of each other. Tried adding a delay but still doesnt work. Spawner:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spawner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spawner extends Actor
{
    /**
     * Act - do whatever the Spawner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int zombies2kill = 5;
    public int zombieskilled= 0;
    public int zombies2spawn = 0;
    private int delay = 0;
    public void act() 
    {
        if(delay > 0){
            delay--;
        }
        
        if(delay == 0) spawn();
        
    }    
    
    private void spawn(){
        while(zombies2spawn <= zombies2kill){
        Green green = new Green();
        int lr = Greenfoot.getRandomNumber(3);
        if(lr==2){
            getWorld().addObject(green,580,380);
            zombies2spawn++;
            delay = 80;
        }
        if(lr==1){
            getWorld().addObject(green,0,380);
            zombies2spawn++;
            delay = 80;
        }
    }
    }
}
Green:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class green here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Green extends Actor
{
    /**
     * Act - do whatever the green wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(1);
        if(isTouching(projectile_1.class)){
            
            getWorld().removeObject(this);
            
        }
    }    
}
Super_Hippo Super_Hippo

2020/5/5

#
Your while loop is running until all the Zombies are added. You don’t want to have a loop there. You only want to add a single one and then wait until the delay variable is back to 0 before adding the next one.
scaps scaps

2020/5/5

#
ohh alright, and then how can I set a limit to the amount that spawn?
scaps scaps

2020/5/5

#
Oh wait just realized && exists
scaps scaps

2020/5/5

#
alr got it working thanks a million
You need to login to post a reply.