2014-09-22 302 views
0

所以,我使用這個類在我的遊戲:我創建了這個類pygame.sprite.Sprite.kill()無法正常工作。爲什麼?

class Magazine(pygame.sprite.Sprite): 
    def __init__(self, image, shooter): 
     self.image = image 
     self.rect = image.get_rect() 
     self.rect.x = shooter.rect.x 
     self.rect.y = shooter.rect.y - shooter.image.get_height() 

    def update(self): 
     self.rect.y -= 5 
     if self.rect.y <= 0 - self.image.get_height(): 
      self.kill() 

後,我做了這個類的一個項目。然後我打電話的更新功能:

magazine = Magazine(magazineImage, forry) 
magazine.update() 

出於某種原因,我得到這個錯誤:

Traceback (most recent call last): 
    File "main.py", line 106, in <module> 
    main() 
    File "main.py", line 78, in main 
    projectileObject.update() 
    File "/Users/number1son100/Desktop/Famous Monsters Game/gameobjects.py", line 117, in update 
    self.kill() 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sprite.py", line 174, in kill 
    for c in self.__g.keys(): 
AttributeError: 'Magazine' object has no attribute '_Sprite__g' 

回答

1

好了,所有你需要做的是,如果你要使用附帶的功能精靈類,你首先必須插入:

super(spritename, self).init() 

因此,對於這個,你需要插入:

super(Magazine, self).init() 

進入你的init函數。

相關問題