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

2019/11/18

Enemies still Spawning

Alexkr Alexkr

2019/11/18

#
The problem is that when the score reaches 50, the enemy should not respawn but still respawning.
public class avoiderWorld extends World
{
   // Game Music// 
   private GreenfootSound Megalovania;
   // Gpame Counter//
   private Counter scoreBoard;
   private int score = 0;
   //Enemies Spawn Rate//
   private int enemySpawnRate = 7;
   private int enemy2SpawnRate = 7;
   private int bossSpawnRate = 0;
   private int meteorSpawnRate = 0;
   //Enemies Speed //
   private int enemySpeed = 1;
   private int enemy2Speed = 1;
   private int bossSpeed = 1;
   private int meteorSpeed = 2;
   //Score for eliminating an enemy//
   private int enemyEliminatedScore = 1;
   private int enemy2EliminatedScore = 1;
   private int meteorEliminatedScore = 1;
   // Score neede for new level and num. of levels//
   private int newlvl = 25;
   private int levels = 1;
   public avoiderWorld()
   {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false); 
        //Plays this sound when the game starts//
        Megalovania = new GreenfootSound("sounds/megalovania.wav");
        Megalovania.playLoop();
        prepare();
   }

   private void prepare()
   {
        //Adds this classes to the screen before starting the game//
        avatar avatar = new avatar();
        addObject(avatar,304,359);
        avatar.setLocation(306,353);
        scoreBoard = new Counter("Score: ");
        addObject(scoreBoard, 70, 20);
   }
    
   public void act() 
   {
       //randomly add enemies to the world//
       if(Greenfoot.getRandomNumber(1500) < enemySpawnRate) 
       {
            //creates a new class called enemy//
            enemy e = new enemy();
            //sets the speed variable, you have to give it a value( it, double, float)
            e.setSpeed(enemySpeed);
            //Spawns the enemies between the screen ranges that you set//
            addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
            //increase points for each enemy spawned
            scoreBoard.setValue(scoreBoard.getValue() + enemyEliminatedScore);
            score += enemyEliminatedScore;
       }
        
       if(Greenfoot.getRandomNumber(1500) < enemy2SpawnRate)
       {
            enemy2 e2 = new enemy2();
            e2.setSpeed(enemy2Speed);
            addObject(e2, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
            //increase points for each enemy2 spawned
            scoreBoard.setValue(scoreBoard.getValue() + enemy2EliminatedScore);
            score += enemyEliminatedScore;
       }
       
       if(Greenfoot.getRandomNumber(1500) < bossSpawnRate)
       {   
            boss bs = new boss();
            bs.setSpeed(bossSpeed);
            addObject(bs,307,62);
            scoreBoard.setValue(scoreBoard.getValue()+1);
       }
       
       if(Greenfoot.getRandomNumber(1500) < meteorSpawnRate)
       {    
            meteor mtr = new meteor();
            mtr.setSpeed(meteorSpeed);
            addObject(mtr, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
            scoreBoard.setValue(scoreBoard.getValue() + meteorEliminatedScore);
            score += meteorEliminatedScore;
       }
         
       /*each time score is == to a multiple of 25 and is less than 100
       run if statement that increases speed of both enemies*/
       if(score == newlvl)
       {
          IncreaseLevel();
       }
       
   }
    
   public void endGame() 
   {    
       //Displays the game over screen//
       gameOverScreen go = new gameOverScreen();
       Greenfoot.setWorld(go);
       Megalovania.stop();
   }
    
   public void IncreaseLevel()
   {
       if(score == 25)
       {
           levels = 8;
       }
       
       switch(score)
       {
           //If score equals to a case, the case will execute it, and once reached the next score it will stop the actual case and start with the new one 
           case 25:
            enemySpeed = 2;
            enemy2Speed = 2;
            break;
            
           case 50:
            enemySpawnRate = 0;
            break;
            
           case 75:
            enemySpawnRate = 8;
            break;
            
           case 100:
            enemy2SpawnRate = 8;
            break;
            
           case 125:
            enemySpawnRate = 0;
            enemy2SpawnRate = 0;
            meteorSpawnRate = 10;
            break;
            
           case 150:
            enemySpawnRate = 9;            
            enemy2SpawnRate = 8;
            break;
            
            case 175: 
            enemySpawnRate = 9; 
            enemy2SpawnRate = 9;
            break;
            
           case 200: 
            enemySpawnRate = 0;
            enemy2SpawnRate = 0;
            bossSpawnRate = 1;
            break;
       }
   }
}
Super_Hippo Super_Hippo

2019/11/18

#
Try to change line 90 to the following to do what the comment says it would do:
if (score % newlvl == 0)
Keep in mind that it will only work like that as long as you never get more than one point at once. For example if two enemies are spawned at once and the score increases from 24 to 26, it was never 25 when it was checked.
danpost danpost

2019/11/18

#
It might be wise to have a level field:
private int level = 0;
Then, your check would be:
if (score >= (level+1)*25)
{
    switch (++level)
    {
        case 1:
            enemySpeed = 2;
            enemy2Speed = 2;
            break;
        case 2:
             // etc.
I am not sure what the purpose of your levels field is. It starts at 1 and jumps to 8 after one level is complete !?
Alexkr Alexkr

2019/11/22

#
I could fix it, thank you for the fast and clear response.
You need to login to post a reply.