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

2019/11/5

The game starts lagging

Alexkr Alexkr

2019/11/5

#
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(); } }
danpost danpost

2019/11/5

#
I noticed you are using an unbounded world ('false' used in super constructor call). Make sure all actors that leave view and are not needed anymore are removed from the world.
Alexkr Alexkr

2019/11/5

#
Yes, I checked all the classes and if the enemies reach the edges they are removed. The only things left at that moment are the boss spaceship and the avatar.
Super_Hippo Super_Hippo

2019/11/6

#
You are adding a “boss” every act cycle. You check if the score is greater than 25. If it is greater than 25, it will always be greater than 25 and you will add more and more bosses.
Alexkr Alexkr

2019/11/8

#
I fixed it, thank you.
You need to login to post a reply.