Reputation: 557
First of all, I am aware some questions were asked similar to this but they didn't help me out.
I am trying to call a list in the same class but in a different method, it should work but it somehow doesn't.
The error:
'Player' object has no attribute 'standing_frames'
Where the list is defined:
self.standing_frames = [self.game.spriteheet.get_images(0, 0, 19, 34),
self.game.spriteheet.get_images(19, 0, 19, 34),
self.game.spriteheet.get_images(38, 0, 19, 34),
self.game.spriteheet.get_images(57, 0, 19, 34),
self.game.spriteheet.get_images(76, 0, 19, 34),
self.game.spriteheet.get_images(95, 0, 19, 34),
self.game.spriteheet.get_images(114, 0, 19, 34),
self.game.spriteheet.get_images(133, 0, 19, 34),
self.game.spriteheet.get_images(152, 0, 19, 34),
self.game.spriteheet.get_images(171, 0, 19, 34),
self.game.spriteheet.get_images(190, 0, 19, 34),
self.game.spriteheet.get_images(209, 0, 19, 34)]
Where the list is first called and where the error first come up:
self.image = self.standing_frames[0]
All the pieces are in different methods but in the same class. The spritesheet is a method where it separates a picture by the co-ordinates and the width and height, the image is already defined.
mcve and full code - Github
Thanks for the help :)
Upvotes: 1
Views: 44
Reputation: 20448
You never call self.load_images()
in the Player
class, so the self.standing_frames
attribute never gets assigned and the game crashes when you reference it the first time.
self.load_images() # Call this before you use the `self.standing_frames` attribute.
self.image = self.standing_frames[0]
You also have to define a self.spritesheet
attribute in the Game
class.
self.spritesheet = Spritesheet('idle.png') # Pass the filename.
Change the __init__
method of the Spritesheet
like so if you want to create different sheets:
class Spritesheet:
def __init__(self, filename):
self.spritesheet = pg.image.load(os.path.join(animation_folder, filename)).convert()
The get_image
method could be beautified a bit with pygame.Surface.subsurface
.
It would also be possible to create all subsurfaces at the time of the instantiation, then you don't have to create a new surface every time an image is switched.
And there are two typos: spriteheet
instead of spritesheet
and .get_images
instead of .get_image
.
Upvotes: 2