数组仅显示1个对象而不是全部变量

时间:2016-05-12 02:26:37

标签: processing

我是编码的新手(因为我相信你可以说出来)。该程序运行并生成1个Tubble,但其他对象不显示。

这是我的班级:

  class Tubble {

  float x;
  float y;
  float diameter;
  float yspeed;
  float tempD = random(2,86);

  Tubble() {
    x = random(width);
    y = height-60;
    diameter = tempD;
    yspeed = random(0.1, 3);
  }

  void ascend() {
    y -= yspeed;
    x = x + random(-2, 2);
  }

  void dis() {
    stroke(0);
    fill(127, 100);
    ellipse(x, y, diameter, diameter);
  }

  void top() {
    if (y < -diameter/2) {
      y = height+diameter/2;
    }
  }
}

  class Particle {
  float x, y;
  float r;
  float speed = 2.2;
  int direction = 1;
  PImage pbubbles;
  Particle(float x_, float y_, float r_) {
    x = x_;
    y = y_;
    r = r_;
  }

  void display() {
    stroke(#F5D7D7);
    strokeWeight(2.2);
    noFill();
    ellipse(x, y, r*2, r*2);
  }

  boolean overlaps(Particle other) {
    float d = dist(x, y, other.x, other.y);
    if (d < r + other.r) {
      return true;
    } else {
      return false;
    }
  }
  void move() {   //testing
    x += (speed * direction); 
    if ((x > (width - r)) || (x < r)) { 
      direction *= -1;
    }
  }      
  void updown() {
    y += (speed * direction); 
    if ((y > (height - r)) || (y < r)) { 
      direction *= -1;
    }
  }
}

主要草图:

    Tubble[] tubbles = new Tubble [126];
PImage bubbles;

Particle p1;
Particle p2;
Particle p3;
Particle p4;
Particle p5;

float x,y;
float speed;
int total = 0;
int i;


//int direction = 1;

void setup() {
  size(800, 925);

  bubbles = loadImage("purple_bubbles.png");
  p1 = new Particle (100, 100, 50);
  p2 = new Particle (500, 200, 100);
  p3 = new Particle (600, 600, 82);
  p4 = new Particle (height/2, width/2, 200);
  p5 = new Particle ((height/3), (width/3), 20);

  for (int i = 0; i < tubbles.length; i++); {
    tubbles[i] = new Tubble();
  }
}

void mousePressed() {
  total = total + 1;
}

void keyPressed() {
  total = total - 1;
}

void draw() {
  image(bubbles, 0, 0, 800, 925);
  //background(0);

for (int i = 0; i < tubbles.length; i++);  {
    tubbles[i].ascend();
    tubbles[i].dis();
    tubbles[i].top();
  }

  if ((p2.overlaps(p1)) && (p2.overlaps(p4))) {
    background(#BF55AB, 25);
  }
  if ((p3.overlaps(p2)) && (p3.overlaps(p4))) {
    background(#506381, 80);
  }

  p2.x = mouseX;
  p3.y = mouseY;


  p1.display();
  p2.display();
  p3.display();
  p4.display();
  p5.display();

  p4.move();
  p5.updown();

//  for (int i = 0; i < tubbles.length; i++);  {
  //  tubbles[i].dis();
  //  tubbles[i].ascend();
  //  tubbles[i].top();
//  }
}

1 个答案:

答案 0 :(得分:4)

行中有额外的分号

for (int i = 0; i < tubbles.length; i++); {
for (int i = 0; i < tubbles.length; i++);  {
//  for (int i = 0; i < tubbles.length; i++);  {

它使for循环几乎不做任何事情。 然后 for循环之后的块将读取全局变量i并仅执行一次操作。

删除像这样的分号,将块放在循环中。

for (int i = 0; i < tubbles.length; i++) {
for (int i = 0; i < tubbles.length; i++)  {
//  for (int i = 0; i < tubbles.length; i++)  {