Python Arcade与Tiled Map Editor中文本对象不显示的问题求助
Tiled文本对象在Arcade中不显示的问题解决
问题原因
Arcade的Scene.from_tilemap()方法默认仅加载瓦片层、精灵层和碰撞相关对象,不会自动处理Tiled中的文本对象。文本属于特殊类型的对象,需要手动从TileMap中提取并单独渲染。
解决方法
步骤1:初始化时提取文本对象
加载TileMap后,从指定的"Tutorial"对象层中筛选出文本对象,保存其关键属性:
# 假设已完成tile_map加载 self.tutorial_texts = [] # 获取Tutorial对象层 tutorial_layer = self.tile_map.object_lists.get("Tutorial") if tutorial_layer: for obj in tutorial_layer: # 判断是否为文本对象(Tiled文本对象自带text属性) if hasattr(obj, "text"): text_info = { "content": obj.text, "x": obj.x, "y": obj.y, "color": obj.properties.get("color", arcade.csscolor.BLACK), "font_size": obj.properties.get("fontsize", 16), "font": obj.properties.get("fontfamily", "Arial") } self.tutorial_texts.append(text_info)
步骤2:在on_draw中手动渲染文本
在场景绘制完成后,使用游戏相机渲染提取到的文本,注意转换Tiled与Arcade的Y轴坐标(Tiled Y轴向上,Arcade Y轴向下):
def on_draw(self): self.clear() self.camera.use() self.scene.draw() # 渲染Tutorial层的文本对象 for text in self.tutorial_texts: # 转换Y轴坐标,适配Arcade的坐标系 map_total_height = self.tile_map.height * self.tile_map.tile_height * TILE_SCALING adjusted_y = map_total_height - text["y"] * TILE_SCALING # 绘制文本并应用缩放比例 arcade.draw_text( text["content"], text["x"] * TILE_SCALING, adjusted_y, text["color"], text["font_size"] * TILE_SCALING, font_name=text["font"] ) self.bullets.draw() self.gui_camera.use() score_text = f"Score: {self.player.score}" arcade.draw_text(score_text, 10, 10, arcade.csscolor.BLACK, 18)
额外注意事项
- 检查Tiled导出的JSON文件:确认"Tutorial"层下的文本对象包含
text字段,确保数据被正确导出。 - 验证坐标范围:如果文本仍不显示,可打印
adjusted_y和text["x"]的值,确认文本处于相机可视区域内。 - 字体兼容性:确保指定的字体在运行环境中存在,否则会自动使用系统默认字体。
内容的提问来源于stack exchange,提问作者user23668033
相关产品推荐
相关产品推荐

