等距游戏鼠标逻辑和在环境中的移动

Isometric Game Mouse logic and movement around an environment

我正在制作一个等距游戏环境,我希望通过按住鼠标右键单击并移动鼠标来环顾地图来环顾环境。

运动第一次运行良好,但当我第二次尝试时,它会自行重置到原始位置,然后再次移动。我知道这与鼠标位置相对于正在移动的背景有关,但我尝试了多种解决方案,但我仍然无法理解逻辑,有没有人可以帮助我弄清楚我做错了什么,在此先感谢。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class CameraMovementTest extends JPanel{

private Timer timer;
private int DELAY = 10;
private  CustomMouseListener mouseListener;
private int positionX = 0, positionY = 0;

public CameraMovementTest() {

    mouseListener  = new CustomMouseListener();
    this.addMouseListener(mouseListener);
    this.addMouseMotionListener(mouseListener);
    this.setSize(500,500);
    this.setVisible(true);

    //Swing Timer
    timer = new Timer(DELAY, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            update();
            repaint();
            validate(); 
        }
    }); 
    timer.start();
}

private void update() {
    if(mouseListener!=null){
        positionX = mouseListener.getX();
        positionY = mouseListener.getY();
    }
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.fillRect( positionX,  positionY, 300,300);
}

public class CustomMouseListener implements MouseListener, MouseMotionListener {
    private int positionX = 0, positionY = 0;
    private int mouseClickX, mouseClickY;

    @Override
    public void mousePressed(MouseEvent evt) {

        if(SwingUtilities.isRightMouseButton(evt)) {

            mouseClickX = evt.getX();
            mouseClickY = evt.getY();
        }   
    }
    @Override
    public void mouseDragged(MouseEvent evt) {
        if(SwingUtilities.isRightMouseButton(evt)) {
            positionX = mouseClickX - evt.getX();
            positionY = mouseClickY - evt.getY();
        }
    }
    @Override
    public void mouseMoved(MouseEvent arg0) {}
    @Override
    public void mouseClicked(MouseEvent arg0) { }
    @Override
    public void mouseEntered(MouseEvent arg0) { }
    @Override
    public void mouseExited(MouseEvent arg0) {}

    @Override
    public void mouseReleased(MouseEvent arg0) {}
    public int getX(){
        return positionX;
    }
    public int getY(){
        return positionY;
    }
}


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame f = new JFrame();
            f.setVisible(true);
            f.setSize(new Dimension(500,500));
            f.setContentPane(new CameraMovementTest());
        }
    });
}

}

it resets itself to the original location before moving again.

您是否添加了任何调试代码来查看您对 x/y 位置的计算?

@Override
public void mousePressed(MouseEvent evt) {

    if(SwingUtilities.isRightMouseButton(evt)) {
        mouseClickX = evt.getX();
        mouseClickY = evt.getY();
    }
}

@Override
public void mouseDragged(MouseEvent evt) {
    if(SwingUtilities.isRightMouseButton(evt)) {
        System.out.println(mouseClickX + " : " + evt.getX());
        positionX = mouseClickX - evt.getX();
        positionY = mouseClickY - evt.getY();
    }
}

您将 mouseClickX/Y 设置为鼠标点,然后在 mouseDragged 中从值中减去鼠标点。这意味着您基本上得到 0(实际上是 1,因为鼠标移动了 1 个像素以生成拖动事件)。

因此在 mousePressed 逻辑中,您不能只将 mouseClickX/Y 重置为当前鼠标点。您还需要跟踪最后一个鼠标点:

@Override
public void mousePressed(MouseEvent evt) {

    if(SwingUtilities.isRightMouseButton(evt)) {
        //mouseClickX = evt.getX();
        //mouseClickY = evt.getY();
        mouseClickX = positionX + evt.getX();
        mouseClickY = positionY + evt.getY();
    }
}