【发布时间】:2018-05-24 12:36:01
【问题描述】:
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、函数、类)
"%c您是否尝试使用 p5.js 的 str() 函数?如果是这样,您可能希望将其移至您的草图的 setup() 函数中。\n\n有关详细信息,请参阅:github.com/processing /p5.js/wiki/Frequently-Asked-Questions#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup"
【问题讨论】:
-
请格式化代码和文本。使用编辑链接编辑问题。
-
尝试“使用严格”;正如您的错误消息所暗示的那样,位于您的代码之上。
标签: javascript arrays