首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MouseMotionListener时遇到麻烦

使用MouseMotionListener时遇到麻烦
EN

Stack Overflow用户
提问于 2017-05-29 08:47:51
回答 1查看 74关注 0票数 0

我前几天刚开始编码。我现在正尝试用FruitNinja来制作一个JPanel类型的游戏,我已经做了很多事情(除了让游戏继续进行的循环和其他的水果/炸弹动画)。

到目前为止,我只对Apple.png进行了动画化(一旦我完成了这一工作,就可以将其他的水果动画化)。

我正在尝试创建一个checkHit方法,以检查鼠标是否检查图像(切割水果),从而检查需要使用的MouseMotionListener。如果老鼠真的翻了果子,我需要它消失/移出框架。

那么,我如何从MouseMotionListener中检索坐标,检查它是否在距我的水果40像素以内,如果是的话,移动帧的水果?如果可能的话,不仅要做到这一点,而且要在这样做的时候播放一个声音。

我有另一个用户帮我解决这个问题,但我仍然不能完全弄清楚。

代码语言:javascript
复制
import javax.swing.*;//imports JPanel class
import java.awt.*;//imports the Graphics class
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
//imports packages needed for the hovering


public class FruitNinja extends JPanel{


   private Image dojo;
   private Image apple;
   private Image orange;
   private Image pear;
   private Image strawberry;
   private Image banana;
   private Image bomb;

   //declares the import image variables 

  private javax.swing.Timer timer;

   private int dx, dy;

   private int vx =40;
   //velocity of the object moving in the x direction
   private int x=-10;

   private int vy=40;
   //velocity of the object moving in the y direction
   private int y=770;


   private int divide=(int)(2*Math.random())+1;//sets random number
   //determines when the fruit will bounce off

  public FruitNinja() { // a constructor to set up graphics windo
      super();
      setBackground(Color.WHITE);
      loadImage();
     dx = 25;
     dy = 25;

     timer = new javax.swing.Timer( 30, new TimerListener() );
         //interval of 10 milliseconds
     timer.start();
         //timer is a facility for threads to schedule tasks for future execution in a background thread.
   }

   private void loadImage() {
      ImageIcon ii = new ImageIcon("Dojo.jpg");
      dojo = ii.getImage();

      ImageIcon oo = new ImageIcon("Orange.ico");
      orange = oo.getImage();

      ImageIcon ss = new ImageIcon("Strawberry.png");
      strawberry = ss.getImage();

      ImageIcon bb = new ImageIcon("Banana.png");
      banana = bb.getImage();

      ImageIcon pp = new ImageIcon("Pear.png");
      pear = pp.getImage();

      ImageIcon aa = new ImageIcon("Apple.png");
      apple = aa.getImage();

      ImageIcon bo=new ImageIcon("Bomb.png");
      bomb=bo.getImage();


      //loads neccaseary images for this game


   }@Override 

   public void paintComponent(Graphics g){ // draw graphics in the panel

     super.paintComponent(g);// to make panel display correctly
     g.drawImage(dojo, 0,0, this);
  //draws out dojo
     g.drawImage(apple, x,y, this);
     g.drawImage(orange, -300,-300, this);
     g.drawImage(pear, -300,-300, this);
     g.drawImage(banana, -300,-300, this);
     g.drawImage(strawberry, -300,-300, this);
     //draws out the fruits somewhere
     g.drawImage(bomb,-300,-300,this);
     //draws out the bomb that will make the player lose

   }

  private class TimerListener implements ActionListener {

         public void actionPerformed(ActionEvent e) {

         //implements the ActionListenter


        x+=vx;   //adds velocity to the x, which makes it travel in x direction        

        if (x<0){

        vx=(-1*vx);
         //tells the velocity to change direction
        }

        y+=vy; //adds velocity to the y, which makes it travel in y direction  
        if (y>=getHeight()||y<=getHeight()/4){

        vy=(-1*vy);
         //tells the velocity to change direction
         }

        repaint(); // causes paintComponent to be called by the JVM

       }


}

   public static void main(String[] args) { 

      FruitNinja panel = new FruitNinja(); // window for drawing  
      JFrame f = new JFrame(); // the program itself
      f.setTitle("Fruit Ninja");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//when the X button is clicked, the program quits
      f.setSize(1280,800);//size of the frame
      Container pane = f.getContentPane();//pane refers to the interior of the JFrame

      FruitNinja p1 = new FruitNinja();

      pane.add(p1);//add the FacePanel object to the interior of the frame
      f.setVisible(true);

   }
}

如果我做错了什么,或者/以及如何在其中使用这个MouseMotionListner,请告诉我。我对此仍然很陌生,所以我不知道如何检索坐标或设置'MouseMotionListener‘。如果我的问题不清楚,可以随意发表意见,如果是这样,请告诉我;我愿意更彻底地解释这个问题。

谢谢

编辑:

这就是方法吗?

代码语言:javascript
复制
      private class HandleMouse extends MouseAdapter {
   //extends MouseAdapter rather than implement Mouse Adapter
   public void mousePressed( MouseEvent e ){
        Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);
        Rectangle appleHitBox=new Rectangle(ax-128,ay-128,256,256);

       Area area = new Area(hitBox);
        area.intersect(new Area(appleHitBox));
        if(!area.isEmpty()){
            ax=-500;
            ay=-500;
            avx=0;
            avy=0;   
        }
        else{
            numOfMisses++;

        }
}
}

EN

回答 1

Stack Overflow用户

发布于 2017-05-29 09:04:37

那么我如何从MouseMotionListener中检索坐标,

MouseMotionListener将一个MouseEvent传递给方法,该方法包含事件的各种属性,这包括组件坐标上下文中事件的坐标。

检查它是否在我水果的40像素以内,如果是的话。

因此,这是基本的碰撞检测,有许多可能的方法,但由于我很懒,我可能会考虑使用Rectangle来表示用户点击框

代码语言:javascript
复制
Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);

这将在MouseEvent的中心点周围生成一个20像素的框。

“但是为什么呢?”你问我?因为Rectangle有许多有用的方法可用于碰撞检测,包括

  • intersection
  • intersects
  • union

举几个例子

因此,鉴于水果的x/y位置和大小,您可以简单地创建第二个Rectangle,并确定第一个是否击中第二个Rectangle

如果是的话,把水果从框架里移开?

这是一个设置标志的过程,“主循环”(你的TimerListener)告诉它水果被击中了,现在由“主循环”从“油漆”列表中删除它,并设置下一个对象

如果可能的话,不仅要这样做,而且要在完成时播放一个声音。

一般的答案是。一旦您能够检测到发生了碰撞,播放声音相对简单,我会再次诱惑您使用“主循环”进行此操作,当它检测到发生了命中时。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44238231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档