简体   繁体   English

Java LibGDX删除项目符号问题

[英]Java LibGDX Remove Bullet Issue

I am trying to remove created bullets when (bullet.position.y <= 0). 我正在尝试在(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: 我使用了一种纹理(64 x 64),这些是我的类:

Game1 (Main Class) 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();
    }
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM