从鼠标单击中分离运动中的子弹

时间:2015-07-27 08:29:44

标签: java 2d

请问如何将已击中的子弹与鼠标滑动的水平位置分开,这样当我点击鼠标时,运动中的子弹不会移动。 此外,当我拍摄所有形状时,我需要创建一种移动到下一级别的方法。

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

       int x_pos=mousework.Width/2;
       int y_pos=mousework.Height-50;
       int bx_pos=mousework.Width/2;
       int by_pos=mousework.Height;
       int y_speed=-15;
       int x_speed=(int)Math.random()*10+1;
       boolean clicked;
       int yyy=420;
       int score=0;
       int scoreb=10;
       //createImage constructor
     public CreateImage(){
         super(true);
     for(int i=0;i<10;i++){
      rect.add(new fallShape(12,12,rect));
      }
     for(int i=0;i<10;i++){
      rect1.add(new fallShape1(12,12,rect1));
     }
    for(int i=0;i<10;i++){
          rect2.add(new fallShape2(12,12,rect2));
     }
    if(rect.isEmpty()==true&&rect1.isEmpty()==true&&rect2.isEmpty()==true){
           int yyy=420;
         }
     Toolkit picx=Toolkit.getDefaultToolkit();
    gunMage=picx.getImage("gunner.jpg");
    gunMage=gunMage.getScaledInstance(200,-1,Image.SCALE_SMOOTH);
      Toolkit pic=Toolkit.getDefaultToolkit();
      pixMage=pic.getImage("ballfall3.jpg");
      pixMage=pixMage.getScaledInstance(200,-1,Image.SCALE_DEFAULT);
      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(mousework.Height);
          bx_pos=e.getX();
      }


    }
    public void mouseReleased(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mousePressed(MouseEvent e){}


    //updating graphics
     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,10,20,null);
       g2.setColor(Color.BLACK);
           Rectangle2D.Float bullet=new Rectangle2D.Float(bx_pos,by_pos,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)>=mousework.Height){
                   by_poss.clear();
               }
             bullet=new Rectangle2D.Float(bx_pos,by_poss.get(i),3,10);


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

       g2.setColor(Color.RED);
       for(fallShape2 b: rect2){
           b.move();
           g2.fill(b);
        if(b.intersects(bullet)&& bullet.y<mousework.Height){

           for(int i=0;i<10;i++){

            rect2.remove(i);
           click.play();
           score+=scoreb;
          }
        }

    }
       for(fallShape1 b: rect1){
             b.move();
            g2.fill(b);
           if(b.intersects(bullet)&& bullet.y<mousework.Height){

            for(int i=0;i<10;i++){
               rect1.removeRange(i,i);
               click.play();
               score+=scoreb;
             }


           }

        }

        for(fallShape b: rect){
            b.move();
            g2.fill(b);
          if(b.intersects(bullet)&& bullet.y<mousework.Height){
            for(int i=0;i<10;i++){
              rect.remove(i);
              click.play();
              score+=scoreb;
            }
            }

         }


         g2.setColor(Color.BLACK);
         g2.setFont(new Font("CALIFORNIAN FB",Font.BOLD,15));
          g2.drawString((score+""),265,10);

            //boolean onStroke;
      //g2.hit(bullet,rect,onStroke);
     if(yyy==420){
          close();
          new room().setVisible(true);

      }
    }
public void close(){
  WindowEvent closy=new WindowEvent(new mousework2(),WindowEvent.WINDOW_CLOSING);
  Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closy);
 }

  }

1 个答案:

答案 0 :(得分:0)

请在将来添加更多不同的评论和格式:)

如果我理解你的问题,你的问题是,如果你再次点击鼠标,所有子弹都会移动到相同的x位置而不是一行飞行。 对我而言,代码看起来像你总是更新完整的图片 所以你需要每个子弹的xposition属性。 您必须添加一个类似于ArrayList by_poss的数组。所以添加这个

ArrayList<Integer> bx_pos_Array = new ArrayList<>();

ArrayList<fallShape> rect= new ArrayList<fallShape>();
ArrayList<fallShape1> rect1= new ArrayList<fallShape1>();
ArrayList<fallShape2> rect2= new ArrayList<fallShape2>();
ArrayList<Integer> by_poss = new ArrayList<>();

并通过创建子弹来设置它 所以改变

public void mouseClicked(MouseEvent e){
       if(e.getButton()==1){
          by_poss.add(mousework.Height);
          bx_pos=e.getX();
      }


    }

public void mouseClicked(MouseEvent e){
           if(e.getButton()==1){
              by_poss.add(mousework.Height);
              bx_pos=e.getX();
              bx_pos_Array.add(bx_pos);  //add this line
          }


        }

并更改

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)>=mousework.Height){
                   by_poss.clear();
               }
             bullet=new Rectangle2D.Float(bx_pos,by_poss.get(i),3,10);


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

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)>=mousework.Height){
                   by_poss.clear();
               }
             bullet=new Rectangle2D.Float(bx_pos_Array.get(i),by_poss.get(i),3,10);  //this line (the first argument)changed


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

用自己的xpos来投掷每个子弹

问候琼斯