【问题标题】:AttributeError when trying to getTileImageByGid in pyTMX尝试在 pyTMX 中获取 TileImageByGid 时出现 AttributeError
【发布时间】:2019-09-12 09:35:19
【问题描述】:

我正在尝试为 pyGame 中的项目加载 TMX 地图。当我不添加对象时,地图加载正常。我使用 Tiled 创建地图。当我这样做时,我得到一个属性错误。

我尝试重新安装 pytmx 并尝试使用其他地图,但似乎没有任何效果。

def gameStart():
    game = True

    tm = pytmx.load_pygame('resources/maps/map1/map.tmx')
    size = tm.width * tm.tilewidth, tm.height * tm.tileheight

    tmx_data = tm

    tw = tmx_data.tilewidth
    th = tmx_data.tileheight
    gt = tmx_data.getTileImageByGid

    while game:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        if tmx_data.background_color:
            screen.fill(tmx_data.background_color)

        for layer in tmx_data.visibleLayers:
            if isinstance(layer, pytmx.TiledLayer):
                for x, y, gid in layer:
                    tile = gt(gid)
                    if tile:
                        screen.blit(tile, (x * tw, y * th))

            elif isinstance(layer, pytmx.TiledObjectGroup):
                pass

            elif isinstance(layer, pytmx.TiledImageLayer):
                image = gt(layer.gid)
                if image:
                    screen.blit(image, (0, 0))



        pygame.display.update()

        clock.tick(framerate)

返回以下内容:

File "main.py", line 45, in gameStart
    gt = tmx_data.getTileImageByGid
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pytmx/pytmx.py", line 214, in __getattr__
    raise AttributeError
AttributeError

【问题讨论】:

  • 您从哪里获得有关getTileImageByGid 的信息?你检查了documentation 吗?似乎它可能有不同的名称 - 类似于其他方法get_tile_properties_by_gid

标签: python python-3.x pygame pytmx


【解决方案1】:

阅读documentation 它有不同的名称: get_tile_image_by_gid(gid)


下次您可以查看print( dir(tmx_data) ) 以查看tmx_data 中的所有方法和属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2013-10-31
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多