Moving Levels

Hello,

I am currently working on a top down dungeon crawler-esque game, inspired by the iPhone game “Solomon’s keep”. The problem I am currently faced with is making the background move around the player. I understand how to make the level as a String, as well as how to use objects to create the walls, floors, exits, etc. The problem I am facing is trying to get all of the objects I create to move around the player, as the player stays in the middle of the screen.

I have a move class that can move every object, however, it seems like I have to call the move method for every object created under the actionPerformed class, and since the number of objects changes with every level there doesn’t seem to be a feasible way to do that.

If anyone knows of a good tutorial that explains this, or wants to take a shot at explaining it themselves, I would greatly appreciate it :slight_smile:

Thanks in advance,
Dirnol

Unfortunately the only way I know how to manage this with the least modification would be to have two co-ordinate systems.

Imagine a window where everything inside of it is drawn and everything outside of it is not drawn. This window has a width a height and a position. Usually this position is the center of the window.

So one way would be to say "Is object x within the window (is the x position greater then the window x - 1/2 the window width and is less then the window x + 1/2 the window width) etc.

Whatever is in the window is then drawn but not to world co-ordinates … to screen co-ordinates.

Building on drakesword, you should have an xOffset and a yOffset variable and you just translate by those before you render. If you are using Java2D, that would be:


g.getTransform().translate(xOffset,yOffset);

drawBackground(g);

g.getTransform().translate(-xOffset,-yOffset);

drawPlayer(g);