How do you implement Tiled maps into Java?

2,320 views
Skip to first unread message

jrwv...@yahoo.com

unread,
Aug 17, 2013, 12:12:48 PM8/17/13
to mape...@googlegroups.com
Hi sorry for asking this but I'm new to using Tiled and I really have no idea how to use its .tmx format to put into java. I am somewhat experienced in Java as I've took a class in it but it did not cover anything with using any xml or tmx format. So I've been really looking hard into this but I'm still confused on how I can put Tiled's map into my java game. For example I have no idea how to actually implement it into my code where it can call the map file and I have no idea how to display the map either.  (I've seen that I have to use the libtiled-java thing but I'm not sure how to use it)

Is there any where you can point me or help me?

Stefan Beller

unread,
Aug 17, 2013, 12:41:53 PM8/17/13
to mape...@googlegroups.com, jrwv...@yahoo.com
signature.asc

jrwv...@yahoo.com

unread,
Aug 17, 2013, 12:59:20 PM8/17/13
to mape...@googlegroups.com, jrwv...@yahoo.com, stefan...@googlemail.com
I already said I've seen it and know about it I just don't know how to use it. Next time please give a little description on what your posting or at least read my whole post. If you want you can explain how to use it

Thorbjørn Lindeijer

unread,
Aug 17, 2013, 2:43:17 PM8/17/13
to mape...@googlegroups.com
Using libtiled-java is one option, another would be to write your own
TMX parser. What to prefer depends on your goal. Writing your own can
be preferable either as a learning experience or because you could
write a parser that's more suitable for your game or application.

While there is no real tutorial, there's two sources of help with
libtiled-java. One is the example TMX viewer application located at
https://github.com/bjorn/tiled/tree/master/util/java/tmxviewer-java.
The other is the javadoc style comments in the source code from which
HTML documentation can be generated using 'ant javadoc'.

Regards,
Bjørn

jrwv...@yahoo.com

unread,
Aug 17, 2013, 5:32:48 PM8/17/13
to mape...@googlegroups.com
I am interested in making my own TMX parser, but like I said before I have no experience in using it. Is there different types of parsers or I can code what it looks for and do when it finds it?

headh...@gmail.com

unread,
Aug 17, 2013, 5:36:10 PM8/17/13
to mape...@googlegroups.com
Are you doing a java application, applet, or an android app?  XML parsing on android is slow unless you’re using android’s xml resources and the xml pull parser.  You’ll want to start with an xml parsing library.  Your first choice there is SAX or DOM sax is faster and more memory efficient usually, but more complicated to work with.  Then you need to create the object model you’ll draw from.  If you don’t know how to draw sprites from a spritesheet you’ll want to read up on that as well.  After that comes the relatively easy task of using your xml parsing library to build your object model so you can draw it.
 
~Tom
 
--
--
Tiled Map Editor mailing list
mape...@googlegroups.com
http://groups.google.com/group/mapeditor
 
---
You received this message because you are subscribed to the Google Groups "Tiled Map Editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapeditor+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

jrwv...@yahoo.com

unread,
Aug 17, 2013, 9:11:34 PM8/17/13
to mape...@googlegroups.com
I'm making a application so does that mean I need to use a library like SAX or DOM or is that for Andriod?

headh...@gmail.com

unread,
Aug 17, 2013, 10:17:48 PM8/17/13
to mape...@googlegroups.com
Those are types of parsers SAX (Simple API for XML) and DOM (Document Object Model).
Unless you want to write your own xml parser you’ll be using one of those types of parsers on any platform.  Platform choice affects how you’ll be drawing because android uses different objects and a different graphics api.
 
SAX parsers process the document character by character and send you events.  They can easily handle very large documents, but the code to handle all of those events becomes complicated because you need to store your state.  You can do it with variables or other techniques, but you have to know where you are in a document when you get one of those events.  With DOM you’re loading the entire xml file into memory at once and instantiating an entire object tree for it meaning you’re going to use more memory but it’s easier to get the data and you can use XPath queries or simply inspect the objects to build your data model from that.
 
libtiled I believe takes care of all this for you.  You give it a filename and it gives you back an object model.
 
Here is a link to a description of the TMX format https://github.com/bjorn/tiled/wiki/TMX-Map-Format.
And here is a link to the source for libtiled-java if you want to see how it handles the parsing https://github.com/bjorn/tiled/tree/master/util/java/libtiled-java.
 
~Tom
 

jrwv...@yahoo.com

unread,
Aug 17, 2013, 11:07:20 PM8/17/13
to mape...@googlegroups.com
The thing is I don't know what to do with the libtiled folder (sorry I'm new with importing things as I've only used straight up java when coding my game). Like do I open all of the folders into the java project folder? Another thing I don't get is the commands for the Apache ant tool. If I was to open the files of libtiled into a java project do I run the build? If so it gets an error that it can't find the javac compiler.

headh...@gmail.com

unread,
Aug 17, 2013, 11:16:36 PM8/17/13
to mape...@googlegroups.com
 
 
Sent from Windows Mail
 

headh...@gmail.com

unread,
Aug 17, 2013, 11:17:07 PM8/17/13
to mape...@googlegroups.com
If ant is giving you that error then you’ve probably just got your paths wrong.  set an environment variable for JAVA_HOME to the jdk path ie C:\Program Files\Java\jdk1.7.0_25 and then add %JAVA_HOME%\bin to your path.  That’s assuming windows.  It’ll be similar for anything else.  Ant has similar install instructions AFAIK (ANT_HOME and adding %ANT_HOME%\bin to your path).  I haven’t used libtiled but usually you just build the library which creates a jar and then you add that jar to your project and classpath.  It’s fairly simple with eclipse and probably other IDEs.  For eclipse I just add a folder called libs and stick the jars in there then in your project setup add a reference to the jar.  The jars don’t need to be in the project folder, but if you’re using source control it’s easier that way so you can version that jar along with the rest of your project files.
 
Somebody else can correct me if any of this is wrong for libtiled, but it’s fairly standard java practices.
 
Depending on licensing you could also just include the libtiled source code directly in your project.  Including it as a jar is probably worth the small up front extra effort.
 
~Tom
 
Sent from Windows Mail
 
From: jrwv...@yahoo.com
Sent: ‎Saturday‎, ‎August‎ ‎17‎, ‎2013 ‎8‎:‎07‎ ‎PM
To: mape...@googlegroups.com
 

jrwv...@yahoo.com

unread,
Aug 18, 2013, 12:04:39 AM8/18/13
to mape...@googlegroups.com
So going through my system settings and choosing system settings I would create a new variable JAVA_HOME with a path of C:\Program File\Java\jdk1.7.0_25 %JAVA_HOME%\bin correct? Also when I run ant it should be in eclipse right?

headh...@gmail.com

unread,
Aug 18, 2013, 12:05:31 AM8/18/13
to mape...@googlegroups.com
The java home path depends on where you have java installed I’m using 64bit windows 8 and 64 bit java sdk v1.7.0 release 25 so that’s my path.  You should check yours.  It’s the folder that contains a bin folder that contains javac.exe the java compiler.
 
I don’t run ant from eclipse I do it from the command line so I make an applications folder in my home folder and extract the zip there.  So for me ANT_HOME is C:\Users\Tom\Applications\apache-ant-1.9.2
 
Then add “%JAVA_HOME%\bin;%ANT_HOME%\bin” to my path without the quotes.  I use maven too so there’s also a MAVEN_HOME environment variable and %MAVEN_HOME%\bin in my path.
 
Once your paths are setup just open a command prompt and go to the folder with build.xml in it and run ant.

jrwv...@yahoo.com

unread,
Aug 18, 2013, 10:39:36 AM8/18/13
to mape...@googlegroups.com
Okay so when I look through my system variables I do not see PATH, JAVA_HOME, or ANT_HOME. Do I make them myself and then set it with the things you put. Also since I am using eclipse I believe it has ant in it so I would link it to the folder inside eclipse instead?

jrwv...@yahoo.com

unread,
Aug 18, 2013, 10:48:17 AM8/18/13
to mape...@googlegroups.com
Also I forgot to mention that the error for when I run the build.xml in eclipse it says:
 com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "C:\Program Files\Java\jre7"

Also When running ant in the command prompt it says that it 'ant' is not recognized as an internal external command, operable program or batchfile.

Tom Hicks

unread,
Aug 18, 2013, 2:39:41 PM8/18/13
to mape...@googlegroups.com

Yeah you chould create the environment variables.  I wouldnt use the ant inside eclipse from the command line.  If you want to use ant through eclipse look for a tutorial on that as I cant help.  I always use it via the command line.

jrwv...@yahoo.com

unread,
Aug 18, 2013, 3:05:05 PM8/18/13
to mape...@googlegroups.com
Okay thank you for all the help!
Reply all
Reply to author
Forward
0 new messages