I have a door class which should send the player to another room(World) when he defeated all the enemies. The code is working well until I try to add a door which should get me back in a room I already was.When I do that Greenfoot freezes and when I restart the game it says that I would have an infinite loop in a world constructor.
I have a Dungeon super class which holds a static hero object for all the rooms.
public class Dungeon1 extends World
{
protected final static int WIDTH = 800;
protected final static int HEIGHT = 600;
protected final static int CELL_SIZE = 1;
protected int enemiesRemaining;
protected static Hero hero;
public Dungeon1(int _width, int _height, int _cellsize)
{
super(_width, _height, _cellsize);
}
public class Door extends Actor
{
World nextRoom;
public Door(World _nextRoom)
{
nextRoom = _nextRoom;
}
public void act()
{
nextRoom(nextRoom);
}
public void nextRoom(World nextWorld)
{
if(getWorld().getObjects(Enemy.class).isEmpty())
{
Hero hero = (Hero)getOneIntersectingObject(Hero.class);
if(hero != null)
{
nextWorld.addObject(hero,100,150);
Greenfoot.setWorld(nextWorld);
}
}
}
}


