On line 34, you set the counter value to 10. Ten act cycles will expire quite quickly (maybe 0.2 seconds). Try increasing the value to something like 300.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class TwoPlayer_Button here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TwoPlayer_Button extends Actor
{
/**
* Act - do whatever the TwoPlayer_Button wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
startGame2P();
}
/**
* Start 2 player world.
*/
public void startGame2P()
{
if (Greenfoot.mouseClicked(this))
{
Greenfoot.setWorld(new TwoPlayer());
}
}
}
import greenfoot.*;
public class TwoPlayer extends Main_Menu
{
private TwoPlayer_Button twoPlayer_Button;
private int countdown;
private Actor[] survivors;
private Walls Walls;
public TwoPlayer()
{
}
public void act()
{
prepare();
canAddWalls();//Walls kept spawning 5 million times
walls();
infect();
}
private void walls()
{
if (canAddWalls()) addObject(new Walls(), 683, 384);
}
public boolean canAddWalls()
{
// in the world class, use this line
return getObjects(Walls.class).size() < 1;
}
private void prepare()
{
if (survivors == null)
{
survivors = new Actor[] { new SurvivorWASD(), new SurvivorARROW() };
addObject(survivors[0], 393, 372);
addObject(survivors[1], 574, 536);
canAddWalls();
}
}
private void infect()
{
if (Greenfoot.mouseClicked(twoPlayer_Button)) // countdown starting
{
countdown = 300;
}
if (countdown == 0) return; // countdown already completed
countdown--;
if (countdown == 0) // countdown just completed
{
Actor[] viruses = new Actor[] { new VirusWASD(), new VirusARROW() }; // must be same order as 'survivors'
int chosen = Greenfoot.getRandomNumber(2);
addObject(viruses[chosen], survivors[chosen].getX(), survivors[chosen].getY());
removeObject(survivors[chosen]);
//removeObject(twoPlayer_Button); // optional
}
}
}