class Target {
constructor(_x, _y, _toul, _ardh, _color, _health) {
this.x = _x;
this.y = _y;
this.toul = _toul;
this.ardh = _ardh;
this.col = _color;
this.health = _health;
}
destroy() {
if (this.health === 0) {
this.x = 900;
this.y = 900;
}
}
//colide w change color
show() {
fill(this.col);
rect(this.x, this.y, this.toul, this.ardh);
}
}
function setup() {
createCanvas(1000, 1000);
var target = [];
for (var i = 0; i < 31; i++) {
if (i < 11) {
var x = 10 + 10 * i;
var y = 20;
var toul = 10;
var aredh = 10;
var col = color(255, 0, 0);
var health = 3;
}
if (i < 21) {
x = 10 + 10 * i - 110;
y = 40;
toul = 10;
ardh = 10;
health = 2;
col = color(0, 255, 0);
}
if (i < 31) {
x = 10 + 10 * i - 210;
y = 50;
toul = 10;
ardh = 10;
health = 1;
col = color(0, 0, 255);
}
target[i] = new Target(x, y, toul, ardh, col, health);
}
}
function draw() {
for (var i = 0; i < target.length; i++) {
target[i].show();
}
}
任何人都可以告诉我这个问题以及如何解决它,这是我第一次尝试面向对象编程。 我试图创造一个游戏:3行矩形,每行都有自己的颜色和健康等等
但我收到此错误: 3:未捕获的SyntaxError:在严格模式之外尚不支持块范围的声明(let,const,function,class)
&#34;%c您是否只是尝试使用p5.js的str()函数?如果是这样,您可能希望将其移动到草图的setup()函数中。\ n \ n有关详细信息,请参阅:github.com/processing/p5.js/wiki/Frequently-Asked-Questions#why-斜面-I-分配变量-使用-P5-函数和变量先于设置&#34;
答案 0 :(得分:0)
您在setup()中本地声明了您的目标数组,因此当您尝试在draw()中访问它时,它不存在。在setup()之上全局声明它并且应该修复它:
var target = [];
function setup() {
createCanvas(1000, 1000);
...