简体   繁体   English

sprite.Group()中的对象不具有应有的唯一对象

[英]objects in a sprite.Group() not behaving as unique objects as they should

Below is the troubling code with a quick breakdown. 以下是快速细分的令人烦恼的代码。

if player.attacked == True and delay >= 60: #if it's monster's turn plus a built in delay so everything isn't instant
    for enemy in enemies_list: #iterating the all the enemies in the sprite group
        if enemy.attack_rdy == True and enemy.health >0 and delay ==60: #if THAT particular monster hasn't attacked, isn't dead, and the delay has been long enough.
            player.hit.play() #play the player's damaged sound
            damage = random.randrange(each.attack[0],each.attack[1]+1)-player.armor #random attack roll of a tuple assigned to the enemy class
            player.health -= damage 
            enemy.attack_rdy = False #Sets THIS particular enemy so that he may not attack until it's his turn again
            player.damaged = True #Just triggers an animation on the player
            each.attack_anim = 1 #triggers an animation on the enemy
            break #stops the for loop so that everything doesn't happen at once and relies on the delay int.

Problem: This iteration works properly in that the player is accurately attacked the correct number of times in accordance to the number of enemies attacking him. 问题:此迭代正常进行,因为根据攻击他的敌人数量,可以正确地对玩家进行正确的攻击次数。 For some weird reason the same enemy will be attacking all those times however. 出于某些奇怪的原因,同一个敌人将在所有这些时间进攻。 When the attacking enemy dies, another one simply takes over his task of being the one who attacks. 当攻击的敌人死亡时,另一人只是接管了自己的任务,即作为攻击者。 I can make no sense of it. 我没有任何意义。

In true bonehead fashion, after staring at and editing my code for hours I find my blunder mere moments after posting. 用真正的骨头方式,盯着我的代码并编辑了好几个小时之后,我发现发布后的片刻只是我的失误。 It's really just a stupid error. 这实际上只是一个愚蠢的错误。

each.attack_anim = 1

SHOULD read 应该读

enemy.attack_anim = 1

With blunder corrected, code runs as designed. 纠正错误之后,代码将按设计运行。

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

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