Using the tile engine

Oct 18, 2010 at 1:24 AM
Edited Oct 18, 2010 at 1:27 AM

This is a great program and both the tile engine and map editor will save me loads of time. However, I can't figure out how to scroll the map. If I do viewport.(X or Y) + (some amount) I seem to have two problems.

1) It seems to change what part of the map is displayed but the tiles remain in the same place. For example I have a sprite fall until it hits the floor. I do this by increasing the Y value of it's position unless the current tile under it has a tileIndex of 1 (the tile index of the grass tile from the example tile sheet). This works fine unless I move the map in which case it stops at the same x,y coordinates it would have before the map was moved.

2)I can't seem to figure out how to make it stop scrolling at the bounds of the map.

Any help would be appreciated. If I haven't been clear enough I can provide a more in depth explanation and code.

Edit: I've also tried using viewport.Location.(X or Y) which has the same effect as just viewport.(X or Y)

Nov 11, 2010 at 10:36 AM

I think you need to factor the viewport coordinates into your sprite drawing code. You can think of the viewport as a camera, or a rectangle that your are sliding across the map to indicate what you want to show on screen.

You should keep your coordinates as they are when performing collision logic (world coordinates). However when you draw the sprite you need to subtract the viewport's X and Y values to convert from world coordinates to view coordinates.

In other words:


drawPosition.X = playerPosition.X - viewPort.X;

drawPosition.Y = playerPosition.Y - viewPort.Y;


As an example, suppose your character is initially at position 100, 50 on your map and the viewport is initially set at position (0, 0). If you move your viewport 10 pixels to the right to position (10, 0), your sprite drawing coordinates become (90, 50). That is, the player should be drawn 10 pixels to the left of the original position to reflect the fact that you panned the viewport 10 pixels to the right.

I hope that helps.

I am planning to eventually implement a moving player within the downloadable xTile demo. If you could provide me with a (legal) sprite sheet I could give it a shot in the immediate future :)

Nov 27, 2010 at 11:14 PM

Ahh thanks. That did the trick, can't believe I missed that.

If you download the tutorial it's got some sprite sheets that are free to use for non-commercial purposes. They're 64x64 so a little big for the background textures you use for your demo but they'll do the trick.

Nov 28, 2010 at 3:27 PM

Glad to hear you got it working :)

Thanks for the offer to use your sprites! I'll keep that in mind when I get to update the demo.