我相信这是一个非常简单的 OO 问题,但我似乎找不到答案:/ 我有一个游戏面板,其中有很多球被涂在面板上。当球击中面板底部时,应显示 Game Over 消息。
我正在处理的问题是关于这个 Game Over JOptionPane
。我认为它应该保留在这个类中,但我需要在Ball
类中调用它。
这是Ball
我要调用该方法的类的一部分(标有**):
private void moveBall() {
if (x == panel.getWidth() - size) {
xa -= speed;
} else if (x < 0) {
xa += speed;
}
if (y == panel.getHeight() - size) {
ya -= speed;
} else if (y < 0) {
ya += speed;
}
if (collision()) {
ya = -speed;
y = platform.getY() - DIAMETER;
}
if (y == panel.getHeight() - size) {
// ***Call gameOver here***
}
x += xa;
y += ya;
}
这是从我的游戏面板中的球类调用的构造函数:
// Constructor to pass a colour and a platform
public Ball(JFrame frame, JPanel panel, Platform platform, Color colour,
int x, int y, int size) {
this.platform = platform;
this.frame = frame;
this.panel = panel;
this.colour = colour;
// Location of the ball
this.x = x;
this.y = y;
// Size of the ball
this.size = size;
animator = new Thread(this);
animator.start();
}
那么如何访问该方法呢?
注意(结构):框架 -> 面板 -> 球
谢谢
如果我没有很好地解释自己或者您需要更多信息,请告诉我