简体   繁体   English

pygame.sprite.Group()中的对象方法

[英]Object methods from pygame.sprite.Group()

I have a couple of sprite groups checking for collisions with each other and I want to access the method of one member of the sprite group as follows... 我有几个精灵组互相检查碰撞,我想按如下方式访问精灵组一个成员的方法...

for hit in pygame.sprite.groupcollide(badSprites, Missiles, 0, 1):
     hit.health(100)
     hit.printMessage("NO")

For some reason the hit.printYes method works but the hit.health one gives me a type error 由于某种原因, hit.printYes方法可以工作,但是hit.health却给我键入错误

TypeError: 'int' object is not callable TypeError:“ int”对象不可调用

Here's the method inside my colliding object... 这是我碰撞对象内的方法...

def health(self, damage):
    self.damage = damage
    self.health -= damage
    return self.health
def printMessage(self, message):
    print(message)

As you can see I just want to alter the health of the object when it collides with the missile but it appears I can only pass strings not numbers. 如您所见,当对象与导弹碰撞时,我只是想更改其运行状况,但是看来我只能传递字符串而不是数字。 I tried sending "100" as a string and then type casting inside health() but that didn't work either. 我尝试将“ 100”作为字符串发送,然后在health()内键入强制类型转换,但这也不起作用。 Perhaps my code is wrong elsewhere. 也许我的代码在其他地方是错误的。

您不能拥有与属性同名的方法,不能将方法更改为set_health或类似的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM