【问题标题】:Processing/Java - Having trouble with Class Array处理/Java - 类数组有问题
【发布时间】:2020-05-15 15:48:55
【问题描述】:

这可能是一个非常愚蠢的问题,但我无法在屏幕上显示更多我的课程副本。 我创建了一个小行星类,可以在屏幕上生成和移动小行星。然而,当我尝试在主体中调用此类的多个版本时,它仍然只显示一个小行星。

主要

int lrgAsteroids = 4;
Asteroid[] asteroid = new Asteroid[lrgAsteroids];

void setup() {
  size(800,800);
  for (int i = 0; i < lrgAsteroids; i++) {
      asteroid[i] = new Asteroid();
      asteroid[i].display();
    }
}

void draw() {
  background(0);
  asteroid[0].move();
  asteroid[1].move();
  for (int i = 0; i < lrgAsteroids; i++) {
    asteroid[i].move();
  }
}

小行星类。

class Asteroid {
  PImage lrgAsteroid;
  float xpos, ypos;
  float yDirection;
  float xDirection;
  float radians = 0;


  Asteroid() {
    lrgAsteroid = loadImage("largeAsteroid.png");
    xpos = random(0,710);
    ypos = random(0,710);

    int xDir = (int) random(2);
    int yDir = (int) random(2);

    if (xDir == 1) {
      xDirection = 1;
    } else if (xDir == 0) {
      xDirection = -1;
    }

    if (yDir == 1) {
      yDirection = 1;
    } else if (yDir == 0) {
      yDirection = -1;
    }
  }

  void display() {
    image(lrgAsteroid, xpos, ypos);
  }

  void move() {    
     background(0);
     pushMatrix();
     imageMode(CENTER);
     translate(xpos, ypos);
     rotate(radians);
     image(lrgAsteroid, 0, 0);
     popMatrix();

     if (xpos <= 0) {
       xpos = random(750,800);
     } else if (xpos >= 800) {
       xpos = random(0,100);
     }

     if (ypos <= 0) {
       ypos = random(750,800);
     } else if (ypos >= 800) {
       ypos = random(0,100);
     }

     radians += 0.02;
     xpos += xDirection;
     ypos += yDirection;
  }

}

任何帮助将不胜感激。

【问题讨论】:

    标签: java arrays class object processing


    【解决方案1】:

    错误很简单。实际上,在绘制小行星之前,由于background(0); 方法move() 中的background(0);,显示是清晰的。清除draw()开头的背景就足够了。

    从方法move()中删除background(0);

    Asteroid() {
      // [...]
    
      void move() {    
    
         // background(0); <---- DELETE 
    
         pushMatrix();
         imageMode(CENTER);
         translate(xpos, ypos);
         rotate(radians);
         image(lrgAsteroid, 0, 0);
         popMatrix();
    
         if (xpos <= 0) {
           xpos = random(750,800);
         } else if (xpos >= 800) {
           xpos = random(0,100);
         }
    
         if (ypos <= 0) {
           ypos = random(750,800);
         } else if (ypos >= 800) {
           ypos = random(0,100);
         }
    
         radians += 0.02;
         xpos += xDirection;
         ypos += yDirection;
      }
    }
    

    【讨论】:

    • 你是个天才。谢谢
    【解决方案2】:

    我认为您创建的所有实例都以某种方式获得相同的 xDir 和 yDir 或相同的 xpos 和 ypos ,您可以打印它以便查看是否是问题所在?

    void setup() {
    size(800,800);
    for (int i = 0; i < lrgAsteroids; i++) {
      asteroid[i] = new Asteroid();
      // add these please to see what happens
      System.out.println(asteroid[i].xDirection+" "+asteroid[i].yDirection);
    
     }
    }
    
    void draw() {
     background(0);
     asteroid[0].move();
     asteroid[1].move();
     for (int i = 0; i < lrgAsteroids; i++) {
       asteroid[i].move();
       // add these please to see what happens
       System.out.println(asteroid[i].xpos+" "+asteroid[i].ypos);
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多