[英]I got this error AttributeError: 'function' object has no attribute 'hlauncher' while trying to get a attribute from another file
So I have the error:所以我有错误:
launchercheck = game.update.hlauncher()
AttributeError: 'function' object has no attribute 'hlauncher'
When I want to press spacebar to shoot my weapon, it worked before I added this in the code:当我想按空格键来射击我的武器时,它在我在代码中添加之前起作用了:
launchercheck = game.update.hlauncher()
if launchercheck is True:
self.kill()
The self.kill()
is just there to test if my If
function works. self.kill()
只是为了测试我的If
函数是否有效。
game.update.hlauncher()
refers to my main.py
file there it should look in the class 'game' for 'update' and within 'update' it should grab hlauncher hlauncher should change from False to True upon the player picking up the homing launcher. game.update.hlauncher()
指的是我的main.py
文件,它应该在“游戏”类中查找“更新”,在“更新”中它应该抓取 hlauncher hlauncher 应该在玩家拿起游戏时从 False 变为 True归位发射器。 Here's update within game:这是游戏内的更新:
def update(self):
# update portion of the game loop
self.all_sprites.update()
self.camera.update(self.player)
# game over
if len(self.mobs) == 0:
self.playing = False
# player hits items
hlauncher = False
hits = pg.sprite.spritecollide(self.player, self.items, False)
for hit in hits:
if hit.type == 'health' and self.player.health < PLAYER_HEALTH:
hit.kill()
self.effects_sounds['health_up'].play()
self.player.add_health(HEALTH_PACK_AMOUNT)
if hit.type == 'pistol':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'pistol'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
if hit.type == 'shotgun':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'shotgun'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
if hit.type == 'rifle':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'rifle'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
if hit.type == 'sniperrifle':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'sniperrifle'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
if hit.type == 'launcher':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'launcher'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
if hit.type == 'hominglauncher':
hit.kill()
self.effects_sounds['gun_pickup'].play()
self.player.weapon = 'hominglauncher'
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
hlauncher = True
# mobs hit player
hits = pg.sprite.spritecollide(self.player, self.mobs, False, collide_hit_rect)
for hit in hits:
if random() < 0.7:
choice(self.player_hit_sounds).play()
self.player.health -= MOB_DAMAGE
hit.vel = vec(0, 0)
if self.player.health <= 0:
self.playing = False
if hits:
self.player.hit()
self.player.pos += vec(MOB_KNOCKBACK, 0).rotate(-hits[0].rot)
# bullets hit mobs
hits = pg.sprite.groupcollide(self.mobs, self.bullets, False, True)
for mob in hits:
for bullet in hits[mob]:
mob.health -= bullet.damage
mob.vel = vec(0, 0)
I already tried putting () behind True and False in the main file which gave the same error and I already fiddled around with game.update.hlauncher() which should look at hlauncher each time I try to shoot to confirm that I'm holding a hominglauncher and vice versa.我已经尝试将 () 放在主文件中的 True 和 False 后面,这给出了相同的错误,并且我已经摆弄了 game.update.hlauncher() 每次我尝试射击时都应该查看 hlauncher 以确认我正在持有一个 hominglauncher,反之亦然。
Thanks for your help!谢谢你的帮助!
If update
was an instance of a class, and hlauncher
was a member-function of that class, then your syntax would be correct.如果
update
是一个类的实例,而hlauncher
是该类的成员函数,那么您的语法就是正确的。
But hlauncher
is a local(?) variable in the function update()
.但是
hlauncher
是函数update()
中的本地(?) 变量。 It could also be a global variable, but it's not possible to know from the code.它也可能是一个全局变量,但无法从代码中得知。 Assuming it's local, there is no way to access it from outside the update function, because it only exists while this function is executing - that's what it means to be locally-defined.
假设它是本地的,则无法从更新函数外部访问它,因为它仅在此函数执行时存在 - 这就是本地定义的含义。 If
hlauncher
is globally defined, at any time it can be set: hlauncher = True
.如果
hlauncher
是全局定义的,则可以随时设置: hlauncher = True
。
But it would probably be best to add this functionality to the player class:但最好将此功能添加到播放器类:
class Player:
def __init__( self ):
pygame.sprite.Sprite.__init__(self)
... # other stuff
self.has_hlauncher = False
self.hlauncher_ammo = 0
def addHLauncher( self ):
self.has_hlauncher = True
self.hlauncher_ammo += 10
Then the code could call:然后代码可以调用:
self.player.addHLauncher()
If you're having difficulty with all this, perhaps have a quick read of how variable scope works in Python.如果您对这一切有困难,不妨快速阅读一下 Python 中变量作用域的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.