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

2018/1/24

changing worlds problem

1
2
ImAndrew ImAndrew

2018/1/25

#
Vercility wrote...
ImAndrew wrote...
Vercility wrote...
You're running a loop again Your Dungeon1 constructor calls R1 which calls super( which is Dungeon1), which calls R1 etc.
Oh,I see , I will get a headache with these loops.
Super_Hippo wrote...
Try
private static World[] worldArr =
{
    new D1_R1(),
    new D1_R2(),
    new D1_R3()
};
as line 8 and remove lines 13-17.
I tried this earlier and I got an exception when I add a door object to D1_R1 World "java.lang.NoClassDefFoundError: Could not initialize class D1_R1 " I'm out of solutions.
How exactly did you do that
I used this code but my game is freezing again or the world is gray.
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 static Hero hero = new Hero();
    
    protected static World[] worldArr =
    {
         new D1_R1(),
         new D1_R2(),
         new D1_R3()
     };
    
    public Dungeon1(boolean _var)
    {
        super(WIDTH, HEIGHT, CELL_SIZE);
    }
}
danpost wrote...
Maybe you can get some pointers from my Door World Entry Demo scenario.
I looked and your demo (it was very confusing for me) and used this code but I get an exception again in Door class method "java.lang.NullPointerException" and also I don't get your constructor Boolean parameter.
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 static Hero hero = new Hero();
    
    protected static World[] worldArr = new World[15];

    public Dungeon1(boolean _var)
    {
        super(WIDTH, HEIGHT, CELL_SIZE);
        if(!_var)
        {
            worldArr[0] = new D1_R1();
            worldArr[1] = new D1_R2();
            worldArr[2] = new D1_R3();
        }
    }
}
public class Door extends Actor
{
    World nextRoom;
    
    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);
            }
        }
    }
}
Also, like it wasn't enough that I keep getting errors now I can't create doors when I right click on the class. screenshot
danpost danpost

2018/1/25

#
The boolean parameter is there to avoid all worlds from being created multiple times. Code all new Dungeon1 object as 'new Dungeon1(true)' -- and similar for its subclasses (which should call 'super(true);'). Change 'protected' to 'public at line 8 in the Dungeon1 class and add the following field:
public static int currentWorld ;
The value of this field, as well as the 'hero' field should be set in the 'if (!_var)' block; so, change line 6 to:
protected static Hero hero;
and insert the following at line 15:
hero = new Hero();
currentWorld = 0;
The last line is necessary to make sure its value is reset when the game is reset or started over. Next, use an int parameter in the Door class constructor instead of a World parameter. The value passed should be the adjustment in the world value held by 'currentWorld'. To proceed to the world, you will use something like the following:
Greenfoot.setWorld(Dungeon1.worldArr[Dungeon1.currentWorld+adjustment]);
You do not have to pass the value from the 'act' method to the 'nextRoom' method (the field and methods are all in the same class).
ImAndrew ImAndrew

2018/1/25

#
danpost wrote...
The boolean parameter is there to avoid all worlds from being created multiple times. Code all new Dungeon1 object as 'new Dungeon1(true)' -- and similar for its subclasses (which should call 'super(true);'). Change 'protected' to 'public at line 8 in the Dungeon1 class and add the following field:
public static int currentWorld ;
The value of this field, as well as the 'hero' field should be set in the 'if (!_var)' block; so, change line 6 to:
protected static Hero hero;
and insert the following at line 15:
hero = new Hero();
currentWorld = 0;
The last line is necessary to make sure its value is reset when the game is reset or started over. Next, use an int parameter in the Door class constructor instead of a World parameter. The value passed should be the adjustment in the world value held by 'currentWorld'. To proceed to the world, you will use something like the following:
Greenfoot.setWorld(Dungeon1.worldArr[Dungeon1.currentWorld+adjustment]);
You do not have to pass the value from the 'act' method to the 'nextRoom' method (the field and methods are all in the same class).
So, after all, I created a really mess and almost ruined the game.I observed that this method of thinking is too complicated for me (this is my first game and big project) so I created a WorldManager class and it works almost well(and it's more simple xD) but the problem is that it doesn't save the rooms.I want to have my rooms cleared after I go back.
import greenfoot.*;

public class WorldManager  
{
    public static World[] worldArr = new World[15];
    
    
    static
    {
        worldArr[0] = new D1_R1();
        worldArr[1] = new D1_R2();
        worldArr[2] = new D1_R3();
        //add more rooms
    }
}
danpost danpost

2018/1/25

#
Can you upload what you have with the source provided? It would make helping out a whole lot easier.
You need to login to post a reply.
1
2