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

2015/1/13

Keep Integers or Booleans When A New World Is Created

Space0fAids Space0fAids

2015/1/13

#
Hi. You may remember me from such topics as 'Help, I don't know how to make gravity' or 'Help. jumping does not work.' Today I am asking about the title. In my game, I have it set up so you go from one screen (a world) to the other (a different world) by hitting a certain part in the boundary of the border of the world. And, using this, you can also go back a screen, so you can backtrack and such. It's meant to be an exploration platformer I guess. My problem is, I want to have there be a certain amount of coins you need to collect before you unlock the area that leads to the end of the game. To do this, I want to be able to track if a player has gotten a coin, and if they have, do not spawn that coin if they re-enter that screen. But I do not know how to track that. I've tried using boolean's in a superclass, with all the other worlds as subclasses, but that didn't seem to work. I think it perhaps will have something to do with the score class, but I haven't been able to wrap my head around that. TL;DR How do I keep track of booleans when new worlds are created. I can upload a scenario at request, can't ATM.
danpost danpost

2015/1/13

#
You can return to a previous world as long as you save a reference to it in the new world. The world will be just as you left it. In new world (I will call it 'NextWorld' class):
// instance object field
private World lastWorld;
// added methods
public void setLastWorld(World world)
{
    lastWorld = world;
}

public void returnToLastWorld()
{
    Greenfoot.setWorld(lastWorld);
}
In old world:
// instance object field
private World nextWorld;
// added method
public void gotoNextWorld()
{
    if (nextWorld == null)
    {
        nextWorld = new NextWorld();
        nextWorld.setLastWorld(this);
    }
    Greenfoot.setWorld(nextWorld);
}
Space0fAids Space0fAids

2015/1/15

#
Thank you for all the help, and if you don't have time to respond to this I understand.
danpost wrote...
You can return to a previous world as long as you save a reference to it in the new world. The world will be just as you left it.
Ok, that's alright, but that will only storage one previous world, right? Here is my game, and I think if you see it you will know my issue; http://www.greenfoot.org/scenarios/13134 Basically, anywhere where there is a gap in the walls, I want you to be able to go through there and get to 'screen' or world above it. Here is a spreadsheet I made to represent the intended 'screens.' The problem is that, since there are multiple ways to leave and enter the same screen, saving only the previous world wouldn't work. If I have 1 different 'private World lastWorld' or 'private World nextWorld' for each screen, do you think that would work? And, for future reference, /is/ there a way to store booleans and integers when new worlds are created? I know using this system that would be unnecessary, but I'm still curious. Thanks a lot, and again, if you can't reply, don't worry.
danpost danpost

2015/1/16

#
Maybe you should look at my Super Level Support Class scenario. It would facilitate in both retaining field values between worlds as well as changing from world to world. Although that scenario creates new worlds instead of returning to previous world, it can still be used. Create a static map (or array of levels) with static fields to track the world currently in and add a static method to change worlds using changes in location within the array:
// Level world class fields
public static World[][] map;
public static int levelX, levelY;
// in constructor with no parameters
map = new World[8][4];
map[1][1] = new ScreenLLLU();
map[2][1] = new ScreenLLU();
map[4][1] = new ScreenU();
map[5][1] = new ScreenRU();
map[6][1] = new ScreenRRU();
map[2][1] = new ScreenLLL();
map[2][2] = new ScreenLL();
map[3][2] = new ScreenL();
map[4][2] = new ScreenMain();
map[5][2] = new ScreenR();
map[6][2] = new ScreenRR();
map[2][3] = new ScreenLLD();
Greenfoot.setWorld(map[4][2]);
// with method
public static void changeScreen(int xOffset, int yOffset)
{
    xOffset = (int)Math.signum(xOffset);
    yOffset = (int)Math.signum(yOffset);
    if map[levelY+yOffset][levelX+xOffset] == null) return;
    levelX += xOffset;
    levelY += yOffset;
    Greenfoot.setWorld(map[levelY][levelX]);
}
Something like that.
You need to login to post a reply.