有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

摆动Java弹跳球移动

我有个问题。 我不想让球从鼠标指针上逃脱。 当鼠标指针进入屏幕时,所有球都会转到左角。 我做错了什么?有什么建议吗

我的完整代码: Java BounceBall mouse event

http://ideone.com/vTGzb7

有问题的方法:

public void move(Ball ball, Point mouse) {

    try {
        Point p = ball.getLocation();
        Point speed = ball.getSpeed();
        Dimension size = ball.getSize();

        int vx = speed.x;
        int vy = speed.y;

        int x = p.x;
        int y = p.y;

        // ----------------------
        if (mouse != null) {

            int xDistance = Math.abs(x + size.width - mouse.x);
            int yDistance = Math.abs(y + size.height - mouse.y);

            if (xDistance < yDistance) {
                if (x + size.width < mouse.x) {
                    if (vx > 0) {
                        vx *= -1;
                    }
                } else {
                    if (vx > 0) {
                        vx *= -1;
                    }
                }
            } else {
                if (y + size.height < mouse.y) {
                    if (vy > 0) {
                        vy *= -1;
                    }
                } else {
                    if (vy > 0) {
                        vy *= -1;
                    }
                }
            }

        }
        // ----------------------

        if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
            vx *= -1;
        }
        if (y + vy < 0
                || y + size.height + vy > getParent().getHeight()) {
            vy *= -1;
        }
        x += vx;
        y += vy;

        ball.setSpeed(new Point(vx, vy));
        ball.setLocation(new Point(x, y));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

对于一些球来说,它很好用。 他们击中鼠标指针并改变你的方向。 但大多数人都走到了屏幕的角落

多谢各位


共 (1) 个答案

  1. # 1 楼答案

    问题解决了

    问题:气泡被锁定在屏幕的上角。不要点击鼠标指针

    解决方案:我计算了X和Y位置相对于气泡直径和鼠标指针的距离。为了碰撞

    int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
    int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);
    

    然后计算气泡的X和Y半径

    int radiusX = (size.width / 2);
    int radiusY = (size.height / 2);
    

    最后,我更改了IF以检查气泡半径距离之间的关系。改变你的方向

    if (xDistance <= radiusX && yDistance <= radiusY) {
        if (xDistance < yDistance) {
            vx *= -1;
        } else {
            vy *= -1;
        }
    
        System.out.println("Hit!");
    }
    

    新的移动方法:

    public void move(Ball ball, Point mouse) {
    
        try {
            Point p = ball.getLocation();
            Point speed = ball.getSpeed();
            Dimension size = ball.getSize();
    
            int diameter = ball.dimeter;
    
            int vx = speed.x;
            int vy = speed.y;
    
            int x = p.x;
            int y = p.y;
    
            int radiusX = (size.width / 2);
            int radiusY = (size.height / 2);
    
            //            
    
            if (mouse != null) {
                int xDistance = Math.abs((x + (diameter / 2)) - mouse.x);
                int yDistance = Math.abs((y + (diameter / 2)) - mouse.y);
    
                System.out.printf("b(%d, %d) m(%d, %d) dx(%d, %d)\n", x, y,
                        mouse.x, mouse.y, (x + vx) - mouse.x, (y + vy)
                                - mouse.y);
    
                if (xDistance <= radiusX && yDistance <= radiusY) {
    
                    if (xDistance < yDistance) {
                        vx *= -1;
                    } else {
                        vy *= -1;
                    }
    
                    System.out.println("Hit");
                }
            }
    
            if (x + vx < 0 || x + size.width + vx > getParent().getWidth()) {
                vx *= -1;
            }
            if (y + vy < 0
                    || y + size.height + vy > getParent().getHeight()) {
                vy *= -1;
            }
    
            x += vx;
            y += vy;
    
            ball.setSpeed(new Point(vx, vy));
            ball.setLocation(new Point(x, y));
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }