I have a mouse sprite that updates the x and y positions of the mouse each iteration of the game loop, but I am not quite sure what the error message found below: "'int' object has no attribute 'mouse_x', seems like its referring to 'self' as an int object, and 'mouse_x' to be its attribute?
import pygame
class mouse_sprite(pygame.sprite.Sprite):
def __init__(self, x, y, screen):
self.screen = screen
self.mouse_x = x
self.mouse_y = y
self.update_rect(self.mx, self.my)
def update_rect(self):
self.rect = pygame.Rect(self.mouse_x, self.mouse_y, 1, 1)
def update(self, x=None, y=None, screen=None):
**#ERROR: 'int' object has no attribute 'mouse_x' Line 24: self.mouse_x = x**
if x is not None:
self.mouse_x = x
if my is not None:
self.mouse_y = y
if screen is not None:
self.screen = screen
self.update_rect()
mouse = mouse_sprite
mx, my = pygame.mouse.get_pos()
mouse.update(mx, my)
You're not creating a new object. You're just calling class method like a static method
Instead of
mouse = mouse_sprite
do
object = mouse_sprite(...) # Pass init parameters
object.update(...) # Pass method parameters
When you call a method on created object, first parameter gets passed as self
. In your case, you've not created a new object so the first parameter mx
is getting assigned to self
which of type int. And it doesn't have field mouse_x
.