关于java:mouseMoved方法中的碰撞检测

Collision detection in mouseMoved method

我正在用java创建一个游戏。在其中,您控制一个跟随鼠标的正方形。我想为正方形实现碰撞检测,使其在 JFrame 内略微停止,而不是在边缘处。使用箭头键执行此操作非常容易,但我无法使用 mouseMoved 方法解决。这是 mouseMoved 方法所在的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void mouseMoved(MouseEvent e){

            repaint();
            if(e.getX() <= 0)
                playerX = 0;
            if(e.getX() >= 300)
                playerX = 500;
            if(e.getY() <= 0)
                playerY = 0;
            if(e.getY() >= 300)
                playerY = 500;
            else
            playerX = e.getX()-25;
            playerY = e.getY()-25;
            repaint();

        }

这是创建正方形的代码:

1
2
3
4
5
6
7
8
public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);
        repaint();

    }

我不认为你会需要这个,但以防万一,这里是 GamePanel 类的所有代码,它用作 Main 类中我的 JFrame 的面板。如果您需要 Main 课程,请告诉我,但我怀疑您会:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable{

    //Global variables

    //Double buffering
    private Image dbImage;
    private Graphics dbg;


    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 500;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);

    //Game variable
    private Thread game;
    private volatile boolean running = false;
    public boolean mouseClicked = false;

    //Character variables
    int playerX = 150, playerY = 150;

    public class Mouse extends MouseAdapter{

        public void mousePressed(MouseEvent e){

            mouseClicked = true;

        }

        public void mouseReleased(MouseEvent e){

            mouseClicked = false;

        }

        public void mouseMoved(MouseEvent e){

            repaint();
            if(e.getX() <= 0)
                playerX = 0;
            if(e.getX() >= 300)
                playerX = 500;
            if(e.getY() <= 0)
                playerY = 0;
            if(e.getY() >= 300)
                playerY = 500;
            else
            playerX = e.getX()-25;
            playerY = e.getY()-25;
            repaint();

        }
    }



    public GamePanel(){

        addMouseMotionListener(new Mouse());
        setPreferredSize(gameDim);
        setBackground(Color.BLUE);
        setFocusable(true);
        requestFocus(true);

    }

    public void run(){

        while(running){



        }

    }

    public void addNotify(){

        super.addNotify();
        startGame();

    }

    private void startGame(){

        if(game == null || !running){

            game = new Thread(this);
            game.start();
            running = true;

        }

    }

    public void stopGame(){

        if(running){

            running = false;

        }

        //Paint method


    }

    public void paint(Graphics g){

        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);

    }

    public void paintComponent(Graphics g) {

        Rectangle player = new Rectangle(playerX, playerY, 50, 50);
        g.setColor(Color.blue);
        g.fillRect(player.x, player.y, player.width, player.height);
        repaint();

    }

    private void log(String s){

        System.out.println(s);

    }

}

感谢您的帮助。如果您需要任何东西,请告诉我。


你的动作代码有点不对劲。仅当 Y 未超出范围并始终替换 Y 值时,才设置 x 位置。我可以建议将来使用完整的块吗?它们有助于避免这样的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
playerX = e.getX()-25;
playerY = e.getY()-25;
if(e.getX() <= 0){
    playerX = 0;
}
else if(e.getX() >= 300){
    playerX = 500;
}

if(e.getY() <= 0){
    playerY = 0;
}
else if(e.getY() >= 300){
     playerY = 500;
}

这将首先设置位置,然后在玩家出界时进行更正。