0
\$\begingroup\$

Ok, so I have made a .tmx file for my first level, but I am unsure how I would load it into my game. Currently I am iterating through an internal array to set the blocks, but I need to know how to load the .tmx file into the code.

Here is my World.java:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;

class World {
    Image ground;
    Block[] blocks = new Block[46];
    int map[][] = {
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        };
    World() {
        int enc = -1;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if (map[i][j] == 1) {
                    enc++;
                    blocks[enc] = new Block(j * 32, i * 32, 32, 32, 1);
                } else if (map[i][j] == 2) {
                    enc++;
                    blocks[enc] = new Block(j * 32, i * 32, 32, 32, 2);
                }
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ So you want to stop using that int array and start loading an external file? \$\endgroup\$ – lozzajp Jan 19 '17 at 13:17
  • \$\begingroup\$ If you do int map[][] = new int[25][25] it'll automatically get populated with 0s \$\endgroup\$ – Bálint Jan 19 '17 at 18:16
  • \$\begingroup\$ @Bálint Except, the array does not only contain 0s. :-) \$\endgroup\$ – Thorbjørn Lindeijer Jan 19 '17 at 21:14
  • \$\begingroup\$ yes, lozzajp, that is what im trying to do \$\endgroup\$ – javaprogammer23 Jan 20 '17 at 2:32
1
\$\begingroup\$

The easiest way to parse TMX files with Java is to use the libtiled library. It is licensed under 2-clause BSD, which means you can use it for anything as long as you give credit.

But if you would like to implement it yourself: The .tmx format is an XML-based format. That means you can parse the files with an XML parser. There are several options for this in Java. Which one you use depends on personal taste.

Depending on how you saved the TMX file, the actual layer data will be either encoded in a stream of ascii-decimal encoded integers separated by commas (aka. CSV) or a stream of int's encoded with base64. The layer data can also be compressed with gzip or zlib. You might want to disable that in the Tiled settings for now because it just makes things more complicated. When your finished game turns out too big, you can achieve even better compression by putting all of your assets in a zip archive.

You might also want to take a look at the other export formats Tiled supports. In one project I found it much easier to use the JSON format. That was because the project was using JSON for other things so I already had a good JSON library and some useful utility functions.

\$\endgroup\$
0
\$\begingroup\$

I'd suggest taking a look at the following classes from the LibGDX framework- TiledMap, TMXMapLoader and MapRenderer.

I found a decent tutorial here with example code for reading in and rendering a tile map from a TMX file that makes pretty short work of the problem. But for the sake of providing a complete answer, what you want to do is

  1. Create an Orthographic Camera- camera

  2. Load your TMX file into an empty TiledMap object- tileMap, using TMXMapLoader.load(filename.TMX)

  3. Create a OrthogonalTiledMapRenderer object- mapRenderer, passing tileMap as a parameter to the constructor

  4. Set the view of your renderer to the camera using
    mapRenderer.SetView(camera)

  5. Render the tile map to the screen with mapRenderer.Render()

When stuck with solving a problem that you know would have been solved many times over by programmers before you, my advice would be to turn to well known libraries for the solution- it will save you a lot of time and avoid a lot of mistakes!

\$\endgroup\$
  • \$\begingroup\$ Ok, thank you, but how do I incorporate collisions into the blocks, too? \$\endgroup\$ – javaprogammer23 Jan 19 '17 at 14:54
  • \$\begingroup\$ You're welcome, that one sounds like it might be better off in it's own forum post though. If the answer above addresses your first question, I'd suggest accepting the answer and posting the follow up question in it's own thread to get more relevant answers, otherwise I'd edit the original question to better reflect what it is you want to know. Rather than waiting around for an answer to a second forum post though, I'd try your luck at searching the existing questions in the forum for tile map collisions as I can guarantee it's already been asked and answered a few dozen times over. \$\endgroup\$ – David Greaves Jan 19 '17 at 15:37
  • \$\begingroup\$ Like here link and here link. One last bit of dad advice- I'd try out the code linked in the answer above to tackle your first problem first, and see if you can get the tile map loading in the first instance, and worry about adding collisions after the fact, your code will be cleaner and you will understand the solution better by dividing the task into smaller problems. \$\endgroup\$ – David Greaves Jan 19 '17 at 15:41
  • \$\begingroup\$ Also, I would like to stay using java.awt, as I am still relatively new to java. \$\endgroup\$ – javaprogammer23 Jan 20 '17 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.