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

2016/11/24

Need help saving world instances when moving to new worlds

CSnoob CSnoob

2016/11/24

#
Like the post says, I'm having issues saving the world instances. My game is a survival style game with the ability to build objects (such as houses and what not). There are a total of 5 worlds that make up my island. while my main character has the ability to move between frames, but I haven't quite figured out how to save the worlds instance to bring it back when the actor returns to said world. Below is the code for my actors method to move between worlds:
 public void moveFrame() //moves frames and passes player instance **save world instance???(nope...)**
    {
        if (getWorld() instanceof CoastalFrame)
        {
            if (getX() <= 10)
            {

                HomeFrame homeFrame = new HomeFrame();

                homeFrame.passObjectsFromCoast(this);
                Greenfoot.setWorld(homeFrame);

            }
        }
        if (getWorld() instanceof HomeFrame)
        {
            if (getX() <= 10)
            {

                MountainFrame mountainFrame = new MountainFrame();

                mountainFrame.passObjectsFromHome(this);
                Greenfoot.setWorld(mountainFrame);

            }
            else if (getY() <= 10)
            {

                ForestFrameNorth forestFrameNorth = new ForestFrameNorth();

                forestFrameNorth.passObjectsFromHome(this);
                Greenfoot.setWorld(forestFrameNorth);

            }
            else if (getY() >= 590)
            {

                ForestFrameSouth forestFrameSouth = new ForestFrameSouth();

                forestFrameSouth.passObjectsFromHome(this);
                Greenfoot.setWorld(forestFrameSouth);

            }
            else if (getX() >=790)
            {

                CoastalFrame coastalFrame = new CoastalFrame();

                coastalFrame.passObjectsFromeHome(this);
                Greenfoot.setWorld(coastalFrame);

            }
        }
        if (getWorld() instanceof MountainFrame)
        {

            if (getX() >= 790) //not needed
            {

                HomeFrame homeFrame = new HomeFrame();

                homeFrame.passObjectsFromMountain(this);
                Greenfoot.setWorld(homeFrame);

            }
        }
        if (getWorld() instanceof ForestFrameSouth)//same
        {
            if (getY() <= 10)
            {
                HomeFrame homeFrame = new HomeFrame();

                homeFrame.passObjectsFromSouth(this);
                Greenfoot.setWorld(homeFrame);
            }
        }
        if (getWorld() instanceof ForestFrameNorth)//same
        {
            if (getY() >= 590)
            {
                HomeFrame homeFrame = new HomeFrame();

                homeFrame.passObjectsFromNorth(this);
                Greenfoot.setWorld(homeFrame);
            }
        }
    }
any insight would be appreciated.
Super_Hippo Super_Hippo

2016/11/24

#
You always create a 'new' world. You could save a reference to all the worlds and then, you could move back to it. Maybe it is clever to save the references in this character class.
private HomeFrame homeFrame; //starting world (if this isn't the starting world, use 'new homeFrame()' here and change the method below)
private CoastalFrame coastalFrame = new CoastalFrame();
private MountainFrame mountainFrame = new MountainFrame();
private ForestFrameNorth forestFrameNorth = new ForestFrameNorth();
private ForestFrameSouth forestFrameSouth = new ForestFrameSouth();

protected void addedToWorld(World w)
{
    homeFrame = (HomeFrame) w;
}
Whenever you change the world, you don't need to create a new one then:
Greenfoot.setWorld(mountainFrame);
CSnoob CSnoob

2016/11/24

#
Thanks for the reply Hippo, but I figured it out last night. Didnt realize it would take so long for my first post to actually get approved. I didn't think to use that protected void function, instead I just passed the starting world to my Survivor.
//in survivor
private CoastalFrame coastalFrame;

public Survivor(CoastalFrame coastalFrame)
    {
        Greenfoot.setSpeed(40);
        this.coastalFrame = coastalFrame;
    }
//in coastalFrame (starting world)
private CoastalFrame coastalFrame = this;

public void setUpCoast()
    {

        Survivor survivor = new Survivor(coastalFrame);
        addObject (new Survivor(coastalFrame), 300, 300);
    }
Super_Hippo Super_Hippo

2016/11/24

#
Alright. Btw, line 15 doesn't do anything since you are doing the same thing in the next line again. And if you change the 'coastalFrame' in line 16 to 'this', you can also remove line 10 ;)
You need to login to post a reply.