简体   繁体   English

我收到此错误 AttributeError: 'function' object has no attribute 'hlauncher' while试图从另一个文件中获取属性

[英]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.

相关问题 使用 python-barcode 尝试生成条形码并收到错误消息:AttributeError: 'function' object has no attribute 'get' - Using python-barcode trying to generate barcode and got error message: AttributeError: 'function' object has no attribute 'get' 得到 AttributeError: &#39;module&#39; 对象在从另一个文件导入全局变量时没有属性 - got AttributeError: 'module' object has no attribute while importing global variable from another file 在设置视图集时,出现错误 AttributeError: &#39;function&#39; object has no attribute &#39;get_extra_actions&#39; - In setting viewset, I got an error AttributeError: 'function' object has no attribute 'get_extra_actions' 我正在尝试让 elasticsearch 和 flask 工作。 这是错误 AttributeError: 'function' object has no attribute 'elasticsearch' - I am trying to get elasticsearch and flask to work. Here is the error AttributeError: 'function' object has no attribute 'elasticsearch' 我收到这个错误(AttributeError: &#39;bytes&#39; object has no attribute &#39;read&#39;)这个程序从这个站点收集信息 - I got this error(AttributeError: 'bytes' object has no attribute 'read') This program collects information from this site 尝试使用 Django 设置 SendGrid 的电子邮件 API 时出现此错误 - AttributeError: &#39;str&#39; object has no attribute &#39;get&#39; - Getting this error while trying to set SendGrid's email API with Django - AttributeError: 'str' object has no attribute 'get' AttributeError: &#39;list&#39; 对象没有属性 &#39;SeqRecord&#39; - 尝试从 fasta 文件中使用 Biopython&gt;SeqIO 切片多个序列 - AttributeError: 'list' object has no attribute 'SeqRecord' - while trying to slice multiple sequences with Biopython>SeqIO from fasta file AttributeError: 'function' object 在尝试从 function 访问变量时没有属性错误 - AttributeError: 'function' object has no attribute error when trying to access a variable from a function AttributeError:&#39;tuple&#39;对象在写入文件时没有属性&#39;write&#39;错误 - AttributeError: 'tuple' object has no attribute 'write' error while writing into a file 在尝试使用 pd.df.replace 时,我得到: AttributeError: 'DataFrame' object has no attribute 'str' - While trying to use pd.df.replace, i get: AttributeError: 'DataFrame' object has no attribute 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM