mouseMoved方法中的碰撞检测

时间:2013-09-09 22:14:30

标签: java swing jpanel paint paintcomponent

我正在用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);

    }

}

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

1 个答案:

答案 0 :(得分:1)

你的动作代码有点偏。仅当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;
}

这首先设置位置,然后在玩家超出界限时进行纠正。