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

2017/3/16

how to end a game when an actor reaches the bottom of the background?

fxrrxr fxrrxr

2017/3/16

#
hello how can i end a game when the player loses???? do i set a new background with the words "game over" or is there another way to do it?
danpost danpost

2017/3/16

#
fxrrxr wrote...
hello how can i end a game when the player loses???? do i set a new background with the words "game over" or is there another way to do it?
There are several ways. The first option you have is whether to stay with the same world or display the words in a new world. The second option is whether to display the words in the background image of the world or in the image of an Actor object that is added into the world. You need to decide what is best (or easiest) in your particular case.
fxrrxr fxrrxr

2017/3/16

#
do you have an example code if i put it in the same world? i'm sorry im very new here. :(
danpost danpost

2017/3/16

#
You could remove all objects and change the background like so:
// in World subclass
public void gameOver()
{
    remeoveObects(getObjects(Actor.class)); // remove all actors from world
    GreenfootImage bg = getBackground(); // get reference to background image
    bg.setColor(Color.BLACK); // set drawing color to black for 'bg' image
    bg.fill(); // fills 'bg' image with the currently set drawing color
    GreenfootImage txtImg = new GreenfootImage("GAME\nOVER", 80, Color.WHITE, Color.BLACK); // create "GAME OVER" text image
    bg.drawImage(txtImg, (bg.getWidth()-txtImg.getWidth())/2, (bg.getHeight()-txtImg.getHeight())/2); // draw text centered onto 'bg' image
    Greenfoot.stop(); // stop the scenario
}
fxrrxr fxrrxr

2017/3/16

#
THANK YOU SO MUCH
You need to login to post a reply.