Java BounceBall 球移动 [英] Java BounceBall Ball Move

查看:40
本文介绍了Java BounceBall 球移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我不会让球从鼠标指针中逃脱.当鼠标指针进入屏幕时,所有的球都会移动到左角.我究竟做错了什么?有什么提示吗??

I have a question. I'm not getting make the balls escape from the mouse pointer. All balls go to the left corner when the mouse pointer enters the screen. What am I doing wrong? Any tips??

我的完整代码:Java BounceBall 鼠标事件

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

}

对于某些球,它工作正常.他们击中鼠标指针并改变您的方向.但大多数都出现在屏幕的角落.

For some balls it works fine. They hit in the mouse pointer and change your direction. But the majority goes to the corner of the screen.

谢谢.

推荐答案

问题已解决...

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

Problem: The bubbles were locked in the upper corner of the screen. And do not hit the mouse pointer.

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

Solution: I calculated the distance from the X and Y position relative to the bubble diameter and the mouse pointer. For collision.

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

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

Then calculated the X and Y radius of the bubbles.

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

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

Finally, I changed the IF to check the relationship between the distance of the bubble radius. Changing your direction.

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

这篇关于Java BounceBall 球移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆