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

2016/3/16

Gameover Screen Help Please

SarahBeresford SarahBeresford

2016/3/16

#
I have copied my code exactly and I do not understand why the gameover text only appears after the game has ended (Greenfoot.stop()), I initially just get the greenfoot logo in its place. For the gameover to then appear, I have to click on Act one more time to force it to move on before it will appear. Can anyone help? This is the code from my Rocket class. Gameover is another Actor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
//This class defines a fish.  Fish live in the sea.
public class Rocket extends Transport
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    
    public void act() 
    {

            if(isAtEdge())
            {
                turn(60);                   
            }
            if(Greenfoot.isKeyDown("up"))
            {
                turn(4);
            }
           
            if(Greenfoot.isKeyDown("down"))
            {
                turn(-4);
            }
            
            Actor Cargo;
            Cargo = getOneObjectAtOffset(0, 0, Cargo.class);
            if (Cargo != null)
            {
                World world;
                world = getWorld();
                Space space = (Space)world;
                //add the counter from Space to counter here
                Counter counter = space.getCounter();
                //now you can apply the method addScore() to the counter from 
                //space to add 1 to the score
                counter.addScore();
                world.removeObject(Cargo);
            }
            
            Actor Asteroid;          
            Asteroid = getOneObjectAtOffset(0, 0, Asteroid.class);  
            GreenfootImage explosion = new GreenfootImage("explosion.png");
           
           if (Asteroid != null)
           {
                World world = getWorld();
                //Gamover is appearing only after Greenfoot.stop()...                             
                Gameover gameover = new Gameover();
                setImage(explosion);
                world.addObject(gameover, world.getWidth()/2, world.getHeight()/2);
                Greenfoot.stop();
                
            }
            else if (Asteroid == null) 
           {
                move(4);
           }
                
    }   

}

danpost danpost

2016/3/16

#
If you are having trouble with the image of the Gameover object, you should post the code for that class.
SarahBeresford SarahBeresford

2016/3/17

#
Sure thing, the code is below but i'm not sure that my Gameover class code is the problem:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Gameover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gameover extends Actor
{
    /**
     * Act - do whatever the Gameover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Game Over", 48, Color.WHITE, Color.BLACK));
    }    
}
Super_Hippo Super_Hippo

2016/3/17

#
Change line 16 to this:
public Gameover()
The act-method is only executed as long as the scenario is running. If you add an object and stop the scenario, its act-method won't be executed.
You need to login to post a reply.