java - 需要 TRIPLE_DOT,发现为 ';'

标签 java processing

我正在使用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 类中的 BlockBall 字段即可。不需要。

关于java - 需要 TRIPLE_DOT,发现为 ';',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18364152/

相关文章:

java - 在 iText 7 中实现 PdfContentByte 和 PdfTemplate 的替代方法是什么

java - 可以对线程的这方面进行一些解释

java - 使用 BufferedReader 对多维数组中的行进行计数

java - 无法连接 Spark-Cloudant

javascript - 在 p5.js 中使用 random() 函数

java - 在自定义模块中访问 "Application"

java - 为什么我的 jar 不反射(reflect)变化?

android - 如何解决Java Eclipse中的 “The hierarchy of the type is inconsistent”错误?

javascript - 如何在 p5.js 中的点数组之间绘制正方形?

java - 无法加载电影文件处理、库问题?