我让我的相机沿着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);
答案 0 :(得分:0)
从SpriteBatch中删除添加和删除代码
batch.begin()
//remove the code that adds and removes from the Array
batch.end();
//put that code here
答案 1 :(得分:0)
迭代时,不应从List中删除值。尝试在另一个列表中保存要删除的对象,并在for循环结束时将其从支柱列表中删除。
您也可以尝试使用Iterator。