Sign in to follow this  
P0jahn

Make stage "move" when main character moves

Recommended Posts

P0jahn    307
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:
[CODE] g.drawImage(stage.map,
(ourChar.posX - stage.visibleWidth / 2),
(ourChar.posY - stage.visibleHeight / 2),
stage.width,
stage.height,
null);[/CODE]

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.

Share this post


Link to post
Share on other sites
Surgura    219
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:
[code]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[/code]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this