我正在尝试使用两个球精灵创建球碰撞。当一个球碰撞时它的速度应该取决于其他球的初始速度,所以在精灵中我需要调用另一个球的速度,但我无法弄清楚如何通过非静态变量信息。这是带错误的行
xa = BIGBALLspeedx()*(BIGBALLmass()-1)+(BallTest.ball.BALLspeedx()*2*1);
这是我的一个精灵和主要包'BallTest'的代码:
package balltest;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import static java.lang.Math.sqrt;
public class BigBall {
private final int DIAMETER = 80;
int x = (int )(Math.random() * 200 + 15 );
int y = (int )(Math.random() * 200 + 15 );
int xa = (int )(Math.random() * 4 );
int ya = (int )(1 );
private BallTest game;
public BigBall(BallTest game) {
this.game= game;
}
public void move() {
if (x + xa < 0)
xa = 1;
if (x + xa > game.getWidth() - DIAMETER)
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > game.getHeight() - DIAMETER)
ya=-1;
if (y + ya < 1)
ya=1;
if (50>sqrt(xDIFF()*xDIFF()+yDIFF()*yDIFF()) && -50<sqrt(xDIFF()*xDIFF()+yDIFF()*yDIFF())) {
xa = BIGBALLspeedx()*(BIGBALLmass()-1)+(BallTest.ball.BALLspeedx()*2*1);
}
x = x + xa;
y = y + ya;
}
public int BIGBALLx() {
return x+40;
}
public int BIGBALLy(){
return y+40;
}
public int BIGBALLspeedx(){
return xa;
}
public int BIGBALLspeedy(){
return ya;
}
public int BIGBALLmass(){
return 5;
}
public int xDIFF(){
return game.ball.BALLx()-game.BigBall.BIGBALLx();
}
public int yDIFF(){
return game.ball.BALLy()-game.BigBall.BIGBALLy();
}
public void paint2(Graphics2D g) {
g.fillOval(x, y, DIAMETER, DIAMETER);
}
public Rectangle getBounds() {
return new Rectangle(x, y, DIAMETER, DIAMETER);
}
}
这是BallTest
代码:
package balltest;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static java.awt.image.ImageObserver.ABORT;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author F27834
*/
public class BallTest extends JPanel {
Ball ball = new Ball(this);
BigBall BigBall = new BigBall(this);
private void move() {
ball.move();
BigBall.move();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
g.setColor(Color.BLUE);
Graphics2D g3d = (Graphics2D) g;
BigBall.paint2(g3d);
}
public void gameOver() {
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
BallTest game = new BallTest();
frame.add(game);
frame.setSize(300, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}
答案 0 :(得分:0)
您正在尝试引用BallTest.ball - BallTest可能是另一个类,而ball是该类中定义的实例变量。在Java术语中,BallTest是一个静态引用(它指的是一个类,而不是一个对象),而ball只是由BallTest的一个实例定义。
在代码的其他部分,您可以参考game.ball。没有看到剩下的代码就无法确定,但是你应该使用game.ball而不是BallTest.ball吗?
那就是这样吗?
xa = BIGBALLspeedx()*(BIGBALLmass()-1)+(game.ball.BALLspeedx()*2*1);