【问题标题】:passing parameters from constructor to functions in processing/java将参数从构造函数传递到处理/java中的函数
【发布时间】:2010-08-13 18:01:08
【问题描述】:

我在处理某些对象时遇到问题。 代码应该有两个显示和移动的对象。但我只看到一个显示和移动的对象。也许我缺少一些东西。查看代码。

Rule myRule;
Rule myRule1;

void setup() {
  size(200,200);
  smooth();

  //Initialize rule objects
  myRule = new Rule(0,100,1);
  myRule1 = new Rule(0,140,20);
}



void draw() {
  background(255);
  //int x1 = 0;
  //int y1 = 0;
  //Operate Rule object
  myRule.move();
  myRule.display();
  myRule1.move();
  myRule1.display();
}


class Rule {

  float x;
  float y;
  float spacing;
  float speed;

  Rule(float x1, float y1, float s1) {
    x = x1;
    y = y1;
    spacing = 10;
    speed = s1;
  }

  void move() {
    x = x + speed;
    if((x > width) || (x < 0)) {
      speed = speed * -1;
    }
  }

  //Display lines at x location
  void display() {
    stroke(0);
    fill(175);
    line(x, height/2, width/2, height/2);
  }
}

【问题讨论】:

  • 代码本身对我来说看起来不错。您使用的是哪个框架?
  • 缺少一些可能有问题的代码。

标签: java oop constructor class processing


【解决方案1】:

这是Rule.display() 中的一个错字。你的意思可能是这样的

line(x, y, width/2, height/2);

【讨论】:

  • 当我给两条线都涂上颜色时让我震惊:-)
  • //在x位置显示行 void display() { stroke(0);填充(175);线(x,y,宽度/2,y);我认为优先事项存在某种问题。我相信高度比允许传递值的 y 更具体......它起作用了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 2012-02-21
  • 2019-03-18
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
相关资源
最近更新 更多