简体   繁体   English

mouseMoved事件似乎只被调用一次

[英]mouseMoved event seems to only be getting called once

Trying to make a simple RPG game with a overhead camera. 试图用高架摄像机制作一个简单的RPG游戏。 When I went to go implement a MouseMotionListener , which isn't the first time I used a MouseMotionListener , it appears that the mousedMoved MouseEvent only get's called once then simple doesn't seem to work anymore. 当我去实现MouseMotionListener时 (这不是我第一次使用MouseMotionListener) ,看来mousedMoved MouseEvent仅被调用一次,然后简单调用似乎不再起作用。 I've been stuck on this for a fair bit of time and all my research leads me to dead ends. 我已经在这个问题上停留了相当长的时间,所有的研究使我陷入僵局。

I do have a full game loop with a tick and render methods. 我确实有一个完整的游戏循环,其中有一个tick和render方法。 But as you see in the code below, in the mouse handling class (The bottom bit of code) It returns a number which gets increased by one every time the mouse moves (In the tick method I print to the console the value of the number) and it also should print to the console the mouse x and y coordinates. 但是正如您在下面的代码中看到的那样,在鼠标处理类(代码的底部)中,它返回一个数字,每次鼠标移动时该数字都会增加一个(在tick方法中,我将数字打印到控制台上) ),并且还应将鼠标的x和y坐标打印到控制台。 When you run the program the number is equal to one and it only prints the mouse coordinates once. 运行该程序时,该数字等于1,并且只打印一次鼠标坐标。 An example would be the console output will look something like this: 例如,控制台输出将如下所示:

124 82
1

Or if my mouse isn't hovering the JFrame when to program launches it'll look like this: 或者,如果我的鼠标没有在程序启动时悬停在JFrame上,它将如下所示:

0

Here's my init method for my game loop: 这是我的游戏循环的初始化方法:

private void init() {
    display = new Display("Operation Blood Bath: Alpha 0_5.0", 800, 600);
    game = new Game();
    Assets.init();
}

Here's my display class which gets called in the init method: 这是在init方法中调用的显示类:

public class Display {

private JFrame frame;
private Canvas canvas;

public Display(String title, int width, int height) {
    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.addMouseMotionListener(new MouseMotionHandling());
    frame.pack();
}

public Canvas getCanvas() {
    return canvas;
}

public JFrame getFrame() {
    return frame;
}

}

The tick method: 勾号方法:

private void tick() {
    game.tick();
    System.out.println(MouseMotionHandling.HELLO());
}

Here's the mouse handling class which gets called in the display class: 这是在显示类中调用的鼠标处理类:

public class MouseMotionHandling implements MouseMotionListener {

private static int number = 0;

@Override
public void mouseDragged(MouseEvent arg0) {

}

@Override
public void mouseMoved(MouseEvent arg0) {
    System.out.println(arg0.getX() + " " + arg0.getY());
    number++;
}

public static int HELLO () {
    return number;
}

}

Where am I going work here? 我要去哪里工作

The problem is that your canvas completely fills the content pane of the JFrame and so captures all mouse events. 问题在于您的canvas完全填充了JFrame的内容窗格,因此捕获了所有鼠标事件。 The easiest fix is to add your mouse listener to the canvas rather than the frame . 最简单的解决方法是将鼠标侦听器添加到canvas而不是frame

As for why you're receiving a single mouse event on your frame - I think this must be due to timing issues in the creation of the various components. 至于为什么在框架上收到一个鼠标事件-我认为这一定是由于创建各种组件时的时序问题所致。

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

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