刚开始使用 tmx 平铺地图文件作为我的 2d 平台游戏的地图。这是我到目前为止的代码。
private void loadLevel() {
// TODO Auto-generated method stub
try {
TMXLoader tmxLoader = new TMXLoader(
ResourceManager.getInstance().activity.getAssets(),
engine.getTextureManager(),
TextureOptions.BILINEAR_PREMULTIPLYALPHA, vbom);
this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/testmap2.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++) {
TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
this.attachChild(layer);
}
for (final TMXObjectGroup group : this.mTMXTiledMap
.getTMXObjectGroups()) {
for (final TMXObject object : group.getTMXObjects()) {
if (group.getName().equals("Unwalkable")) {
Rectangle rect = new Rectangle(object.getX(),
object.getY(), object.getWidth(),
object.getHeight(), vbom);
FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(
0.0f, 0.0f, 1f, false,
CATEGORYBIT_WALL,
MASKBITS_WALL, (short) 0);
PhysicsFactory.createBoxBody(physicsWorld, rect,
BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
final PhysicsHandler physicsHandler = new PhysicsHandler(
rect);
rect.registerUpdateHandler(physicsHandler);
attachChild(rect);
}
}
}
}
但它给了我这个错误 - 致命异常:UpdateThread 和 java.lang.IllegalArgumenetException:没有找到属性的值:'width'
这是我的 tmx 文件的问题吗?它是如何编码的或我是如何设置的?还是在编码端?谢谢你们,我真的很感谢你的帮助。这是 LogCat 的其余部分,以备您使用。
04-25 08:36:01.716: E/AndroidRuntime(13119): FATAL EXCEPTION: UpdateThread
04-25 08:36:01.716: E/AndroidRuntime(13119): java.lang.IllegalArgumentException: No value found for attribute: 'width'
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.util.SAXUtils.getAttributeOrThrow(SAXUtils.java:47)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.util.SAXUtils.getIntAttributeOrThrow(SAXUtils.java:86)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.extension.tmx.TMXObjectGroup.<init>(TMXObjectGroup.java:37)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.extension.tmx.TMXParser.startElement(TMXParser.java:161)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:143)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.extension.tmx.TMXLoader.load(TMXLoader.java:117)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.extension.tmx.TMXLoader.loadFromAsset(TMXLoader.java:102)
04-25 08:36:01.716: E/AndroidRuntime(13119): at com.meowcatfighter.goodygreen.scene.GameScene.loadLevel(GameScene.java:96)
04-25 08:36:01.716: E/AndroidRuntime(13119): at com.meowcatfighter.goodygreen.scene.GameScene.createScene(GameScene.java:85)
04-25 08:36:01.716: E/AndroidRuntime(13119): at com.meowcatfighter.goodygreen.scene.SceneManager.setGameScene(SceneManager.java:75)
04-25 08:36:01.716: E/AndroidRuntime(13119): at com.meowcatfighter.goodygreen.scene.MainMenuScene.onMenuItemClicked(MainMenuScene.java:66)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.menu.MenuScene.onAreaTouched(MenuScene.java:139)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.Scene.onAreaTouchEvent(Scene.java:413)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:357)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.CameraScene.onSceneTouchEvent(CameraScene.java:64)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.Scene.onChildSceneTouchEvent(Scene.java:420)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.entity.scene.Scene.onSceneTouchEvent(Scene.java:338)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.engine.Engine.onTouchScene(Engine.java:452)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.engine.Engine.onTouchEvent(Engine.java:438)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.input.touch.controller.BaseTouchController$TouchEventRunnablePoolItem.run(BaseTouchController.java:102)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:54)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.util.adt.pool.RunnablePoolUpdateHandler.onHandlePoolItem(RunnablePoolUpdateHandler.java:1)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.util.adt.pool.PoolUpdateHandler.onUpdate(PoolUpdateHandler.java:88)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.input.touch.controller.BaseTouchController.onUpdate(BaseTouchController.java:62)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.engine.Engine.onUpdate(Engine.java:584)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
04-25 08:36:01.716: E/AndroidRuntime(13119): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)
(编辑 - 非常感谢 Matej Spili 编辑 LogCat!现在看起来更容易了!:D)
编辑 2014 年 4 月 29 日
所以,所以我采访了 Tiled 的制作者 bjorn。
- 我 - 我使用什么代码似乎并不重要,因为它总是那个错误
我 - 当我查看一个示例 tmx 文件时,最后它看起来像....
<objectgroup name="Unwalkable" width="400" height="21">
- 我 - 但是当我查看我创建的 tmx 文件时,没有高度和宽度规范。如何指定对象层的高度/宽度?
- bjorn - dpod:不再写出这些属性。
- bjorn - dpod: AndEngine 应该被修复为不需要它们。它们基本上总是毫无意义的,因为对象组没有大小限制。
- bjorn - 这些函数名称如“getTMXObjectGroupProperties”或“containsTMXProperty”绝对是疯狂的......
- bjorn - 在 Tiled 中,该代码更像:“foreach (ObjectGroup *group, map->objectGroups()) { if (group->hasProperty("wall")) { ...
- IRC - [15:02] == bjorn [~quassel@pdpc/supporter/active/bjorn]
- IRC - [15:02] == 实名:Thorbjørn Lindeijer
- IRC - [15:02] == 频道:#tiled
- IRC - [15:02] == 服务器:asimov.freenode.net [TX,美国]
- IRC - [15:02] == 帐户:bjorn
- IRC - [15:02] == WHOIS 结束
- 我 - 哦,我的天哪,你是制作瓷砖的人?!
那很好玩哈哈。但现在我有更多的问题!到底什么是foreach?