Advertisement Jump to content
Sign in to follow this  

Make stage "move" when main character moves

This topic is 2243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
(ourChar.posX - stage.visibleWidth / 2),
(ourChar.posY - stage.visibleHeight / 2),
null); 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(

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
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.

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!