You placed the code in the world constructor -- not in the world 'act' method. Currently, and by default, the World 'act' method is empty (void of any executable code). If you look at the World class API documentation, you will see that it is indeed present, however (just as in the Actor class). The class code should be as follows:
import greenfoot.*;
public class Background extends World
{
public Background()
{
super(531, 709, 1);
// any world preparation code
}
public void act()
{
if (getObjects(Food.class).isEmpty())
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Food(), x, y);
}
}
// any other methods
}
