【问题标题】:New to classes and my class doesn't work. Why doesn't it?刚开始上课,我的课不工作。为什么不呢?
【发布时间】:2018-06-16 04:16:44
【问题描述】:

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

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

【问题讨论】:

  • 即使在您的 pastebin 中,您也不会显示程序的关键部分:向我们展示 translatetriangle 方法的代码
  • 糟糕,我忘记了处理与 java 的不同之处。我在处理中写了这个。它有点像一个java库。抱歉,我删除了 java 标签。
  • 好吧,我意识到我忘了初始化 x 和 y 变量。它画了……一些东西。这绝对不是我想要的三角形。
  • 以后请尝试发minimal reproducible example

标签: class arraylist processing


【解决方案1】:

好吧,我想通了,原来我是个假人。 查看第 2 点和第 3 点的 rotateEnemy 函数。没有y只有x。我在复制它时使用了很多查找和替换,所以我必须摆脱 y 并用 x 替换它们。另一个错误是在drawEnemy中,我用参数(x1,y1,x2,x2,x3,x3)绘制了三角形。不,又来了。哎呀,我是个聪明人哈哈。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多