我有点小麻烦,不知道你能不能帮我修一下。
所以我做了一个精灵,并创建了一个空闲动画方法,我在__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()但是,不管出于什么原因,尽管在__init__方法中调用了idleAnimation方法,但它并没有在组中运行。如果我稍后在循环中调用它:
while StillLooping:
player.idleAimation()
window.fill((0, 0, 0))
playerGroup.update()
playerGroup.draw(window)
pygame.display.flip()它可以运行,但不能以其他方式运行。我不知道为什么。任何想法都将是伟大的感谢!
发布于 2013-07-14 20:22:28
playerGroup.update()方法不能神奇地调用idleAnimation()方法。我真的不明白你为什么认为应该是..。
Group.update的文档说这会调用每个sprite的update()方法,所以如果您希望每次循环都调用该方法,则应该将该方法重命名为update()。
发布于 2013-07-14 22:29:11
__init__方法仅在实例化对象时调用一次。因此,在创建对象时会调用idleAnimation()方法,仅此而已。
您的组的update()方法将只调用您的sprite的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(),因为它会在你的循环中运行。
https://stackoverflow.com/questions/17639142
复制相似问题