简体   繁体   English

调用他的方法后如何删除对象?

[英]How can i remove object after calling his method?

I am doing a pc game. 我在做电脑游戏。 What I need is: after object Bomb make his attack I want to remove her from my ArrayList<Entity> . 我需要的是:在对象炸弹发动攻击后,我想将其从ArrayList<Entity>删除。

Entity is my abstract class for all enemies, like bomb, archer... I use ArrayList to indicate what I need to draw and update. Entity是所有敌人(例如炸弹,弓箭手)的抽象类。我使用ArrayList指示需要绘制和更新的内容。 If it's possible, I want to remove that object at all, not just from ArrayList . 如果有可能,我想删除所有对象,而不仅仅是从ArrayList删除。

Here is my code of GameEngine class: 这是我的GameEngine类的代码:

    public class GameEngine {
      gameEntities = new ArrayList<Entity>();
      Bomb bomb1;

      bomb1 = new Bomb();
      gameEntities.add(bomb1)
      public void update() {   
        for(Entity entity : gameEntities){
            entity.whatNow();
        }        
      }
    }

And other classes: 和其他类:

    public class Bomb extends Entity { 
      void whatNow() {
        GameData.entityRemove(this);
      }
    }

    public class GameData {
      static void entityRemove(Entity entity) {
        for(Entity listEntity : GameEngine.gameEntities){
            if(entity == listEntity){
                 listEntity = null;
                 GameEngine.gameEntities.remove(listEntity);
            }
      }
    }

An Entity in your context shouldn't directly access elements of the GameEngine . 您所处环境中的Entity不应直接访问GameEngine元素。 I would rather suggest something like this: 我宁愿建议这样的事情:

  • your engine has two lists: one for current things, and one for future things 您的引擎有两个列表:一个用于current ,另一个用于future
  • your code simply walks trough the current list 您的代码只是遍历current列表
  • when processing an entry leads to a future event, one should call a method such as addFuture(Entity whatever) on the master GameEngine object 处理条目导致future事件时,应在主GameEngine对象上调用诸如addFuture(Entity whatever)类的方法
  • when you are dong processing the current list - then you clear current and move all entries from future into it 当你洞处理current列表-那么你明确current移动的所有项目future进去
  • then you clear future and start processing current again 然后您清除future并再次开始处理current

In other words: avoid removing entries. 换句话说:避免删除条目。 Simply process a list once, throw it away and collect "new" entries within a second list. 只需处理一次列表,将其丢弃并收集第二个列表中的“新”条目。

And unrelated: read about the @Override annotation and start using it. 且无关:阅读@Override批注并开始使用它。

You should let the bombs mark themself as "toClean" and let the GameEngine remove them after the whatNow() updates (I assume that your GameEngine is a singleton): 您应该让炸弹将自己标记为“ toClean”,并让GameEnginewhatNow()更新后将其删除(我假设您的GameEngine是单例):

public class Bomb extends Entity { 
  void whatNow() {
    // do my bomb stuff
    GameEngine.instance().remove(this);
  }
}

public class GameEngine {

  // Declare a gameEntitiesToRemove queue/list

  public void update() {
    for(Entity entity : gameEntities){
       entity.whatNow();
    }
    while(gameEntitiesToRemove.hasNext()){
       gameEntities.remove(gameEntitiesToRemove.pop());
    }
  }

  public void remove(Entity entity) {
    this.gameEntitiesToRemove.add(entity);
  }
}

The problem is in the if-clause inside the for-loop: 问题出在for循环中的if子句中:

listEntity = null;
GameEngine.gameEntities.remove(listEntity);

The second line would remove the listEntity from the ArrayList, if listEntity would not be set to null in the first line. 如果第一行中的listEntity不设置为null,则第二行将从ArrayList中删除listEntity。 Now it tries to remove null from the list, which is likely not what you want. 现在,它尝试从列表中删除null,这可能不是您想要的。

At the very least, swap these two lines. 至少要交换这两行。 However there is really no point in setting listEntity to null, as a local variable it will be gone after that loop anyway. 但是,将listEntity设置为null确实没有意义,因为作为局部变量,无论如何它将在该循环之后消失。

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

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