简体   繁体   中英

Tiled / pytmx - Problem with getting same gid for same image in different maps

I always getting gid == 1 for first element in maps, even with same tileset. Saved file "example.tmx" have value encoded with CVS. I open file with txt editor and it's look like this:

<data encoding="csv">
24,24,19,24,24,22,19,23,18,23,2
...

This is great. First element is 24'th tile. But when I load map with pytmx, and I try to get x, y, gid it's different.

ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
            #print(layer.name)
            if layer.name == "layer1": 
                for x, y, gid in layer:
                    print(x, y, gid, self.id)
                    tile = ti(gid)
                    #print(self.tmxdata.get_tileset_from_gid(gid))
                    #print(tile)
                    if tile:
                        surface.blit(tile, (x * self.tmxdata.tilewidth + (y&1) * self.tmxdata.tilewidth / 2, y * self.tmxdata.tileheight / TILESIZE[0] * TILESIZE[1]))
                    self.id += 1

Printed values is (first == x, second == y, third == gid, last == id):

0 0 1 0
1 0 1 1
2 0 1 2 ...

And it's for all maps, the first gid is always "1" but it's discribe different tile in different map.

With ti(gid) / get_tile_image_by_gid it's getting correct image. But I want use gid number for other thinks like:

if gid == 1:
    self.grass_group.append(...)
else:
    self.other_group.append(...)

Ok, I solved it.

In pytmx "register_gid" doesn't work properly for me. I needed to make list like this:

self.listtiles = [z for z in tm.gidmap]

And then when I go through each element in layer, I can get it from this list.

if layer.name == "layer1": 
    for x, y, gid in layer:
        # old wrong
        print(x, y, gid, self.id)   
        # new correct
        print(x, y, self.listtiles[int(gid - 1)], self.id)    
        tile = ti(gid)
        if tile:
            surface.blit(tile, (x * self.tmxdata.tilewidth + (y&1) * self.tmxdata.tilewidth / 2, y * self.tmxdata.tileheight / TILESIZE[0] * TILESIZE[1]))
        self.id += 1

Pytmx is strange. It's quite difficult to get the id of the tile (probably bcz there can be multiple tilemaps)

This is what I found.

tmx_data = #load tmx data
gid_to_id_map = list(tmx_data.gidmap) # get list of keys

def get_id(gid): #index is supposed to mean id
    if gid != 0: #getting id for 0 is very strange
        index = tmx_data[gid-1]-1
    else: index = -1
    return index
# this is so stupidly complicated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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