gpt4 book ai didi

java - libGDX foreach 创建对象吗?

转载 作者:行者123 更新时间:2023-11-30 08:05:54 25 4
gpt4 key购买 nike

我正在通过这些 g3d 教程学习:https://xoppa.github.io/blog/using-materials-with-libgdx/

现在我听说 foreach 循环创建了一个对象,这在渲染方法中使用是不好的。

示例:

private PerspectiveCamera cam;
private CameraInputController camController;
private Shader shader;
private Model model;
private Array<ModelInstance> instances = new Array<ModelInstance>();
private ModelBatch modelBatch;

@Override
public void show() {
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(2f, 2f, 2f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 300f;
cam.update();

camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);

ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createSphere(2f, 2f, 2f, 20, 20,
new Material(),
VertexAttributes.Usage.Position |
VertexAttributes.Usage.Normal |
VertexAttributes.Usage.TextureCoordinates);

for (int x = -5; x <= 5; x+=2) {
for (int z = -5; z <= 5; z+=2) {
instances.add(new ModelInstance(model, x, 0, z));
}
}

shader = new TestShader();
shader.init();

modelBatch = new ModelBatch();
}

@Override
public void render(float delta) {
camController.update();

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

modelBatch.begin(cam);
for (ModelInstance instance : instances) {
modelBatch.render(instance, shader);
}
modelBatch.end();

}
  1. 这是真的吗?它会创建一个对象吗?
  2. 解决方案是什么?

最佳答案

不,它不会创建对象。 for-each 循环只是一个语法快捷方式。请参阅Does the Java foreach loop create a new object?

关于java - libGDX foreach 创建对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31121892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com