How to load chunks of 2d map segments when player reaches a certain point?

Posted by 2kan on Game Development See other posts from Game Development or by 2kan
Published on 2012-05-26T03:39:14Z Indexed on 2012/06/26 3:25 UTC
Read the original article Hit count: 146

Filed under:
|
|

In my 2d platformer (made with Java and Slick2d), random maps are made by combining different segments together and displaying them one after the other. My problem is that I can't load too many segments or the game will run out of memory, so I want to load n number of segments at a time in chunks, then load the next chunk when the player comes near the end of one.

I've attempted to do this for a couple of hours now, but I just can't get it to work at all.

This is my chunk generation function where chunkLoad is the number of segments to load and BLOCK_WIDTH is the number of blocks/tiles each segment is across. Chunk1 and map are arrays of segments.

Random r    = new Random();
for(int i=0; i<chunkLoad; i++) {
    int id  = r.nextInt(4)+2;
    chunk1[i]   = new BlockMap("res/window/map"+id+".tmx", i*BLOCK_WIDTH);
}
map = chunk1;
chunksLoaded++;

The map is then drawn on the screen like this. tmap is a TiledMap object and each block/tile is 16 pixels wide

for(int i=0; i<chunkLoad; i++) {
    map[i].tmap.render((i * BLOCK_WIDTH * 16) + (cameraX), 0);
}

I can successfully load new chunks, but I can't display them in the correct position, nor the hitboxes.

Any suggestions? Thanks.

© Game Development or respective owner

Related posts about java

Related posts about slick