Java 2D游戏-为适当实体呈现攻击视觉效果

Java 2D游戏-为适当实体呈现攻击视觉效果,java,animation,2d,2d-games,Java,Animation,2d,2d Games,我有一个关于在Java2D游戏中呈现单个实体的特定状态的问题 完整的代码在,但我将在这里列出最重要的部分 在我的Player类中,我有以下布尔变量: // Checks if entity is hurt by player public static boolean isHurt = false; 在checkAttacks()方法中,isHurt设置为true for (Entity e : handler.getWorld().getEntityManager().getEntities

我有一个关于在Java2D游戏中呈现单个实体的特定状态的问题

完整的代码在,但我将在这里列出最重要的部分

在我的Player类中,我有以下布尔变量:

// Checks if entity is hurt by player
public static boolean isHurt = false;
checkAttacks()
方法中,
isHurt
设置为
true

for (Entity e : handler.getWorld().getEntityManager().getEntities()) {
        if (e.equals(this)) {
           // Check if Entity is player 
           // So the player doesnt hurt himself when attacking
            continue; 
        }
        if (e.getCollisionBounds(0,0).intersects(aRectangle)) {
            isHurt = true;
            e.hurt(1);
            return;
        }
    }
在我的树类中,
tick()
方法如下:

@Override
public void tick() {
    if (Player.isHurt) {
        getCurrentAnimationFrame();
        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                Player.isHurt = false;
            }
        },180);
    }

}
它调用这个方法:

private BufferedImage getCurrentAnimationFrame() {   
       if (Player.isHurt) {  
          return treeHurtTwo.getCurrentFrame();
       } else {
           return treeTwo.getCurrentFrame();
       }

    } 
由于
Timer
TimerTask
的原因,“受伤”状态动画会正确返回并在屏幕上停留足够长的时间,但问题是它会在所有树上呈现受伤状态,而不是在玩家攻击的树上

如何使其仅在玩家当前攻击的实体上呈现伤害状态

编辑: 游戏世界中的实体由
EntityManager
类管理,该类将实体存储在ArrayList结构中

 public ArrayList<Entity> entities;

“完整代码位于..//github…”大多数人不会关注链接-所有相关代码都应该在问题中“但我会在这里列出最重要的部分”。&我所说的“相关”是指一个链接。在问题中添加一个MCVE。问题是它是相互关联的,我不能创建MCVE,因为这意味着我的游戏中至少有几个类包含在问题中。如果我这样做的话,我的代码将超过300行,我想也不会有人想读它。“这意味着我的游戏中至少有几个类”额外的类可以包含在MCVE中。只是唯一的
public
类必须有
main(…)
方法。“代码将超过300行”我打赌这个问题可以用更少的语言来表达。我还打赌,短时间的MCVE,不会引起太多关注。顺便说一句-是否使用Swing或AWT组件?Swing和AWT组件都使用,但主要使用AWT。我使用
instanceof
操作符对此进行了修补,但它只在我的ArrayList中有不同的类实体时才能正常工作。我在考虑在实体类的构造函数中添加一个
intuid
字段,这样每个实体在游戏世界中都必须有一个唯一的ID。然后,我获取
for(){}
循环中每个实体的
uID
,并在该实体的
tick()
方法的
if
条件中检查它。
entityManager.addEntity(new Tree(handler,100,200));
entityManager.addEntity(new Tree(handler,200,200));
entityManager.addEntity(new Tree(handler,350,200));
entityManager.addEntity(new Rock(handler,400,450));