为什么球不在窗口的中心?

时间:2016-04-18 16:30:03

标签: processing

代码:

 
class Attractor {

  PVector location;
  float mass;

  Attractor() {
    location = new PVector(width/2, height/2);
    mass = 5;
  }

  void display() {
    stroke(0);
    fill(125);
    ellipse(location.x, location.y, mass*10, mass*10);
  }
}

Attractor a = new Attractor();

void setup()
{
  size(640, 360);
}

void draw()
{
  background(255);
  a.display();
}

球的位置在Attractor对象中,即PVector(宽度/ 2,高度/ 2)。

所以我想知道为什么当我运行代码时,它不在中心,而是在窗口的右侧和上侧。

1 个答案:

答案 0 :(得分:1)

这是因为您在调用Attractor函数之前创建了setup() widthheight尚未设置,因此它们默认为100

要解决此问题,请确保在致电Attractor功能后创建size()

 
Attractor a;

void setup()
{
  size(640, 360);
  a = new Attractor();
}