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

2015/8/31

Can't get the 'Game Over' screen to work?

rolando09 rolando09

2015/8/31

#
I just started Greenfoot today so I'm a real newbie to this stuff. I wanted my game to have a Game Over screen when the player is touched by the enemy but it just doesn't work. I have no scoring in the game too. Here's the code for My World:
import greenfoot.*;
import java.util.List;
import java.awt.Color;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    public MyWorld()
    {    
        // Create a new world with 20x20 cells with a cell size of 10x10 pixels.
        super(800,600,1);
    }
    
    public void gameOver() 
    {
        
        removeAllActors();
        addObject(new GameOver(), getWidth()/2, getHeight()/2);
        Greenfoot.stop();
        
    }
    
    public void removeAllActors() 
    {
        
        removeObjects(getObjects(Actor.class));
        
    }
}
And here's the code for the player:
import greenfoot.*;

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

    private int speed = 0;

    public void act() 
    {
        if (Greenfoot.isKeyDown("up"))
        {
            speed++;
            if (speed>7)
            {
                speed = 7;
            }
        }
        else
        {
            speed--;
            if (speed<0)
            {
                speed = 0;
            }
        }

        move(speed);

        if (Greenfoot.isKeyDown("left"))
        {
            turn(-3);
        }

        if (Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }

    }

    public void checkEnemy() 
    {

        if(touchingEnemy()) 
        {

            MyWorld w = (MyWorld)(getWorld());
            w.gameOver();

        }
    }

    public boolean touchingEnemy()
    {
        Actor enemy = getOneIntersectingObject(Enemy.class);
        return (enemy != null);
    }

    public void isTouchingEnemy()
    {
        if (getWorld() == null)
        {
            return;
        }

        Actor player;
        player = getOneObjectAtOffset(0,0, Player.class);
        if (player != null)
        {
            World world = getWorld();
            world.removeObjects(world.getObjects(null));
            Greenfoot.stop();
        }
    }
}
Help is much appreciated. Thanks to anyone who replies!
Super_Hippo Super_Hippo

2015/8/31

#
Insert the following line at the end of the act method (line 48):
checkEnemy();
As it is now, this method is never executed, so it doesn't check if the player is intersecting an enemy.
You need to login to post a reply.