[英]Maximum recursion depth exceeded when creating subclass of pygame.sprite.Sprite
[英]AttributeError in a pygame.sprite.Sprite subclass
我目前正在开发一款带有pygame的2D平台游戏,我发现了一个问题。 我通常使用在main函数内部声明的单个sprite组来处理sprite渲染。 现在我需要有一些特定的精灵超过其他人/精灵,有一个小组不会削减它,并且让多个小组刚刚铺设是一团糟。 所以我决定将组添加到我的Entity类中:
class Entity(pygame.sprite.Sprite):
entitiesTop = pygame.sprite.Group()
entitiesMid = pygame.sprite.Group()
entitiesBot = pygame.sprite.Group()
entities = [entitiesBot, entitiesMid, entitiesTop]
def __init__(self, force = None):
pygame.sprite.Sprite.__init__(self)
if force is None:
if isinstance(self, Platform):
Entity.entitiesTop.add(self)
elif isinstance(self, (Bullet, Gun)):
Entity.entitiesMid.add(self)
else:
Entity.entitiesBot.add(self)
else:
Entity.entities[force].add(self)
并且我使用__init__
方法自动将实体的所有其他子类添加到组中。 我认为它在类中运行良好,因为当我初始化实体本身时错误没有显示,而是当我尝试运行此代码时
for group in Entity.entities:
出现了一个AttributeError
AttributeError: type object 'Entity' has no attribute 'entities'
我对python OOP比较新,所以我不太清楚我在这里缺少什么。 有谁知道这个解决方案?
福拉斯在评论中解开了这个谜团。 我只是忘了删除班级的旧定义! 傻我。 它现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.