【问题标题】:libGdx Array removal causes texture to flickerlibGdx 数组移除导致纹理闪烁
【发布时间】:2015-05-28 22:02:53
【问题描述】:

我让我的相机沿 x 轴滚动,当我的地板对象离开屏幕时,我在屏幕边界之外添加一个新的地板对象并移除离开视图的旧地板对象。

当且仅当我移除对象时,它会导致该数组中的其他对象闪烁

这里是删除注释掉的代码。

batch.begin()

for(GameObject pillar: pillars) {
    pillar.draw(batch);     
    if(pillar.getBounds().getX() + 64 < camera.position.x - SCREEN_WIDTH/2) {
        //pillars.removeValue(pillar, false);
    }
}

batch.end()

这是另一个我也添加到数组中的示例

for(GameObject ceiling: ceilings) {
    ceiling.draw(batch, SCREEN_WIDTH, CEILING_HEIGHT);
    if((ceiling.getBounds().getX() + ceiling.getBounds().getWidth()) < (camera.position.x - SCREEN_WIDTH/2)) {
        ceilings.add(new GameObject(boundsTexture, ceiling.getBounds().getX() + (ceiling.getBounds().getWidth()*2),SCREEN_HEIGHT - CEILING_HEIGHT, false));
        //ceilings.removeValue(ceiling, false);
    }
}       

这是 GameObject 类

public class GameObject {
protected Vector2 location;
private Rectangle bounds;
protected Texture frame;
protected TextureRegion currentFrame;

GameObject(Texture texture, float xLocation, float yLocation, boolean flippedV) {
    frame = texture;
    location = new Vector2(xLocation, yLocation);
    bounds = new Rectangle( location.x, location.y,
                            frame.getWidth(),
                            frame.getHeight());

    currentFrame = new TextureRegion(frame, 0, 0, 64, 64);
    if(flippedV) {
        currentFrame.flip(false, true);
    }
}
public void draw(SpriteBatch batch) {
    batch.draw(currentFrame, location.x, location.y);
}
public void draw(SpriteBatch batch, float width, float height) {
    batch.draw(currentFrame, location.x, location.y, width, height);
}
public Rectangle getBounds() {
    return bounds;
}

}

我应该提到我正在使用 deltaTime 移动相机,没有其他移动, 相机是正交的。

camera.translate(300*delta,0);

我也试过

camera.translate((int)300*delta,0);

【问题讨论】:

  • 我修好了,我把添加和删除的代码从... batch.begin();批处理.end();
  • 在这里发布您的修复作为您问题的答案。这将帮助其他有类似问题的人。谢谢
  • 我这样做了,但我不能接受 2 天的答案
  • 感谢您发布答案

标签: java arrays libgdx textures flicker


【解决方案1】:

从 SpriteBatch 中删除添加和删除代码

batch.begin()
//remove the code that adds and removes from the Array
batch.end();
//put that code here

【讨论】:

    【解决方案2】:

    您不应该在迭代时从 List 中删除值。尝试将要删除的对象保存在另一个列表中,当 for 循环完成时,将它们从支柱列表中删除。

    您也可以尝试使用 Iterator 进行操作。

    【讨论】:

    • 所以我应该创建第二个数组/列表来添加/引用我想要删除的对象,然后遍历该列表并从原始列表中删除这些对象,我想我明白了。谢谢。
    • 是的,例如你可以保存对象所在的索引,然后通过索引删除它
    猜你喜欢
    • 2013-02-11
    • 2013-07-04
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多