我試圖刪除創建的項目符號時(bullet.position.y < = 0)。但是遊戲給出了錯誤。我無法刪除創建的子彈。我該如何解決它?我使用一個紋理(64×64),這些都是我的課:
Java LibGDX刪除項目符號問題
的Game1(主類)
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();
}
}
球員
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();
}
}
子彈
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);
}
}
看起來這是造成'ConcurrentModificationException',這可以通過創建一個新的'ArrayList'加子彈的物體,如果他們需要被刪除,然後刪除所有的循環之後被避免。 – Orin