我正在创建一个游戏,当子弹(一个矩形)与落下的矩形相交时,矩形会掉落并且用户会在矩形处拍摄它从arraylist中删除。现在的问题是,当我快速点击我的鼠标并且子弹在飞行中它们相交但是除了最后一颗运动中的子弹之外不会移除任何形状。请有人帮我纠正这个错误。
//class that creates sprite & background
class CreateImage extends JPanel implements MouseListener{
ArrayList<fallShape> rect= new ArrayList<fallShape>();
ArrayList<fallShape1> rect1= new ArrayList<fallShape1>();
ArrayList<fallShape2> rect2= new ArrayList<fallShape2>();
ArrayList<Integer> by_poss = new ArrayList<>();
ArrayList<Integer> bx_poss_motion=new ArrayList<>();
int x_pos=mousework2.Width/2;
int y_pos=mousework2.Height-50;
int bx_pos=mousework2.Width/2;
int by_pos=mousework2.Height;
int y_speed=-15;
int x_speed=(int)Math.random()*10+1;
int score=0;
int scoreb=10;
String failedmess="YOU FAILED";
//createImage constructor
public CreateImage(){
super(true);
//Background image
ImageIcon pic=new ImageIcon("ballfall3.jpg");
//pixMage is a gloal variable of the outer class
pixMage=pic.getImage();
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(pixMage,0);
try {
tracker.waitForID(0);
}
catch (InterruptedException e)
{}
//pixMage=pixMage.getScaledInstance(200,-1,Image.SCALE_DEFAULT);
setPreferredSize(new Dimension(mousework2.Width,mousework2.Height));
for(int i=0;i<10;i++){
rect.add(i,new fallShape(12,12,rect));
}
for(int i=0;i<10;i++){
rect1.add(i,new fallShape1(12,12,rect1));
}
for(int i=0;i<10;i++){
rect2.add(i,new fallShape2(12,12,rect2));
}
Toolkit picx=Toolkit.getDefaultToolkit();
gunMage=picx.getImage("gunner.jpg");
//gunMage=gunMage.getScaledInstance(200,-1,Image.SCALE_SMOOTH);
addMouseListener(this);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e){
x_pos=e.getX()-5;
}
});
}
public void mouseClicked(MouseEvent e){
if(e.getButton()==1){
by_poss.add(mousework2.Height);
bx_pos=e.getX();
bx_poss_motion.add(bx_pos);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(pixMage,0,0,Width,Height,null);
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(gunMage,x_pos,y_pos+5,10,20,null);
g2.setColor(Color.BLACK);
Rectangle2D.Float bullet=new Rectangle2D.Float(bx_pos,by_pos+10,3,10);
for(int i = 0; i < by_poss.size(); i++){
by_poss.set(i, by_poss.get(i)+y_speed); //move the bullet
if(by_poss.get(i)>=mousework2.Height){
by_poss.clear();
}
bullet=new Rectangle2D.Float(bx_poss_motion.get(i),by_poss.get(i),3,10);
g2.fill(bullet);
//return;
//g2.draw(bullet);
}
g2.setColor(Color.RED);
for(fallShape2 b: rect2){
b.move();
g2.fill(b);
for(int i=0;i<10;i++){
if(bullet.intersects(b)){
rect2.remove(b);
click.play();
score+=scoreb;
}
}
if(b.y>=(mousework2.Height-30)){
gameOverMessage(g);
score=score;
}
}
for(fallShape1 b: rect1){
b.move();
g2.fill(b);
for(int i=0;i<10;i++){
if(bullet.intersects(b)){
rect1.remove(b);
click.play();
score+=scoreb;
}
}
if(b.y>=(mousework2.Height-30)){
gameOverMessage(g);
score=score;
}
}
for(fallShape b: rect){
b.move();
g2.fill(b);
for(int i=0;i<10;i++){
if(bullet.intersects(b)){
rect.remove(b);
click.play();
score+=scoreb;
}
}
if(b.y>=(mousework2.Height-30)){
gameOverMessage(g);
score=score;
//System.out.println(b.y);
}
}
g2.setColor(Color.BLACK);
g2.setFont(new Font("CALIFORNIAN FB",Font.BOLD,15));
g2.drawString((score+""),260,10);
if(new recMove(b).running==false){
g2.drawString("paused",150,250);
}
//boolean onStroke;
//g2.hit(bullet,rect,onStroke);
}