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

2013/11/20

World background keeps changing back

mwishum mwishum

2013/11/20

#
My main character "saves" the current world he is by getting all the objects in it and storing it in an List of Actors. Then when returning to that room a different constructor for that world is called passing it the list. My problem is that when you return to a room that has its objects restored the background image lingers from the room you just left. Here is that second constructor (the part bellow is actually inherited and called using super):
public SuperWorld(Player newP, int x, int y, List<Actor> fromSave)
    {    
        super(xS, yS, 1);
        for (Actor A : fromSave)
        {
            //...//
            addObject(A,A.getX(),A.getY());
        }
        addObject (newP, x, y);
    }
I tried setting the background in the world as so:
//Player is the main character
public Room1(Player  newP, int x, int y, List<Actor> fromSave)
    { 
        super(newP, x, y, fromSave); //code above
        setBackground("roombg3_fullsize.png"); 
    }
But that doesn't work. I have looked at Actor objects and they seem to store the background image of the world they were in, but I'm not sure that has anything to do with it. Any ideas? Thanks in advance for any help!
davmac davmac

2013/11/20

#
I guess that the "background" you are seeing is actually an actor, and not the world background at all. You can easily check this by pausing the scenario and seeing if you can move the "background" around for instance.
danpost danpost

2013/11/20

#
You may want to download and look at the code of my Level Changing Demo to see how to accomplish this. I have two other demos that use portal-doors to facilitate changing 'rooms'. One uses a static array for the rooms in the world superclass (as I suggest above); the other links the 'to' and 'from' doors of the different worlds which eliminates the need for the static array.
mwishum mwishum

2013/11/20

#
Thank you both for replies. @davmac, I originally thought that was going to be a problem but in my testing only objects that I wanted to be "copied" over were contained in that list, no Worlds. And upon inspecting the world, it's imageFileName is the wrong image, the image from the last room. Each Room also stores it's number... that's where discovered my mistake (at the bottom). @danpost, this game already utilizes doors to get from room to room, but there are a lot of objects that the Player can destroy that need to "saved" so when they return they don't have to start all over. The doors work by each having a unique ID & Location combo that corresponds to a place in an array (doorFrom). The Player, upon nearing a door it searches through that array for the matching entry and gets it's index and using that looks in another array (doorTo) for another ID & Location combo that is then used to determine the room and spot to spawn the player. Your examples might have been useful to have seen before I came up with this solution, but the non-linear routing allowed in design is useful for my purposes (I also don't completely comprehend your examples from my short look). Anyways, here was my stupid copy-paste downfall:
case 2: 
                if (!roomSaved[2]) Greenfoot.setWorld(new Room2(z,newPointX,newPointY, health));
                else Greenfoot.setWorld(new Room5(z,newPointX,newPointY,room2)); //Should be Room2 not Room5 
                break;
So, whenever you re-visit a room it always spawns Room5, but the with correct List of items. Not quite sure how this worked... Anyways, thank you for your help!
danpost danpost

2013/11/20

#
In all my demo scenarios mentioned above, the rooms (worlds) will always be returned to in the state they were left in. Any objects removed will still be removed; any objects moved will still be at the last location they were moved to; etc. The only object that will need replaced is your main character which was removed and inserted into the different world. It will need put back in the world at whatever location you deem appropriate (possibly depending on from where and from which world it is now leaving).
mwishum mwishum

2013/11/20

#
Yes, do see that the doors are kept in the same location after leaving the room, as well as the lines drawn. But I can't quite figure out how... I can't quite figure out how the two dimensional arrays and the
rooms[1][0] = new Room302();
work.
danpost danpost

2013/11/20

#
As long as a reference is kept to any already created world, you can return to it by using the reference (without using the 'new' keyword, which would create a world in its initial state).
You need to login to post a reply.