function Apple() {
this.x = 0;
this.y = 0;
this.show = function() {
fill(255, 0, 0);
noStroke();
rect(this.x, this.y, scl, scl);
}
this.create = function() {
var cols = fieldWidth/scl;
var rows = fieldHeight/scl;
do {
this.x = floor(random(cols))*scl+15;
this.y = floor(random(rows))*scl+30;
} while (!empty());
}
function empty() {
for (var i = 0; i < s.body.length; i++) {
if (this.x == s.body[i].x & this.y == s.body[i].y) {
return false;
}
}
return true;
}
}
这是我的蛇游戏中苹果对象的代码。 empty()函数用于确保在create()函数中苹果不会在蛇内产卵。目前无法正常工作,我该如何解决?
答案 0 :(得分:1)
我相信您输入了逻辑AND运算符。
您应该使用&&
而不是&
这是您的代码应如何:
function Apple() {
this.x = 0;
this.y = 0;
this.show = function() {
fill(255, 0, 0);
noStroke();
rect(this.x, this.y, scl, scl);
}
this.create = function() {
var cols = fieldWidth/scl;
var rows = fieldHeight/scl;
do {
this.x = floor(random(cols))*scl+15;
this.y = floor(random(rows))*scl+30;
} while (!empty());
}
function empty() {
for (var i = 0; i < s.body.length; i++) {
if (this.x == s.body[i].x && this.y == s.body[i].y) {
return false;
}
}
return true;
}
}