2013-07-14 39 views
0

我有一点麻烦,我想知道你是否可以帮我修复它。Sprite组中的Sprite方法Pygame

所以我做了一个精灵并创建了一个空闲的动画方法,我打电话给__init__这样的方法。

class Player(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.attributes = "blah" 

     self.idleAnimation() 

    def idleAnimation(self): 
     self.animationCode = "Works normally I've checked it" 

player  = Player() 
playerGroup = pygame.sprite.Group() 
playerGroup.add(player) 
window = pygame.display.set_mode(yaddi-yadda) 

while StillLooping: 
    window.fill((0, 0, 0)) 
    playerGroup.update() 
    playerGroup.draw(window) 
    pygame.display.flip() 

但无论什么原因,idleAnimation方法不被该组中,尽管被称为在__init__方法运行。如果我稍后在循环中调用它:

while StillLooping: 
    player.idleAimation() 
    window.fill((0, 0, 0)) 
    playerGroup.update() 
    playerGroup.draw(window) 
    pygame.display.flip() 

它运行,但不以其他方式运行。我无法弄清楚为什么。任何想法将非常感谢!

回答

1

idleAnimation()方法并不神奇地由playerGroup.update()方法调用。我真的不明白,为什么你认为它应该是...

Group.update文档说,这要求每一个角色的update()方法,所以你如果你希望它被称为每一个应该重命名的方法update()循环。

+0

我不认为它应该是什么神奇,而是因为idleAnimation方法被调用,其中通常具有的自动运行的实例方法。但是,谢谢。 – LauraKellman

1

__init__方法被调用一次,当你实例化你的对象。所以当你创建你的对象时,你的idleAnimation()方法被调用,就是这样。

您的群组update()方法只会调用你的精灵的update方法,所以你需要或者重命名idleAnimation(),因为已经建议,或添加update()方法调用它,这应该证明更加灵活:

class Player(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.attributes = "blah" 

     self.idleAnimation() # You can probably get rid of this line 

    def idleAnimation(self): 
     self.animationCode = "Works normally I've checked it" 

    def update(self): 
     '''Will be called on each iteration of the main loop''' 
     self.idleAnimation() 

机会是你不需要调用idleAnimation()在你的初始化,因为它将你的循环中运行之后。