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:
And here's the code for the player:
Help is much appreciated. Thanks to anyone who replies!
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));
}
}
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();
}
}
}

