2017-10-21 60 views
0
class GameObject: 
def __init__(self, x=0, y=0, width=0, high=0, imName="", imAlpha=False): 
    self.x = x # on screen 
    self.y = y 
    self.width = width 
    self.high = high 
    self.imageName = imName 
    self.imageAlpha = imAlpha 
    self.image 

def loadImage(self): 
    self.image = pygame.image.load(self.imageName) 
    self.image = pygame.transform.scale(self.image, (self.width, self.high)) 
    if self.imageAlpha == True: 
     self.image = self.image.convert_alpha() 
    else: 
     self.image = self.image.convert() 


pygame.init() 
player = GameObject(px0, py0, width, high, "player.png", True) 
player.loadImage() 

任何想法,为什么我有以下错误?AttributeError:GameObject实例没有属性 - PyGame

AttributeError: GameObject instance has no attribute 'image'

+0

'__init__'中单独使用'self.image'有什么意义?如果你想声明,可以用'self.image = None'来代替 – PRMoureu

回答

0

这样做是不好的做法,但不需要在构造函数中声明对象属性(init)。当你写self.image时,它试图访问一个尚不存在的属性。 我建议做self.image =没有,直到你加载它,但完全删除该行也将工作。

+0

You are write!非常感谢你! – Nebula

0

__init__最后一行,也就是当你定义player调用,您试图访问self.image,它不存在。

相关问题