TMX地图格式

The TMX (Tile Map XML) map format used by Tiled is a flexible way to describe a tile based map. It can describe maps with any tile size, any amount of layers, any number of tile sets and it allows custom properties to be set on most elements. Beside tile layers, it can also contain groups of objects that can be placed freely.

TMX(Tile Map XML)是Tiled使用的很具有扩展性的瓦片地图描述格式。他描述的内容包括地图的大小,层次,每个层次的瓦片组,以及各个元素的特殊参数。除此之外,他还可以描述可以自由放置的对象组。

In this document we'll go through each element found in this map format. The elements are mentioned in the headers and the list of attributes of the elements are listed right below, followed by a short explanation. Attributes that are deprecated or unsupported by the current version of Tiled are formatted in italics.

本文我们会全面的了解一次每一种元素。每个元素都列在标题上,紧跟着的就是该元素的普通参数。

<map>

  • version: The TMX format version, generally 1.0.
  • orientation: Map orientation. Tiled supports "orthogonal" and "isometric" at the moment.(视图方式。目前只有直角和45度两种各种,不支持6角形)
  • width: The map width in tiles.(宽格子数,如果是isometric则是斜边的格子数)
  • height: The map height in tiles.(高格子数)
  • tilewidth: The width of a tile.(一个格子的宽度,isometric格子是棱型。64×64的格子显示出来大概是80*64)
  • tileheight: The height of a tile.(一个格子的高度)

The tilewidth and tileheight properties determine the general grid size of the map. The individual tiles may have different sizes. Larger tiles will extend at the top and right (anchored to the bottom left).

tilewidth、tileheight参数决定了说明地图的瓦片大小。不同的格子可以有不同的大小。大的格子会在右上角进行大小拓展。

Can contain: properties, tileset, layer, objectgroup

可以包含的元素:properties、tileset、layer、objectroup

<tileset>

  • firstgid: The first global tile ID of this tileset (this global ID maps to the first tile in this tileset).
  • source: If this tileset is stored in an external TSX (Tile Set XML) file, this attribute refers to that file. That TSX file has the same structure as the attribute as described here. (There is the firstgid attribute missing and this source attribute is also not there. These two attributes are ḱept in the TMX map, since they are map specific.)
  • name: The name of this tileset.
  • tilewidth: The (maximum) width of the tiles in this tileset.
  • tileheight: The (maximum) height of the tiles in this tileset.
  • spacing: The spacing in pixels between the tiles in this tileset (applies to the tileset image).
  • margin: The margin around the tiles in this tileset (applies to the tileset image).

Can contain: properties (since 0.8.0), image, tile

<image>

  • format: To be used when embedding images. Deprecated and unsupported in Tiled Qt.
  • id: Used by some versions of Tiled Java. Deprecated and unsupported by Tiled Qt.
  • source: The reference to the tileset image file (Tiled supports most common image formats).
  • trans: Defines a specific color that is treated as transparent (example value: "FF00FF" for magenta).

As of the current version of Tiled Qt, each tileset has a single image associated with it, which is cut into smaller tiles based on the attributes defined on the tileset element. Later versions may add support for adding multiple images to a single tileset, as is possible in Tiled Java.

<tile>

  • id: The local tile ID within its tileset.

Can contain: properties, image

<layer>

  • name: The name of the layer.
  • x: The x coordinate of the layer in tiles. Defaults to 0 and can no longer be changed in Tiled Qt.
  • y: The y coordinate of the layer in tiles. Defaults to 0 and can no longer be changed in Tiled Qt.
  • width: The width of the layer in tiles. Traditionally required, but as of Tiled Qt always the same as the map width.
  • height: The height of the layer in tiles. Traditionally required, but as of Tiled Qt always the same as the map height.
  • opacity: The opacity of the layer as a value from 0 to 1. Defaults to 1.
  • visible: Whether the layer is shown (1) or hidden (0). Defaults to 1.

Can contain: properties, data

<data>

  • encoding: The encoding used to encode the tile layer data. When used, it can be "base64" and "csv" at the moment.
  • compression: The compression used to compress the tile layer data. Tiled Qt supports "gzip" and "zlib".

When no encoding or compression is given, the tiles are stored as individual XML tile elements. Next to that, the easiest format to parse is the "csv" (comma separated values) format.

The base64-encoded and optionally compressed layer data is somewhat more complicated to parse. First you need to base64-decode it, then you may need to decompress it. Now you have an array of bytes, which should be interpreted as an array of unsigned 32-bit integers using little-endian byte ordering.

Whatever format you choose for your layer data, you will always end up with so called "global tile IDs" (gids). They are global, since they may refer to a tile from any of the tilesets used by the map. In order to find out from which tileset the tile is you need to find the tileset with the highestfirstgid that is still lower or equal than the gid. The tilesets are always stored with increasing firstgids.

When you use the tile flipping feature added in Tiled Qt 0.7.0, the highest two bits of the gid store the flipped state. Bit 32 is used for storing whether the tile is horizontally flipped and bit 31 is used for the vertically flipped tiles. And since Tiled Qt 0.8.0, bit 30 means whether the tile is flipped (anti) diagonally, enabling tile rotation. These bits have to be read and cleared before you can find out which tileset a tile belongs to.

The following C++ pseudo-code should make it all clear:

// Bits on the far end of the 32-bit global tile ID are used for tile flags
const unsigned FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
const unsigned FLIPPED_VERTICALLY_FLAG   = 0x40000000;
const unsigned FLIPPED_DIAGONALLY_FLAG   = 0x20000000;

...

// Extract the contents of the <data> element
string tile_data = ...

unsigned char *data = decompress(base64_decode(tile_data));
unsigned tile_index = 0;

// Here you should check that the data has the right size
// (map_width * map_height * 4)

for (int y = 0; y < map_height; ++y) {
  for (int x = 0; x < map_width; ++x) {
    unsigned global_tile_id = data[tile_index] |
                              data[tile_index + 1] << 8 |
                              data[tile_index + 2] << 16 |
                              data[tile_index + 3] << 24;
    tile_index += 4;

    // Read out the flags
    bool flipped_horizontally = (global_tile_id & FLIPPED_HORIZONTALLY_FLAG);
    bool flipped_vertically = (global_tile_id & FLIPPED_VERTICALLY_FLAG);
    bool flipped_diagonally = (global_tile_id & FLIPPED_DIAGONALLY_FLAG);

    // Clear the flags
    global_tile_id &= ~(FLIPPED_HORIZONTALLY_FLAG |
                        FLIPPED_VERTICALLY_FLAG |
                        FLIPPED_DIAGONALLY_FLAG);

    // Resolve the tile
    for (int i = tileset_count - 1; i >= 0; --i) {
      Tileset *tileset = tilesets[i];

      if (tileset->first_gid() <= global_tile_id) {
        tiles[y][x] = tileset->tileAt(global_tile_id - tileset->first_gid());
        break;
      }
    }
  }
}

(Since the above code was put together on this wiki page and can't be directly tested, please make sure to report any errors you encounter when basing your parsing code on it, thanks.)

Can contain: tile

<tile>

  • gid: The global tile ID.

Not to be confused with the tile element inside a tileset, this element defines the value of a single tile on a tile layer. This is however the most inefficient way of storing the tile layer data, and should generally be avoided.

<objectgroup>

  • name: The name of the object group.
  • color: The color used to display the objects in this group.
  • x: The x coordinate of the object group in tiles. Defaults to 0 and can no longer be changed in Tiled Qt.
  • y: The y coordinate of the object group in tiles. Defaults to 0 and can no longer be changed in Tiled Qt.
  • width: The width of the object group in tiles. Meaningless.
  • height: The height of the object group in tiles. Meaningless.
  • opacity: The opacity of the layer as a value from 0 to 1. Defaults to 1.
  • visible: Whether the layer is shown (1) or hidden (0). Defaults to 1.

The object group is in fact a map layer, and is hence called "object layer" in Tiled Qt.

Can contain: properties, object

<object>

  • name: The name of the object. An arbitrary string.
  • type: The type of the object. An arbitrary string.
  • x: The x coordinate of the object in pixels.
  • y: The y coordinate of the object in pixels.
  • width: The width of the object in pixels.
  • height: The height of the object in pixels.
  • gid: An reference to a tile (optional).

While tile layers are very suitable for anything repetitive aligned to the tile grid, sometimes you want to annotate your map with other information, not necessarily aligned to the grid. Hence the objects have their coordinates and size in pixels, but you can still easily align that to the grid when you want to.

You generally use objects to add custom information to your tile map, such as spawn points, warps, exits, etc.

When the object has a gid set, then it is represented by the image of the tile with that global ID. Currently that means width and height are ignored for such objects. The image alignment currently depends on the map orientation. In orthogonal orientation it's aligned to the bottom-left while in isometric it's aligned to the bottom-center.

Can contain: properties, polygon, polyline, image

<polygon>

  • points: A list of x,y coordinates in pixels.

Each polygon object is made up of a space-delimited list of x,y coordinates. The origin for these coordinates is the location of the parentobject. By default, the first point is created as 0,0 denoting that the point will originate exactly where the object is placed.

<polyline>

  • points: A list of x,y coordinates in pixels.

polyline follows the same placement definition as a polygon object.

<properties>

Can contain: property

Wraps any number of custom properties. Can be used as a child of the maptile (when part of a tileset), layerobjectgroup andobject elements.

<property>

  • name: The name of the property.
  • value: The value of the property.

When the property spans contains newlines, the current versions of Tiled Java and Tiled Qt will write out the value as characters contained inside the property element rather than as the value attribute. However, it is at the moment not really possible to edit properties consisting of multiple lines with Tiled.

It is possible that a future version of the TMX format will switch to always saving property values inside the element rather than as an attribute.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
tIDE是多国语言的免费软件,但是!竟然没有中文,关于它的介绍自己看就不说了,下面来创建第一张地图,用于我们的TD游戏。 你现在需要一张这样的tile图片,来做完地图的基本元件 在Explorer面板Map节点地下的Tile Sheets右键Add,然后浏览选择上面的或者你自己的tile文件。 OK弹出Tile Sheet Properties对话框设置一下Tile Size让Tile完美的切割。这里的值是60x60。 ok,然后会在Tile picher面板里就会出现一个个分割好的Tile。 有了这些tile的支持就可以在map上玩拼图了。在map Explorer面板下的Map节点的Layers上右键Add,在弹出的Layer Properties设置地图的大小Layer Size,因为我现在只打算做一个最简单的也就是一屏幕而且不能拖动所以这里设置14x8 (800/60 x 480/60),OK。 为了方便绘制在工具面板的右上角有一个显示网格的按钮,点下去它 右边的绘图工具栏上有一个很好使用的按钮区域绘制,它能把选择区域填充出当前选择的tile,下面就先在tile Picker选择一块浅色的草地区域绘制到整个地图。然后画上我们的路。 Save as为 tmx类型的文件 中间如果出现警告确定就好了 这里出现了最关键的一步,这里一定要选Base64(gzip compressed),ccx现在的版本还只支持这一种。 把生成的tmx文件以及tile图片加入我们的项目中。 这里可能需要调整一下tmx文件中的Image source的路径 在程序中显示TMX地图 先显示出来看看努力的效果,之后再慢慢完善。 把GameScreen构造函数中的说明Label删除,添加CCTMXTiledMap 代码很简单如下 private GameScreen() { CCTMXTiledMap tmxmap = CCTMXTiledMap.tiledMapWithTMXFile("Map/Level1"); addChild(tmxmap); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值