Home > categories > Construction & Real Estate > Tile > Tiled map in Java Game?
Question:

Tiled map in Java Game?

I‘m trying to tile a map using swing (JPanel and JFrame) but don‘t know where to start. Ideally the map would end up looking like pokemon or 2D zelda games, where there is an invisible, underlying grid, and the person occupies one spot at a time. Does anyone know how to do this?I can get a sprite to move, but that‘s based on pixels, not on tiles. If you know how to do this, can you paste code and explain it?

Answer:

That's the most efficient way to do it. You use math to calculate positions, so the tiles are virtual. Unless you really need to draw tiles with boundaries, don't show them (like the original Warcraft game). Perform all of your calculations like movement and clicks-to-tile conversions based on pixel location. This is far more efficient that trying to do it with lots of actual Java subpanels merged together. But, note that it's a lot of work and you have to carefully work out your calculations to make sure your clicks are aligned correctly. One mistake I made with a hex map once was I used doubles and I casted them to integers too early, and the slight different (by losing percentage) threw all my calculations off slightly. The trick is to create your background image tiles so that they blend seamlessly together on all sides. That will take a LOT of effort.

Share to: