0

我正在用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 的面板。如果您需要 Main 课程,请告诉我,但我怀疑您会:

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

    }

}

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

4

1 回答 1

1

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

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

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

于 2013-09-09T22:30:10.003 回答