Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a game and I have almost finished it, but I'm usually finding the error RuntimeError: maximum recursion depth exceeded when I iterate a sprite group.

This is an example and the group is self.nextStageCollision():

def nextStageCollision(self):
    for tile in self.nextStageCollision():
        if tile.rect.colliderect(self.rect):
            self.rect.x = 18*20
            self.rect.y = 20*20
            return True

    return False

The group self.nextStageCollision() has 4 sprites (tiles) and I'm checking collision between my player and those 4 tiles to go to the next stage but I have to be doing something wrong iterating the sprite group because (I don't know why) it becomes recursive and raises the maximum recursion depth runtime error. I've debugged it and when it reaches the for each line, it loops a lot of times there (althought there are only 4 sprites in that group).

I know I can increase the recursion depth, but probably it's better to not play with that and try to improve the code. So, what am I doing wrong? Is there another better way to interate a sprite group to check collisions?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You've made an infinite loop:

def nextStageCollision(self):
    for tile in self.nextStageCollision():

You probably meant something like:

def nextStageCollision(self):
    for tile in self.tileList:

Note what you are iterating over. You are calling the nextStageCollision to find what to iterate over, which calls nextStageCollision to find what to iterate over, which calls nextStageCollision and so on until you've hit the "maximum recursion depth".

You shouldn't change it, it's in a good position and it wouldn't matter, it would just take longer for your code to crash.

share|improve this answer
    
omg what a stupid error.. xD –  Drumnbass Jun 15 at 10:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.