我将一个对象传递给这样的类:
public Class Monster{
public float x;
public float y;
public Area area;
public Monster(float x,float y,Area area){
thix.x=x;
this.y=y;
this.area=area;
}
}
在这种情况下,我会将一个区域传递给怪物,并保存参考 在声明的"区域"。
Area是存储关于关卡的所有信息的关卡类, 还有很多ArrayLists,包含怪物本身。
我知道理想情况下怪物不需要知道它在哪里,因为 关卡课应该处理所有事情。
我的问题是关于效率。 我读到参考费用为4-8个字节。但是是对ArrayList的引用, 或者包含多个ArrayList的Object仍然是一个4字节的引用?
The Level将为每个怪物调用public void update(), 目前怪物会在更新或碰撞时执行此操作:
public void spawnParticles(){
area.getTheRightParticleSystem.add(particleEmitter);
}
或
public void shoot(){
area.getTheRightList.add(projectile);
}
以及使用参考区域的其他方法。
这会耗费大量内存吗? 我能想到的另一种方式是制作一个对象" LevelCommand" 并将Monster的更新方法更改为" public LevelCommand update()" 但是我要再添加
public ArrayList<LevelCommand> newCommands;
然后使用这样的更新:
public LevelCommand update(){
[...]
newCommands.add(command);
[...]
newCommands.add(command2);
[...]
return newCommands;
}
最后,我的问题归结为:
是否使用&#34;区域进行引用。&#34; 昂贵的,并且不适合&#34;使用返回方法的方式更贵?
如果我以错误的方式提出问题,或者我可以在某处读到这个问题,我很抱歉。这是我第一次来这里。