不使用ArrayList Java 2D平台进行拍摄

不使用ArrayList Java 2D平台进行拍摄,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,我从来没有尝试过在任何类型的游戏中实现射击,当我尝试删除ArrayList的元素时,我正在使用它抛出和索引越界异常。我希望能够按下回车键,存储玩家面对的当前方向,然后在当前方向上发射一枚炮弹。然后,一旦投射物到达其目的地,将其自身从ArrayList中移除,并且不再进行渲染。但是,当它到达映射的末尾(x==map.mapSizeX)时,项目符号冻结,此时抛出异常(线程“AWT-EventQueue-0”java.lang.IndexOutOfBoundsException中的异常:索引:1,大小

我从来没有尝试过在任何类型的游戏中实现射击,当我尝试删除ArrayList的元素时,我正在使用它抛出和索引越界异常。我希望能够按下回车键,存储玩家面对的当前方向,然后在当前方向上发射一枚炮弹。然后,一旦投射物到达其目的地,将其自身从ArrayList中移除,并且不再进行渲染。但是,当它到达映射的末尾(x==map.mapSizeX)时,项目符号冻结,此时抛出异常(线程“AWT-EventQueue-0”java.lang.IndexOutOfBoundsException中的异常:索引:1,大小:1)

射击类的代码(sparket.java) Player类的代码 ContentPane类的代码(所有呈现都发生在该类中)
打包引擎;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入javax.swing.JPanel;
@SuppressWarnings({“串行”、“静态访问”})
公共类ContentPane扩展了JPanel{
私有映射=新映射();
公共玩家=新玩家();
公共内容窗格(){
}
公共组件(图形g){
图形2d g2=(图形2d)g;
超级组件(g2);
map.renderMap(true,g2);
player.renderPlayer(true,g2);
如果(player.bullet.bullets.size()>0){
player.bullet.updatePos();
}
对于(int i=0;i
异常具体在哪里引发?堆栈跟踪是什么样子的?你能试着把代码简化成一个简短但完整的程序来演示这个问题吗?
Map
是你自己的一个类吗?看看
newmap()
将如何为您提供编译。而且,当人们在帮助你解决问题时,我认真地认为你需要研究依赖注入。我认为维基百科甚至有一篇关于它的好文章。您过度使用了
静态
字段,主要是因为错误的原因。
package Engine;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;

public class Projectile {

public static ArrayList<Integer> bullets = new ArrayList<Integer>(3);
private static DirectionalVector dir = new DirectionalVector();
private static int index = 0;
private static int x,y;

public Projectile(int direction, int x, int y){
    dir.applyDirection(direction);
    bullets.add(index);
    index += 1;
    this.x = x;
    this.y = y;
}

public void updatePos(){
    if (dir.right){
        x += 1;
        dir.left = false;
        dir.down = false;
    }
    if (dir.left){
        x -= 1;
        dir.right = false;
        dir.down = false;
    }
    if (dir.down){
        y += 1;
        dir.left = false;
        dir.right = false;
    }
    if (dir.down){
        System.out.println("DOWN");
    }
    if (dir.right){
        System.out.println("RIGHT");
    }
    if (dir.left){
        System.out.println("LEFT");
    }

    if (x > Map.mapSizeX){
        bullets.remove(index);
    }

    if (x < 0){
        bullets.remove(index);
    }

    if (y > Map.mapSizeY){
        bullets.remove(index);
    }

    if (y < 0){
        bullets.remove(index);
    }
}

public static void render(Graphics2D g2){
    int r = new Random().nextInt(256);
    int g = new Random().nextInt(256);
    int b = new Random().nextInt(256);
    if (r < 50){
        r = new Random().nextInt(256);
    }
    if (g < 50){
        g = new Random().nextInt(256);
    }
    if (b < 50){
        b = new Random().nextInt(256);
    }
    g2.setColor(new Color(r,g,b));
    g2.fillRect(x * Map.tileSize, y * Map.tileSize, Map.tileSize, Map.tileSize);
}
package Engine;

public class DirectionalVector {

public boolean left = false, right = true, down = false;

public DirectionalVector(){

}

public void applyDirection(int id){
    if (id == 0){
        right = true;
    }
    if (id == 1){
        left = true;
    }
    if (id == 2){
        down = true;
    }
}

}
package Engine;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Player implements KeyListener, Runnable{
public static int x = 0,y = 0;
private static boolean right = false, left = false, falling = false, jumping = false;
private Thread thread = new Thread(this, "GAME LOOP");
private static int lastDirectionPressed = 0;
public static Projectile bullet;

public Player(){
    thread.start();
}

public void renderPlayer(boolean value, Graphics2D g2){
    if (value){
        g2.setColor(Color.blue);
        g2.fillRect(x * Map.tileSize, y * Map.tileSize, Map.tileSize, Map.tileSize);
    }
}

@Override
public void keyPressed(KeyEvent e) {

    int c = e.getKeyCode();

    if (c == KeyEvent.VK_RIGHT && right){
        x += 1;
        lastDirectionPressed = 0;
    }

    if (c == KeyEvent.VK_LEFT && left){
        x -= 1;
        lastDirectionPressed = 1;
    }

    if (c == KeyEvent.VK_SPACE && jumping){
        y -= 1;
    }

    if (c == KeyEvent.VK_ENTER){
        bullet = new Projectile(lastDirectionPressed, x, y);
    }
}

public void update(){
    if (x != Map.mapSizeX){
        right = Map.getTile(x + 1, y) == 0;
    }

    if (x > 0){
        left = Map.getTile(x - 1, y) == 0;
    }

    if (x == 0){
        left = false;
    }

    if (x == Map.mapSizeX){
        right = false;
    }

    if (y != Map.mapSizeY){
        falling = Map.getTile(x, y + 1) == 0;
    }

    if (y == Map.mapSizeY){
        falling = false;
    }

    if (falling){
        y += 1;
    }
}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void run(){
    while (true){
        try{
            Thread.sleep(100);
            update();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

    }
}

}
package Engine;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

@SuppressWarnings({"serial", "static-access"})
public class ContentPane extends JPanel{

    private Map map = new Map();
    public Player player = new Player();

    public ContentPane(){

    }

    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;
        super.paintComponent(g2);
        map.renderMap(true, g2);
        player.renderPlayer(true, g2);
        if (player.bullet.bullets.size() > 0){
            player.bullet.updatePos();
        }
        for (int i = 0; i < player.bullet.bullets.size(); i++){
            player.bullet.render(g2);
        }

        System.out.println(player.bullet.bullets.size());
        repaint();
    }

}