在一定时间内自主停止和启动动画吗?

时间:2019-02-22 17:03:04

标签: javascript class animation intervals

看看我在Trees类中的颤抖类:

class Trees {
  constructor(x, y) {
    this.x = x,
    this.y = y,
    this.l = this.x - 2,
    this.r = this.y + 2,
    this.a = 0

  }
  trunk() {
    noStroke();
    fill(126, 60, 1);
    rect(this.x + 3, this.y, 14, 40);
    triangle(this.x + 10, this.y + 30, this.x - 4, this.y + 40, this.x + 24, this.y + 40);
  }
  leaves() {
    fill(71, 215, 45);
    ellipse(this.x + 3, this.y - 5, 25);
    ellipse(this.x + 10, this.y - 15, 25);
    ellipse(this.x + 17, this.y - 5, 25);
  }
  shudder() {
    let d = dist(mouseX, mouseY, this.x, this.y);
    if (d < 50) {
      this.a = 4;
      if (this.x < this.l || this.x > this.r) {
        this.a = this.a * -1;
      }
    } else {
      this.a = 0;
    }
    this.x = this.x + this.a;
  }
}

如您所见,当我单击正在运行的树时,我正在来回摇晃这棵树!但是,我试图模拟它的晃动,好像它被斧头卡住了三遍一样。也就是说,我希望它开始抖动半秒钟,然后停止,开始,停止,开始然后永久停止。

如您所见,我已经尽力了:

automated shaking tree attempt 但是没有骰子。

我应该提到我正在使用p5库,但是除了mouseX和mouseY预定义函数之外,我认为它不会对此造成太大影响。

有人能解决这个难题吗?

非常感谢!

0 个答案:

没有答案
相关问题