Hey, I'm currently coding a little game where I want random actors to spawn with a delay that gets smaller with time, and I cant get it to work
import greenfoot.*;
public class GameBackground extends World
{
private int score;
private int starttime;
private boolean x;
public void act()
{
starttimer();
lessDelay();
checkForSpawning();
//TestOhneDelay(); //später durch starttimer wieder ersetzen
addScore();
GameOver();
}
public GameBackground()
{
super(1500, 800, 1);
setBackground("dark-wooden-background-high-resolution-11231.jpg");
prepare();
}
public void starttimer() // 5 Sekunden nach geklicktem Startbutton starten
{
starttime++;
int SpawnDelay = starttime;
if(starttime == 70 && starttime < 140)
{
showText("3", 800, 480);
}
if(starttime == 140 && starttime < 210)
{
showText("2", 800, 480);
}
if(starttime == 210 && starttime < 280)
{
showText("1", 800, 480);
}
if(starttime == 280 && starttime < 350)
{
showText("GO!!!", 800, 480);
}
if(starttime == 350)
{
showText("", 800, 480);
}
if(starttime == 370)
{}
}
public void randomSpawner() // alle ca. 70 ticks/acts soll random ein Käfer iwo am Rand (oben/unten) gespawnt werden
{
}
public void addScore()
{
if(Menü.leben > 0 && starttime >= 350)
{
score++;
}
showText("Score: " + score, 65, 10);
}
public void TestOhneDelay()
{
if(x == false)
{
addObject(new act2(),Greenfoot.getRandomNumber(1500)+ 1, 899); //Nur zum Test ohne verzögerten Start
x = true;
}
}
public void GameOver()
{
if(Menü.leben == 0)
{
Greenfoot.setWorld(new GameOver());
}
}
private int delayDelay = 180;
private int delay = 180;
private int spawnTimer;
private void checkForSpawning() // call from act method
{
if(starttime >= 370)
{
spawnTimer = (spawnTimer+1)%delay; // repeat every 3 seconds (about)
if (spawnTimer == 0) // at each timer reset
{
addObject(new act2(),Greenfoot.getRandomNumber(1500)+ 1, 899);
}
}
}
private void lessDelay() // call from act method
{
if(starttime >= 500)
{
spawnTimer = (spawnTimer+1)%delayDelay; // repeat every 3 seconds (about)
if (spawnTimer == 0) // at each timer reset
{
delay = delay/900;
}
}
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
heart heart = new heart();
addObject(heart,1351,30);
heart2 heart2 = new heart2();
addObject(heart2,1409,37);
heart2.setLocation(1404,29);
heart3 heart3 = new heart3();
addObject(heart3,1465,35);
heart3.setLocation(1458,29);
}
}
