gpt4 book ai didi

java - 如何使用返回 2 个以上值的 boolean 方法

转载 作者:行者123 更新时间:2023-12-01 08:07:37 25 4
gpt4 key购买 nike

此方法返回 true、false 和另一个值。

public boolean Intersection (Circle circle, Rectangle rectangle) {

... // test something
... return true;
... // test another thing
... return false;
...
... return xCornerDistSq + yCornerDistSq <= maxCornerDistSq; //Third return value
}

这是一款 2D 游戏,球应该从矩形(包括矩形的边缘)弹起。我在上面链接的第三个返回值应该检测球和矩形边缘之间的碰撞。问题是,一旦调用该方法,我就不知道如何在代码中使用它。

我目前拥有的是这个

这是该方法的完整代码:

        public boolean Intersection (Circle circle, Rectangle rectangle) {
double cx = Math.abs(circle.getLayoutX() - rectangle.getLayoutX() - rectangle.getWidth() / 2);
double xDist = rectangle.getWidth() / 2 + circle.getRadius();

if (cx > xDist) { return false; }

double cy = Math.abs(circle.getLayoutY() - rectangle.getLayoutY() - rectangle.getHeight() / 2) ;
double yDist = rectangle.getHeight() / 2 + circle.getRadius();

if (cy > yDist) { return false; }

if (cx <= rectangle.getWidth() / 2 || cy <= rectangle.getHeight() / 2) { return true; }

double xCornerDist = cx - rectangle.getWidth() / 2;
double yCornerDist = cy - rectangle.getHeight() / 2;
double xCornerDistSq = xCornerDist * xCornerDist;
double yCornerDistSq = yCornerDist * yCornerDist;
double maxCornerDistSq = circle.getRadius() * circle.getRadius();
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
}

那么,当我调用该函数时,如何实现它呢?我希望我的球也从边缘反弹,但我不知道如何使用此方法调用它。

我目前拥有的是:

                boolean intersection = Intersection(circle1, rect1);
if (intersection == true) {
double x = (rect1.getLayoutX() + rect1.getWidth() / 2) - (circle1.getLayoutX() + circle1.getRadius());
double y = (rect1.getLayoutY() + rect1.getHeight() / 2) - (circle1.getLayoutY() + circle1.getRadius());
if (Math.abs(x) > Math.abs(y)) {
c1SpeedX = -c1SpeedX;
} else {
c1SpeedY = -c1SpeedY;
}
}
}
}

最佳答案

“假设的”枚举可能看起来像

public enum Ternary {
TRUE, FALSE, MAYBE;
}

关于java - 如何使用返回 2 个以上值的 boolean 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20253014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com