Java LibGDX删除子弹问题

时间:2016-08-30 17:13:46

标签: java android libgdx

我正在尝试删除创建的项目符号(bullet.position.y< = 0)。但游戏给出了错误。我无法删除创建的项目符号。我该如何解决?我使用了一个纹理(64 x 64),这些是我的类:

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);
    }
}

1 个答案:

答案 0 :(得分:3)

迭代时无法从列表中删除元素。要在迭代时删除元素,您需要标记要删除的项目符号,并在迭代后实际从主列表中删除所有标记的项目符号。

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);