I've seen many questions and answers regarding how to draw tiled maps but I can't really get my head around it. Many answers suggest either loading the visible part of the map, or loading and unloading chunks of the map. I've decided the best option would be to load chunks, but I'm slightly confused as to how this would be implemented.

Currently I'm loading the full map to a 2D array of buffered images, then drawing it every time repaint is called. Q1: If I were to load chunks of the map, would I load the map as a whole then draw the necessary chunk(s), or load & unload the chunks as the player moves along, and if so, how?

My second question regards the camera. I want the player to be in the centre of the X axis and the camera to follow it. I've thought of drawing everything in relation to the map and calculating the position of the camera in relation to the players coordinates on the map. So, to calculate the camera's X position I understand that I should use cameraX = playerX - (canvasWidth/2), but how should I calculate the Y position? I want the camera to only move up when the player reaches cameraHeight/2 but to move down when the player reaches 3/4(cameraHeight). Q2: Should I check for this in the same way I check for collision, and move the camera relative to the movement of the player until the player stops moving, or am I thinking about it in the wrong way?


Q1: Would depend on your hardware limitations (usually mainly memory size and the speed of loading new chunks from disk) and your game, but for larger worlds, the latter one (un/loading on the way) will usually be more suitable. Note that if using this option, you will often also want to pre-load some chunks (yet-not-visible) around the borders of the viewport to avoid stuttering when the players moves and you will need to do this in a separate thread (otherwise the game will stop whenever you load a new chunk).

Again, this might be just an over-kill for a simple 2D side-scroller, so you will probably want to consider whether it wouldn't be feasible to go with the first solution.

Q2: Don't know if I actually understood what you were asking about, but I would change the Y coordinate of the camera only when

  • player moves down & is at 3/4(cameraHeight)
  • player moves up & is at cameraHeight/2 (and both relatively to his own movement). Can't see any reason why complicate this.
  • \$\begingroup\$ Thanks. I realised Q2 was a pretty stupid question. I'd already answered it myself. Because of your insight I've decided to go with pre-loading the map and drawing only the visible area instead of loading chunks. \$\endgroup\$ – Troubleshoot Oct 20 '13 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.