【问题标题】:Expecting TRIPLE_DOT, found ';'期待 TRIPLE_DOT,找到 ';'
【发布时间】:2013-08-22 01:47:54
【问题描述】:

我正在使用 Processing 编写程序,但我不断收到 Expecting TRIPLE_DOT, found ';'。 有什么问题?

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; // 形状的宽度 浮动 xpos, ypos; // 形状的起始位置

  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);
  }
}

【问题讨论】:

    标签: java processing


    【解决方案1】:

    在构造函数的参数名称中,点 (.) 应替换为 _。您应该为这些参数指定类型:

    Collision(int ball_xpos, int ball_rad, ... so on){
        //
    }
    

    如果您使用ball.xpos,则编译器期望在ball 之后的第一个st 点(.)之后有一个var-args


    但似乎想要传递Ball 类的属性,以使用Ball 类属性初始化字段。在这种情况下,您应该只传递一个参数,即对Ball 的引用:

    Collision(Ball ball) {
        this.ball = ball;
    }
    

    但我不明白为什么你在Collision 类中有这些字段(ball_xposball_ypos),因为你也有一个Ball 类型字段。您可以删除它们,只需将ball 引用设置为上述构造函数中传递的引用即可。

    Block 类型引用也是如此。您只是在Collision 类中再次拥有BlockBall 字段的副本。不需要。

    【讨论】:

    • 但我试图从Ball class 中调用一个变量xpos。然后使其等于x_pos
    • @AdegokeA。已编辑帖子。
    猜你喜欢
    • 2021-06-16
    • 2019-11-30
    • 2021-09-17
    • 2019-08-12
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多