The game is about evading enemies and beating levels, but when I get into level 2 and it spawns a spaceship it starts lagging.
Here is the code:
public class avoiderWorld extends World
{
private GreenfootSound Megalovania;
private Counter scoreBoard;
private int enemySpawnRate = 20;
private int enemy2SpawnRate = 20;
private int enemySpeed = 1;
private int enemy2Speed = 1;
private int Level2 = 25;
private int drawedImage;
public avoiderWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1, false);
Megalovania = new GreenfootSound("sounds/megalovania.wav");
Megalovania.playLoop();
prepare();
}
private void prepare()
{
avatar avatar = new avatar();
addObject(avatar,304,359);
avatar.setLocation(306,353);
scoreBoard = new Counter("Score: ");
addObject(scoreBoard, 70, 20);
}
private void increaseLevel()
{
int score = scoreBoard.getValue();
if( score > Level2)
{
enemySpawnRate = 0;
enemy2SpawnRate = 0;
boss boss = new boss();
addObject(boss,302,100);
}
}
public void act()
{
//randomly add enemies to the world
if(Greenfoot.getRandomNumber(1500) < enemySpawnRate)
{
enemy e = new enemy();
e.setSpeed(enemySpeed);
addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
//increase points for each enemy spawned
scoreBoard.setValue(scoreBoard.getValue() + 1);
}
if(Greenfoot.getRandomNumber(1500) < enemy2SpawnRate)
{
enemy2 e2 = new enemy2();
e2.setSpeed(enemy2Speed);
addObject(e2, Greenfoot.getRandomNumber(getHeight()-20)+10, -30);
//increase points for each enemy
scoreBoard.setValue(scoreBoard.getValue() +1);
}
increaseLevel();
}
public void endGame()
{
gameOverScreen go = new gameOverScreen();
Greenfoot.setWorld(go);
Megalovania.stop();
}
}