How to make all my bullets remove intersected objects

爱⌒轻易说出口 提交于 2019-12-08 10:26:30

问题


I am creating a game were rectangles fall and user shoots at the rectangles when the bullet(a rectangle) intersects the falling rectangle it is removed from the arraylist. The problem now is that when I rapidly click my mouse and the bullets are on the fly they intersect but do not remove any shape except for the last bullet in motion. Please can someone help me correct this error .

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

}

回答1:


I Finally did this by adding my bullet to another arraylist, then getting bullets from the arraylist when testing for intersection.

 ArrayList<Rectangle2D.Float> bulletStore=new ArrayList<>();
        Rectangle2D.Float bullet;//=new Rectangle2D.Float(bx_pos,by_pos+10,3,10);

      for(int j=0;j<by_poss.size();j++){
        by_poss.set(j, by_poss.get(j)+y_speed); //move the bullet
         bullet=new Rectangle2D.Float(bx_poss_motion.get(j),by_poss.get(j),3,10);
      //Rectangle2D.Float bulletOu=new Rectangle2D.Float(bx_pos,by_pos+10,3,10);

            g2.fill(bullet);
       bulletStore.add(bullet);
            //g2.draw(bullet);
   }



 if(b.intersects(bulletStore.get(j))){


来源:https://stackoverflow.com/questions/31776720/how-to-make-all-my-bullets-remove-intersected-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!