Make stage "move" when main character moves

Started by
0 comments, last by Surgura 11 years, 4 months ago
I am developing a two dimension game. As for the current state, the size of the game window is equal to the size of the map. I want to change this, so it is like example Super Mario Bros, where the game have a fixed dimension, and when you move to, for example to the right, the, the stages "moves".

I should be able to achieve this with one method call, drawImage from java.awt.Graphics:
g.drawImage(stage.map,
(ourChar.posX - stage.visibleWidth / 2),
(ourChar.posY - stage.visibleHeight / 2),
stage.width,
stage.height,
null);


stage.map is a BufferedImage.
ourChar.posX and posY is the current position of the main character(example Mario in Super Mario Bros).
stage.visibleWidth and Height is the size of the JFrame.
stage.width and height is the full size of the entire stage(stage.map).

So according to the coordinates, the main character should always be in the middle of the screen(if not overlapping at the edges, which will be fixed once I get this to work). But thats no the case at all.

For those not familiar with java, drawImage paints an image according to this: drawImage (img, x, y, w, h, null)
x and y is the start coordinate of the image, w and h is the size to paint.
Advertisement
I assume the coordinates in this function represent the position on the screen in pixels. So if you want to move the viewport, what you call 'stage', you would have to substract the viewport position from the position of the object you want to draw to find the right place on the screen.

Example:
int objectPosX = 2000;
int objectPosY = 2500;
int viewPortPosX = 1500;
int viewPortPosY = 2000;
int drawPosX = objectPosX - viewPortPosX; // 500. actual position in pixels where the object should be drawn on the screen
int drawPosY = objectPosY - viewPortPosY; // 500

// put here your draw call


Forgive me, I have no experience in Java, but I hope this makes it clearer.

This topic is closed to new replies.

Advertisement