简体   繁体   English

如何使我的所有子弹删除相交的对象

[英]How to make all my bullets remove intersected objects

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

}

I Finally did this by adding my bullet to another arraylist, then getting bullets from the arraylist when testing for intersection. 最后,我将子弹添加到另一个arraylist中,然后在测试相交时从arraylist中获得了子弹,从而完成了此操作。

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

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

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