将运动中的子弹与单击的鼠标分开
Separating bullet in motion from mouse clicked
请问如何让已经射出的子弹与我鼠标点击的水平位置分开,这样当我点击鼠标时运动中的子弹就不会移动。
而且我还需要创建一种方法,以便在拍摄所有形状时进入下一个级别,而现在我做不到。
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);
}
}
请在以后添加更多的解释和评论和格式:)
如果我没理解错的话,你的问题是,如果你再次点击鼠标,所有的子弹都会移动到相同的 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
问候琼斯
请问如何让已经射出的子弹与我鼠标点击的水平位置分开,这样当我点击鼠标时运动中的子弹就不会移动。 而且我还需要创建一种方法,以便在拍摄所有形状时进入下一个级别,而现在我做不到。
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);
}
}
请在以后添加更多的解释和评论和格式:)
如果我没理解错的话,你的问题是,如果你再次点击鼠标,所有的子弹都会移动到相同的 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
问候琼斯