如何用Python Arcade库获取Tiled Map对象的custom attribute?
如何访问Arcade中Tiled地图对象的自定义属性
我使用Python Arcade库和Tiled Map Editor制作平台游戏,加载Tiled地图后无法访问对象的自定义属性。
加载地图的代码
self.tile_map = arcade.load_tilemap("TiledMap.json", scaling=1)
尝试访问自定义属性的代码
self.enemies = self.tile_map.sprite_lists.get("Enemies", arcade.SpriteList()) for e in self.enemies: print(e.custom_attribute)
报错信息
Traceback (most recent call last): File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 429, in <module> main() File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 425, in main arcade.run() File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\arcade\window_commands.py", line 323, in run pyglet.app.run() File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\__init__.py", line 107, in run event_loop.run(interval) File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\base.py", line 184, in run timeout = self.idle() File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\app\base.py", line 245, in idle self.clock.call_scheduled_functions(dt) File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\clock.py", line 277, in call_scheduled_functions item.func(now - item.last_ts, *item.args, **item.kwargs) File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\arcade\application.py", line 318, in _dispatch_updates self.dispatch_event('on_update', delta_time) File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\window\__init__.py", line 1361, in dispatch_event super().dispatch_event(*args) File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\event.py", line 427, in dispatch_event raise e File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyglet\event.py", line 422, in dispatch_event if getattr(self, event_type)(*args): File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 223, in on_update self.update_enemies() File "c:\Users\galax\Documents\Arcade platformer\The Miss Alien Adventure\The miss alien adventure.py", line 418, in update_enemies print(e.custom_attribute) AttributeError: 'Sprite' object has no attribute 'custom_attribute'
解决方案
Arcade加载Tiled地图后,对象的自定义属性会被存入Sprite的properties字典,而非直接作为Sprite的属性,需要通过字典方式访问:
self.enemies = self.tile_map.sprite_lists.get("Enemies", arcade.SpriteList()) for e in self.enemies: # 替换为你在Tiled中设置的自定义属性名 print(e.properties.get("custom_attribute"))
如果希望直接通过属性名访问,可以遍历Sprite时手动赋值:
self.enemies = self.tile_map.sprite_lists.get("Enemies", arcade.SpriteList()) for e in self.enemies: e.custom_attribute = e.properties.get("custom_attribute") print(e.custom_attribute)
另外需确认:在Tiled编辑器中,已为Enemies对象层的目标对象添加了custom_attribute自定义属性,且该图层类型为对象层(不是瓦片层)。
内容的提问来源于stack exchange,提问作者user22290650
相关产品推荐
相关产品推荐

