Jump to content

  • Log In with Google      Sign In   
  • Create Account

Make stage "move" when main character moves


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 P0jahn   Members   -  Reputation: 270

Like
0Likes
Like

Posted 01 December 2012 - 05:03 AM

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.

Sponsor:

#2 Aart   Members   -  Reputation: 219

Like
0Likes
Like

Posted 01 December 2012 - 07:32 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS