sprite group doesn't support indexing
Posted
by
user3956
on Game Development
See other posts from Game Development
or by user3956
Published on 2011-05-02T04:52:47Z
Indexed on
2014/08/20
10:36 UTC
Read the original article
Hit count: 193
I have a sprite group created with pygame.sprite.Group() (and add sprites to it with the add method)
How would I retrieve the nth sprite in this group?
Code like this does not work:
mygroup = pygame.sprite.Group(mysprite01)
print mygroup[n].rect
It returns the error: group object does not support indexing.
For the moment I'm using the following function:
def getSpriteByPosition(position,group):
for index,spr in enumerate(group):
if (index == position):
return spr
return False
Although working, it just doesn't seem right...
Is there a cleaner way to do this?
EDIT: I mention a "position" and index but it's not important actually, returning any single sprite from the group is enough
© Game Development or respective owner