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

2017/11/30

Help with Game Over Screen

HelpMe123 HelpMe123

2017/11/30

#
I have this so far, but it doesn't seem to run the gameOver() class correctly. The gameOver() class is last in my act method, just after kill(), if it matters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void kill() { //detects when the gun is close to an asteroid and shoots removes them both
        List<Enemy1> enemies = new ArrayList<Enemy1>();
        enemies = this.getObjectsInRange(25, Enemy1.class);            
        for (Enemy1 e : enemies) {
            if (enemies.size()>0) {
                dead = true;
 
                getWorld().removeObject(enemies.get(0));
                getWorld().removeObject(this);
            }
        }
         
             
    }
     
    public void gameOver() {
           if (dead == true){
               getWorld().removeObjects(getWorld().getObjects(null));
               getWorld().addObject(new GameOver(), 300, 200);
               Greenfoot.stop();
        }
    }
Super_Hippo Super_Hippo

2017/11/30

#
What you try to do: - if enemy (asteroid)? is in range --- remove the enemy / all enemies --- remove the gun --- then remove all objects in the world and add a GameOver object --- stop the program What happens: You remove the object from the world (line 9), then the gameOver method (not class!) executes, getWorld returns null and you can't remove/add objects to null, so you will receive your error. What you can do: Simply remove all object from the world if an enemy1 is in range and add the GameOver object. No need for the dead variable or removing enemy and 'this' before removing all objects anyway.
HelpMe123 HelpMe123

2017/11/30

#
Thanks for the response! Quick question, how do I remove every object in my game? I thought the code i posted above would work, but it won't destroy the randomly generated asteroid...
Super_Hippo Super_Hippo

2017/11/30

#
Line 18 alone removes all objects from the world.
You need to login to post a reply.