2016-08-30 11 views
0

(bullet.position.y < = 0)のときに作成した箇条書きを削除しようとしています。しかし、ゲームはエラーを与える。私は作成された箇条書きを削除することはできません。どうすれば修正できますか?私は1つのテクスチャ(64×64)を使用し、これらは私のクラスである:
Java LibGDX Bullet Issueを削除する

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

これは、彼らが削除する必要がある場合は、箇条書きのオブジェクトを追加し、ループの後、すべてを削除するために新しい 'ArrayList'を作成することによって回避することができ、' ConcurrentModificationException'を引き起こしているように思えます。 – Orin

答えて

2

それを繰り返しているときにリストから要素を削除しないでください。反復処理時に要素を削除するには、削除する箇条書きをマークする必要があります。反復処理を実行すると、マークされた箇条書きがメインリストから実際に削除されます。

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

beetwen bullets collisionをチェックするにはどうしたらいいですか?私は弾丸配列で他の弾丸を取得する必要があります。私は2つの弾丸が衝突するときに破壊したい。 –

+1

これは別の質問ですが、libGDX Intersectorクラスをチェックします。 https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html箇条書きを「rectaps」に設定し、「overlaps」メソッドを使用するのが適切な解決策です。 – pr0gramist

関連する問題