Peter Parker Peter Parker - 3 months ago 14
Java Question

Java LibGDX Remove Bullet Issue

I am trying to remove created bullets when (bullet.position.y <= 0). But the game gives error. I can not remove created bullets. How can I fix it? I used one texture ( 64 x 64 ) and these are my classes:


Game1 (Main Class)

package com.outlook.anil136.game1;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import java.util.ArrayList;

public class Game1 extends ApplicationAdapter {
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
private OrthographicCamera camera;
private Texture blueTexture;
private Player player1;
private ArrayList<Bullet> bullets;
private Sprite blueSprite;

@Override
public void create () {
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
blueTexture = new Texture("BlueRectangle.png");
player1 = new Player(blueTexture, new Vector2(240, 600));
bullets = new ArrayList<Bullet>();
blueSprite = new Sprite(blueTexture);
}

@Override
public void render () {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rectLine(0, 401, 480, 401, 2);
shapeRenderer.end();
batch.setProjectionMatrix(camera.combined);
batch.begin();
player1.draw(batch);
for (Bullet bullet : bullets) {
bullet.draw(batch);
bullet.update(Gdx.graphics.getDeltaTime());
if (bullet.position.y <= 0)
bullet.remove(bullets);
}
blueSprite.setPosition(416, 736);
blueSprite.setColor(1, 1, 1, 0.5f);
blueSprite.draw(batch);
batch.end();
player1.update(Gdx.graphics.getDeltaTime());
for (int i = 0; i < 20; i++) {
Vector3 touchPosition = camera.unproject(new Vector3(Gdx.input.getX(i), Gdx.input.getY(i), 0));
if (Gdx.input.isTouched(i) && touchPosition.y >= 401 && touchPosition.y > 64 && !new Rectangle(416, 736, 64, 64).contains(touchPosition.x, touchPosition.y))
player1.touchPosition = touchPosition;
if (Gdx.input.justTouched() && new Rectangle(416, 736, 64, 64).contains(touchPosition.x, touchPosition.y))
bullets.add(new Bullet(blueTexture, new Vector2(player1.position), -player1.speed));
}
if (player1.position.y + 32 > 800)
player1.position.y = 768;
else if (player1.position.y - 32 < 402)
player1.position.y = 434;
}

@Override
public void dispose () {
shapeRenderer.dispose();
batch.dispose();
blueTexture.dispose();
}
}


Player

package com.outlook.anil136.game1;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import java.util.ArrayList;

import javafx.scene.input.KeyCode;
import sun.security.provider.SHA;

public class Game1 extends ApplicationAdapter {
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
private OrthographicCamera camera;
private Texture blueTexture;
private Player player1;
public static ArrayList<Bullet> bullets;
private Sprite blueSprite;

@Override
public void create () {
shapeRenderer = new ShapeRenderer();
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
blueTexture = new Texture("BlueRectangle.png");
player1 = new Player(blueTexture, new Vector2(240, 600));
bullets = new ArrayList<Bullet>();
blueSprite = new Sprite(blueTexture);
}

@Override
public void render () {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.rectLine(0, 401, 480, 401, 2);
shapeRenderer.end();
batch.setProjectionMatrix(camera.combined);
batch.begin();
player1.draw(batch);
for (Bullet bullet : bullets) {
bullet.draw(batch);
bullet.update(Gdx.graphics.getDeltaTime());
if (bullet.position.y <= 0)
bullet.remove(bullets);
}
blueSprite.setPosition(416, 736);
blueSprite.setColor(1, 1, 1, 0.5f);
blueSprite.draw(batch);
batch.end();
player1.update(Gdx.graphics.getDeltaTime());
for (int i = 0; i < 20; i++) {
Vector3 touchPosition = camera.unproject(new Vector3(Gdx.input.getX(i), Gdx.input.getY(i), 0));
if (Gdx.input.isTouched(i) && touchPosition.y >= 401 && touchPosition.y > 64 && !new Rectangle(416, 736, 64, 64).contains(touchPosition.x, touchPosition.y))
player1.touchPosition = touchPosition;
if (Gdx.input.justTouched() && new Rectangle(416, 736, 64, 64).contains(touchPosition.x, touchPosition.y))
bullets.add(new Bullet(blueTexture, new Vector2(player1.position), -player1.speed));
}
if (player1.position.y + 32 > 800)
player1.position.y = 768;
else if (player1.position.y - 32 < 402)
player1.position.y = 434;
}

@Override
public void dispose () {
shapeRenderer.dispose();
batch.dispose();
blueTexture.dispose();
}
}


Bullet

package com.outlook.anil136.game1;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

import java.util.ArrayList;

public class Bullet {
private Texture texture;
Vector2 position;
private int speed;

public Bullet(Texture texture, Vector2 position, int speed) {
this.texture = texture;
this.position = position;
this.speed = speed;
}

public void draw(SpriteBatch batch) {
batch.draw(texture, position.x - 16, position.y - 16, 32, 32);
}

public void update(float deltaTime) {
position.y += speed;
}

public void remove(ArrayList<Bullet> bullets) {
bullets.remove(this);
}
}

Answer

You can't remove elements from list when you are iterating it. To remove elements when iterating you need to mark bullet to remove and after iterating actually remove all marked bullets from the main list.

List<Bullet> bulletsToRemove = new ArrayList<Bullet>();
for (Bullet bullet : bullets) {
    bullet.draw(batch);
    bullet.update(Gdx.graphics.getDeltaTime());
    if (bullet.position.y <= 0)
        bulletsToRemove.add(bullet);
}
bullets.removeAll(bulletsToRemove);