[英]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: 我宁愿建议这样的事情:
current
things, and one for future
things current
,另一个用于future
current
list current
列表 future
event, one should call a method such as addFuture(Entity whatever)
on the master GameEngine
object future
事件时,应在主GameEngine
对象上调用诸如addFuture(Entity whatever)
类的方法 current
list - then you clear current
and move all entries from future
into it current
列表-那么你明确current
和移动的所有项目future
进去 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”,并让
GameEngine
在whatNow()
更新后将其删除(我假设您的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.