I’m following this tutorial and I’ve double and triple checked that I have it as the author wrote it, but I get a “list index out of range” error when I try to run it.
The line 15- super().init(pos, self.frames[self.frame_index], groups, z) is where it stops in the debug. It’s a part of this AnimatedSprite class. There are also no errors when I comment it out.
class AnimatedSprite(Sprite):
def __init__(self, pos, frames, groups, z = Z_LAYERS['main'], animation_speed = ANIMATION_SPEED):
self.frames, self.frame_index = frames, 0
super().__init__(pos, self.frames[self.frame_index], groups, z) #### doesnt work for some reason @ end of level setup animated obj frame obj not showing atm
self.animation_speed = animation_speed
def animate(self, dt): #not working because of above
self.frame_index += self.animation_speed * dt
self.image = self.frames[int(self.frame_index % len(self.frames))]
def update(self, dt): # not working because of above
self.animate(dt)
I assume it’s a small detail I’ve overlooked and my own lack of knowledge is preventing me from seeing it. (obviously) Either I don’t properly understand why this list is causing this error cause I don’t really understand lists (?!) or I’m reading the call stack wrong or it’s an error in another file? Though I don’t think so.
How many elements are in your frames? And what is the value in your frame index?
An index out of range error means you are trying to access a position that is beyond the available indexes in the list.
Take for example an list with two items. If you tried to access index 2, you would get an out of range error because index 2 doesn’t exist. Only index 0 and 1 are valid (because that’s how it is stored in memory).
I maybe reading too much into the stack and the error could be else where. I may not be completely understanding how the code works. I mean, I understand, but maybe not completely.
I assumed the variables declared in the class were only in the class? Sorry if the terminology is wrong. But if one of the other python files were an issue, I’d assume it’d show up in the stack.