简体   繁体   English

Java-平铺地图对象

[英]Java - Tiled Map Objects

I'm trying to make a basic 2D game using Slick2D with the Tiled Map Editor. 我正在尝试使用带有平铺地图编辑器的Slick2D制作基本的2D游戏。 I have figured out how to do basic collision detection using TileProperties but I'm not sure how Objects work with the map editor. 我已经弄清楚了如何使用TileProperties进行基本的碰撞检测,但是我不确定对象如何与地图编辑器一起工作。 I'm trying to do two things (if they're possible): more precise polygon collision detection and game items on the map that the player can pick up. 我正在尝试做两件事(如果可能的话):更精确的多边形碰撞检测以及玩家可以拾取的地图上的游戏项目。

The problem is that I don't know how to check for objects. 问题是我不知道如何检查对象。 I've looked in the Slick javadoc and I saw some methods that take int ObjectId and ObjectGroup as parameters but I'm not sure how they can be found. 我查看了Slick Javadoc,并看到了一些将int ObjectId和ObjectGroup作为参数的方法,但是我不确定如何找到它们。 Could someone please explain? 有人可以解释一下吗? Even if I know how to check Objects, how would I scan the whole map for, say, "item" objects and do things with it, like render an image at that position? 即使我知道如何检查对象,我也将如何扫描整个地图以查找“项目”对象并对其进行处理,例如在该位置渲染图像?

Any help will be greatly appreciated. 任何帮助将不胜感激。

Edit: I think I now know how to use objects but I still don't know how to get the objectID and objectGroupID. 编辑:我想我现在知道如何使用对象,但是我仍然不知道如何获取objectID和objectGroupID。 Could someone please explain ho to get the IDs either from Tiled or with Slick? 有人可以解释一下如何从Tiled或Slick获取ID吗?

Check out the java doc http://slick.ninjacave.com/javadoc/ and object TiledMap. 查看Java文档http://slick.ninjacave.com/javadoc/和对象TiledMap。

The two methods you need here are: getObjectGroupCount() & getObjectCount(int groupid) 这里需要的两个方法是: getObjectGroupCount()getObjectCount(int groupid)

the getObjectGroupCount() method is going to return the total number of layers in your tiled map which are object layers, or rather the identifiers for each object layer. getObjectGroupCount()方法将返回平铺地图中作为对象层的图层总数,或者返回每个对象层的标识符。

the getObjectCount(int groupid) is going to return the total number of objects on any given layer, or the amount of objects within an object group. getObjectCount(int groupid)将返回任何给定层上的对象总数,或对象组内的对象数量。

From here you have the total number of layers in your map and the total number of objects on each layer, so you know how many times you need to need to loop in order to access each object by it's index, first it's group id and it's object id 在这里,您可以获得地图中的图层总数以及每个图层上的对象总数,因此您知道需要循环多少次才能通过其索引访问每个对象,首先是组ID ,然后是对象ID

I don't see a way to search by name, if somebody else does then please correct me. 我找不到按名称搜索的方法,如果有人这样做,请指正我。 Otherwise I suggest reading this array when loading the map. 否则,我建议在加载地图时阅读此数组。 If there are any object ID's that you need to call dynamically within the main game loop (while it's drawing) I would hold the ID's somewhere that you can use to access the object easily later. 如果有任何对象ID需要在主游戏循环中动态调用(绘制时),我会将ID放在某个地方,以后可以用来轻松访问该对象。

EDIT: I'll do a quick non-syntax checked or tested code to explain: 编辑:我将做一个快速的非语法检查或测试的代码来解释:

TiledMap aMap = new TiledMap("whatever.tmx");
int objectGroupCount = aMap.getObjectGroupCount();
for( int gi; gi < objectGroupCount; gi++ ) // gi = object group index
{
    int objectCount = aMap.getObjectCount(gi);
    for( int oi; oi < objectCount; oi++ ) // oi = object index
    {
        System.out.println( aMap.getObjectName(gi, oi) );
        System.out.println( aMap.getObjectProperty(gi, oi, "somepropertyname", "" ) );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM