我正在使用Processing编写一个程序,但我不断收到期望TRIPLE_DOT,找到“;”。 可能出了什么问题?
class Collision {
Ball ball = new Ball();
Block block = new Block();
int ball_xpos;
int ball_rad;
int ball_ypos;
int block_width;
int block_height;
int block_control;
Collision(ball.xpos, ball.rad, ball.ypos, block.width, block.height, block.control){
//
}
void detect_() {
//not done yet
}
}
球类: 球类{ 积分弧度 = 30;//形状的宽度 float x 坐标、y 坐标;//形状的起始位置
float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
Ball() {
ellipseMode(RADIUS);
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
}
void display() {
ellipseMode(CENTER);
ellipse(xpos, ypos, 410, 40);
}
void move() {
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-rad || xpos < rad) {
xdirection *= -1;
}
if (ypos > height-rad || ypos < rad) {
ydirection *= -1;
}
// Draw the shape
ellipse(xpos, ypos, rad, rad);
}
}
最佳答案
在构造函数的参数名称中,点 (.
) 应替换为 _
。您应该为这些参数指定类型:
Collision(int ball_xpos, int ball_rad, ... so on){
//
}
如果您使用 ball.xpos
,则编译器需要在第 1st 点后出现 var-args(.
) 在球
之后。
但似乎想要传递 Ball
类的属性,以使用 Ball
类属性初始化字段。在这种情况下,您应该只传递一个参数,即对 Ball
的引用:
Collision(Ball ball) {
this.ball = ball;
}
但我根本不明白为什么你在 Collision
类中拥有这些字段 (ball_xpos
, ball_ypos
),因为你还有一个 Ball
类型字段。您可以删除它们,只需将 ball
引用设置为上面构造函数中传递的引用即可。
对于Block
类型引用也是如此。您只需再次复制 Collision
类中的 Block
和 Ball
字段即可。不需要。
关于java - 需要 TRIPLE_DOT,发现为 ';',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18364152/