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

2015/3/23

World Transition

Linkk0 Linkk0

2015/3/23

#
I am trying to make an open game world where the player can move through different screens at the top, bottom, left or right of the screen. I am able to do this for the initial world, but I don't know how I would then move to new worlds from the ones I have moved to. For example in the starting world I would move to the top of the screen which would take me to a new world, then if I went to the top of the screen in the new world I would end up in a third new world. The transitions I currently have a written within the player class, and was wondering if they would be able to be put in the world class so that each world would just have a few lines of code, rather than the player having many lines. Is this possible to create (The full game I intend to have will have a total of 25 screens) Cheers
Super_Hippo Super_Hippo

2015/3/23

#
Let's say all your worlds are organized in one class. Usually the world doesn't do much (or at least not so much different in each world), it is there to create the objects and this can be done with arrays.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Player class
 
//only execute this if the player moved
int dx=0, dy=0;
 
if (getX()<1) dx--;
else if (getX()>WorldName.x-2) dx++;
/*else*/ if (getY()<1) dy--;
else if (getY()>WorldName.y-2) dy++;
 
if (dx>0 || dy>0)
{
    if (!((WorldName)getWorld()).changeWorld(this, dx, dy)) setLocation(getX()-dx, getY()-dy);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//World class
//not really needed, but these values are easier to use than always call 'getWorld().getWidth()' - my opinion, I always use it
public static final int x=/**width of your world*/, y=/**height of your world*/;
 
private static int wx=0, wy=0; // current positions
//array for the build-up for every world, can look a bit like this:
private int[][][][] map = //wx, wy, x, y
{//first row
    {//first column
        {1,1,0,1}, //for example 1 represent trees
        {1,0,0,0},
        {0,0,0,1},
        {1,0,1,1}
    },
    {//second column
        //...
    },
    {//third column
        //...
    },
    {//fourth column
        //...
    },
    {//fifth column
        //...
    }
},
{//second row
    //and so on
 
//only needed for the beginning
public WorldName()
{
    super(x,y,1);
    Player p = new Player();
    addObject(p, 150, 150); //add it to somewhere
    wx=/*...*/; wy=/*...*/; //if it should not start from the top left corner, change the values
    createWorld();
}
 
public WorldName(final int wx, final int wy, Player p)
{
    super(x,y,1);
    this.wx = wx;
    this.wy = wy;
 
    createWorld();
    //set Player to right position
    int px=p.getX(), py=p.getY();
    if (getX()<1 || getX() > x-2) px = x-px;
    else if (getY()<1 || getY() > y-2) py = y-py;
    addObject(p, px, py);
}
 
public void createWorld()
{
    //use the array of this world, so map[wx][wy], to create the world
}
 
public boolean changeWorld(Player p, final int dx, final int dy)
{
    //I guess, these 25 screens are 5x5, so 0-4 each in the array
    if (wx+dx<0 || wx+dx>4 || wy+dy<0 || wy+dy>4) return false;
    Greenfoot.setWorld(new WorldName(wx+dx, wy+dy, p));
    return true;
}
I just wrote the code here, so there could be mistakes. And probably there will be. ;) But maybe you get an idea how it is possible to do it.
Linkk0 Linkk0

2015/3/23

#
That's amazing, thank you so much for the reply, it definitely helps to give me an idea of what needs done, I'll be putting it in the code soon and see what I can get done with it! Thanks alot!
You need to login to post a reply.