首页 > 解决方案 > p5.j​​s 库函数不能与 javascript 类函数一起使用?

问题描述

我正在为我的代码使用 p5 库,当我尝试使用库函数创建类函数时,它似乎中断了。我去了参考页面,甚至那里的示例类函数都使用库函数,所以我知道我做错了什么,但我似乎无法弄清楚是什么。

这是我的代码:

class Enemy {
  constructor(x, y, radius, health) {
    this.x = x
    this.y = y
    this.health = health
    this.r = radius
    this.show();
  }
//  this breaks on strokeWeight();
  show() {
    strokeWeight(4);
    stroke(255, 0, 0);
    noFill();
    ellipse(this.x, this.y, this.r * 2, this.r * 2;
  }
}

这是我得到的错误:

Uncaught ReferenceError: strokeWeight is not defined (sketch: line 11)

它说当我尝试注释掉strokeWeight();, stroke();,noFill();或时每个函数都没有定义ellipse();。如果你能帮忙,请做。谢谢。

标签: p5.js

解决方案


我想到了。我需要将类放在类setup()中对象的初始化之后但之前。


推荐阅读