课程和班级的新手并不起作用。为什么不呢?

时间:2018-01-06 15:53:58

标签: class arraylist processing

在我的程序中,我想让三角形旋转并跟随鼠标位置。我有它工作,但它很丑,因为我没有使用任何类(我是新的),只是粘贴三角形并更改变量。这是我提出的课程。

class Enemy {
  float x = random(-width, 0);
  float y = random(0, height);
  float x1;
  float x2 = -20;
  float x3 = 20;
  float y1 = (+(sqrt(3)/3)*40);
  float y2 = (-(sqrt(3)/3)*40);
  float y3 = (-(sqrt(3)/3)*40);
  float speed;
  float slope;
  float atanSlope;
  Enemy(float tempSpeed) {
    speed = tempSpeed;
  }
  void rotateEnemy() {
    float x1Rotated = rotateX(x1, y1, theta2, 0);
    y1 = rotateY(x1, y1, theta2, 0);
    x1 = x1Rotated;
    float x2Rotated = rotateX(x2, x2, theta2, 0);
    x2 = rotateY(x2, x2, theta2, 0);
    x2 = x2Rotated;
    float x3Rotated = rotateX(x3, x3, theta2, 0);
    x3 = rotateY(x3, x3, theta2, 0);
    x3 = x3Rotated;
  }
  void move() {
    slope = (y - mouseY)/(x-mouseX);
    atanSlope = atan(slope);
    if (slope < 0 && mouseY < y ) {
      x += cos(atanSlope)*(speed + speedChange);
      y += sin(atanSlope)*(speed + speedChange);
    } else if (slope >= 0 && mouseY < y) {
      x -= cos(atanSlope)*(speed + speedChange);
      y -= sin(atanSlope)*(speed + speedChange);
    } else if (slope > 0) {
      x += cos(atanSlope)*(speed + speedChange);
      y += sin(atanSlope)*(speed + speedChange);
    } else {
      x -= cos(atanSlope)*(speed + speedChange);
      y -= sin(atanSlope)*(speed + speedChange);
    }
  }
  void drawEnemy() {
    translate(x, y);
    triangle(x1, y1, x2, x2, x3, x3);
    translate(-x, -y);
  }
  void collisionDetect() {
    if (abs(mouseX-x) + abs(mouseY-y)  < 80)
      if (isDeadly) {
        respawn();
        energy -= height/16;
        points += 500;
      } else
        energy = 0;
  }
  void respawn() {
    int ranQuadrant1 = (int)random(0, 2);
    int ranSide1 = (int)random(0, 2);
    if (ranQuadrant1 == 0) 
      if (ranSide1 == 0) 
        x = random(0, -width/2);
      else {
        x = random(width, 3*width/2);
        y = random(-height/2, 3*height/2);
      } else 
    if (ranSide1 == 0) 
      y = random(0, -height/2);
    else {
      y = random(height, 3*height/2);
      x = random(-width/2, 3*width/2);
    }
  }
}

我像这样使用它

ArrayList<Enemy> enemies = new ArrayList<Enemy>();
void setup() {
  for (i = 0; i<difficulty; i++); 
    enemies.add(new Enemy(i*5));

  for (i = 0; i<enemies.size()-1; i++)
      enemies.get(i).respawn();
}
void draw() {
 for(i = enemies.size()-1; i>=0; i--) {
    enemies.get(i).rotateEnemy();
    enemies.get(i).move();
    enemies.get(i).drawEnemy();
    enemies.get(i).collisionDetect();
}

当我运行它时,三角形不会绘制。不仅如此,我写的一些省略号在尝试绘制三角形之后才会画出来。跟随你的鼠标和计时器以及其他东西的广场。请帮忙。谢谢! 现在,这不是整个计划。我为一个项目制作游戏,这些三角形是敌人。 如果你想看整个程序的上下文/如果我没有足够的,我把它放在一个pastebin:https://pastebin.com/Bfd4Fk6t

1 个答案:

答案 0 :(得分:0)

好吧我弄清楚了,原来我是个假人。 查看第二和第三点的rotateEnemy函数。没有y只有x。复制时我使用了大量的查找和替换,所以我必须摆脱y并用x替换它们。另一个错误是在drawEnemy中,我用参数(x1,y1,x2,x2,x3,x3)绘制三角形。再也不是了。 Geez我是一个聪明的家伙哈哈。