最近想做个Android的游戏,用到libgdx,现在研究地图这块,制作好地图tmx文件后需要处理后libgdx才能载入,需要用到TiledMapPacker工具,该工具被放在gdx-tiled-preprocessor.jar里,这破玩意可愁死我了,一路尝试下来各种各种报错神马的诸如:

Failed to load Main-Class manifest attribute

 

Exception in thread "main" com.badlogic.gdx.utils.GdxRuntimeException: Creating window failed
at com.badlogic.gdx.backends.jogl.JoglApplication.<init>(JoglApplication.java:86)
at com.badlogic.gdx.tiledmappacker.TiledMapPacker.main(TiledMapPacker.java:329)
at com.skyd.tiledmappacker.TiledMapPackerTool.main(TiledMapPackerTool.java:15)
Caused by: java.lang.reflect.InvocationTargetException
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
at com.badlogic.gdx.backends.jogl.JoglApplication.<init>(JoglApplication.java:80)
… 2 more
Caused by: java.lang.UnsatisfiedLinkError: no gdx64 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.badlogic.gdx.utils.GdxNativesLoader.load(GdxNativesLoader.java:118)
at com.badlogic.gdx.backends.jogl.JoglNativesLoader.load(JoglNativesLoader.java:29)
at com.badlogic.gdx.backends.jogl.JoglApplication.initialize(JoglApplication.java:111)
at com.badlogic.gdx.backends.jogl.JoglApplication$1.run(JoglApplication.java:82)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

 

Exception in thread "main" java.lang.NullPointerException
at com.badlogic.gdx.tiledmappacker.TiledMapPacker.processMap(TiledMapPac
ker.java:92)
at com.skyd.tiledmappacker.Main.main(Main.java:38)

 

到最后看网上别人也无解,说这玩意全是错误,都是用单独的图块文件通过TexturePacker拼起来后得到整图及packfile文件,然后再拿进地图编辑器编辑以输出tmx文件,感觉切成小图再重新拼合挺麻烦的,就尝试修正这个工具:

image

我新建了这个项目,引入了相关的jar和gdx-tiled-preprocessor.jar的源码,在新建的Main类中的主函数里仿照原先TiledMapPacker类的主函数的调用方式调用TiledMapPacker类的功能,但是去掉了新建窗口的那部分代码:

image

我真不知道这操蛋的窗口建起来是搞毛用的,里面屁也不执行,还是死活都会报错,娘的,这是Main类的代码:

package com.skyd.tiledmappacker;

import java.io.File;
import java.io.IOException;

import com.badlogic.gdx.tiledmappacker.TiledMapPacker;
import com.badlogic.gdx.tools.imagepacker.TexturePacker.Settings;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File tmxFile, inputDir, outputDir;

		Settings settings = new Settings();

		// Note: the settings below are now default...
		settings.padding = 2;
		settings.duplicatePadding = true;

		TiledMapPacker packer = new TiledMapPacker();

		if (args.length != 2) {
			System.out.println("Usage: INPUTDIR OUTPUTDIR");
			System.exit(0);
		}

		inputDir = new File(args[0]);
		outputDir = new File(args[1]);

		System.out.println("待处理目录:"+inputDir+" "+inputDir.getAbsolutePath());
		System.out.println("待处理目录:"+outputDir+" "+outputDir.getAbsolutePath());

		if (!inputDir.exists()) {
			throw new RuntimeException("Input directory does not exist");
		}

		try {
			packer.processMap(inputDir, outputDir, settings);
		} catch (IOException e) {
			throw new RuntimeException("Error processing map: " + e.getMessage());
		}
	}

}

修好了窗口问题,执行起来还是有BUG,执行到TiledMapPacker类的processMap方法里就会报错,要修改其源码,将其中创建FileHandle的方式全部由“Gdx.files.absolute(XXX.getAbsolutePath())”改为“new FileHandle(XXX.getAbsolutePath())”形式,改好后的代码:

 

	public void processMap (File inputDir, File outputDir, Settings settings) throws IOException {
		FileHandle inputDirHandle = new FileHandle(inputDir.getAbsolutePath());
		File[] files = inputDir.listFiles(new TmxFilter());

		for (File file : files) {
			System.out.println("待处理文件:"+file+" "+file.getAbsolutePath());
			map = TiledLoader.createMap(new FileHandle(file.getAbsolutePath()));

			for (TileSet set : map.tileSets) {
				if (!processedTileSets.contains(set.imageName)) {
					processedTileSets.add(set.imageName);
					packTileSet(set, inputDirHandle, outputDir, settings);
				}
			}

			writeUpdatedTMX(outputDir, map.tmxFile);
		}
	}

 

然后导出为Runnable JAR file:

SNAGHTML1f2bb657

SNAGHTML1f2cbb27

注意入口选择Main类。

 

修正好之后的测试环境:

image

在in目录里放置素材图片和地图编辑器生成的tmx文件:

image

命令行执行:

D:test>java -jar TiledMapPacker.jar in out

SNAGHTML1f30d2bc

out目录将得到处理后的文件:

image

至此就搞定了,可以在libgdx里进行渲染了。

 

image

修正后的TiledMapPacker.jar下载:http://vdisk.weibo.com/s/4jXW0

 

参考文档:

http://www.cnblogs.com/htynkn/archive/2012/01/12/libgdx_12.html

http://blog.csdn.net/cng1991/article/details/7288234

https://code.google.com/p/libgdx/wiki/TiledMaps

http://dpk.net/2011/05/01/libgdx-box2d-tiled-maps-full-working-example-part-1/

分享或转载本博客站点内的所有原创内容时,都必须遵循此协议:

姓名标示-非商业性-相同方式分享 4.0 国际 (CC BY-NC-SA 4.0)

同时必须附加指向本文页面本博客首页的超链接。

除此之外的转载、分享方式都必须征得本博客作者的授权,否则将会诉诸法律。