我正在用Java创建游戏。在其中,您可以控制跟随鼠标的正方形。我想对正方形实施碰撞检测,以使其在JFrame中稍微停下来,而不是在边缘处停下来。使用箭头键执行此操作非常容易,但是我无法使用mouseMoved方法来解决。这是mouseMoved方法所在的代码:

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 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的面板。如果您需要主班让我知道,但我怀疑您会:

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不超出范围时才设置x位置,并始终替换Y值。我能否在将来建议全部屏蔽-它们有助于避免此类问题。

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;
}


这将首先设置位置,然后在玩家超出范围时对其进行更正。

09-10 07:02