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

2013/10/18

Simple game over end screen

CreepingFuRbalL CreepingFuRbalL

2013/10/18

#
Would someone be able to give me a code for a simple game over end screen once the character loses all of his lives or is taken away from the world
Gevater_Tod4711 Gevater_Tod4711

2013/10/18

#
What should this game over screen do? If you just want a game over screen you just need a new class with an image which you add to the world when your character dies. You could also remove all the objects in the world before doing this or stop the execution of the game. To stop the game you just need to call Greenfoot.stop(); To remove all the objects you can use this code:
1
2
3
4
5
if (characterDies()) {//only an example;
    World world = getWorld();
    world.removeObjects(world.getObjects(null)); //removes all the objects in the world;
    world.addObject(new GameOverScreen(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world;
}
If you also need something else just ask.
danpost danpost

2013/10/18

#
As an alternative, after removing all the actors from the world, you can set a new background image to the world (the game over screen) and call 'Greenfoot.stop();'. This avoids adding another Actor class for something that really has no actions or influential purpose in the world (as far as interacting with other actors).
CreepingFuRbalL CreepingFuRbalL

2013/10/25

#
How would i say if character is !null for it to remove all objects from the game?
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
To remove all objects in the world you can use this:
1
2
3
4
World world = getWorld();
if (world != null) {
    world.removeObjects(world.getObjects(null));
}
But I don't realy understand what you mean by if character is !null. Could you explain that?
CreepingFuRbalL CreepingFuRbalL

2013/10/25

#
1
2
3
4
5
6
7
8
9
10
Actor Rocket;
    Rocket = getOneObjectAtOffset(0,0,Rocket.class);
    if (Rocket != null)
        {
         
        
            World world = getWorld();
            world.removeObjects(world.getObjects(null));
             
        }
Ive got this
CreepingFuRbalL CreepingFuRbalL

2013/10/25

#
How do i add a game over screen to pop up after this has just happened?
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
You need to create a new class GameOver or any other name, set the image of this actor to the game over screen and add it to the world. So in line 9 you add this:
1
world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
danpost danpost

2013/10/25

#
Or, since you removed all the actors, just change the background image of the world to your game over screen:
1
2
world.setBackground("game_over.jpg");
Greenfoot.stop();
needcodehelp needcodehelp

2015/5/7

#
I tried setting up the world.setBackground("game_over.jpg"); Greenfoot.stop(); but cannot get it to work. I am a Greenfoot newbie, where do I place this in my code (in the class where my collision/stop occurs?) & do I need to create a game_over class under the World class? Thanks!
danpost danpost

2015/5/7

#
You had this:
1
2
3
4
5
6
7
8
Actor Rocket;
Rocket = getOneObjectAtOffset(0,0,Rocket.class);
if (Rocket != null)
{
    World world = getWorld();
    world.removeObjects(world.getObjects(null));
    // here
}
You should be able to get it to work if you place it at line 7 above.
needcodehelp needcodehelp

2015/5/8

#
I'm using the Greenfoot book Asteroids game as an example to try this, where do I place this in my code? checkCollision( )? private void checkCollision() { Asteroid a = (Asteroid) getOneIntersectingObject(Asteroid.class); if(a != null) { getWorld().addObject(new Explosion(), getX(), getY()); getWorld().removeObject(this); //here?? } }
needcodehelp needcodehelp

2015/5/8

#
I keep getting "java.lang.NullPointerException" and "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed" errors, so I must be placing this incorrectly in the code.
danpost danpost

2015/5/8

#
Note line 5 in my last post. It sets up a local variable to hold the world the actor is currently in. In the code you just posted, you are not doing that and are not able to access the world after you remove the actor from it. Hence, your error messages.
You need to login to post a reply.