Wednesday, November 4, 2015

java - Stop map from scrolling but let player still move?


I have a basic method of scrolling around on a map (moving the map instead of the player), but at when the player gets to a certain proximity to the edge, how do you stop the map from scrolling, but still allow the player to move around until it is away from that proximity? I'm not looking for any code. Just a suggestion so that I can implement it myself. I can see it visually (creating 4 boxed intersecting boundaries for the player to enter), but not sure how to come about stopping and resuming the scrolling of the map.



Answer



Define your viewport (position and size of the viewport). If you have this, you can define your borders. When your viewport position defines the upper left corner of your screen its pretty easy to restrict the screen position.


First of all you set the screen position to be centered on the player:


viewport.x = player.x - viewport.width / 2
viewport.y = player.y - viewport.height / 2


Then you have to make sure, that the viewport doesn't goes over the map.


if(viewport.x < 0) viewport.x = 0
if(viewport.y < 0) viewport.y = 0
if(viewport.x > map.width - viewport.width) viewport.x = map.width - viewport.width
if(viewport.y > map.height - viewport.height) viewport.y = map.height - viewport.height

Basically this should be what you are looking for... You have to keep some things in mind, for example you can leave out the upper bounds check, if the screen size in that dimension is bigger than the map size ( viewport.width > map.width ). Also, if you have your map width in tiles instead of pixel, you would have to multiply the map sizes with the tile size.


If your screen position marks the middle of the screen, you would need to change some of the calculations a bit, but the basic idea is the same.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...