我最近开始使用ProcessingJS库在Javascript中编写代码。我完成了可汗学院的JS课程。当x和y在所选区域时,我有一个功能。它应该停止循环,移动x和y,并将score1X和score1Y移动到0然后开始循环。得分1X和得分1Y没有移动。该函数在draw =函数中调用。非常感谢!
var CreatePoint = function(score1X, score1Y) {
if (x >= score1X - 10 && x <= score1X + 10 && y >= score1Y - 10 && y <= score1Y + 10){
noLoop();
points ++;
x -= 10;
y -= 10;
score1X = 0;
score1Y = 0;
loop();
}
else if (points <= 0) {
ellipse(score1X, score1Y, 20, 20);
}
};
draw = function() {
fill(255, 255, 255);
text(points + "/5", 20, 20);
CreatePoint(score1X, score1Y);
CreatePoint(score2X, score2Y);
CreatePoint(score3X, score3Y);
CreatePoint(score4X, score4Y);
CreatePoint(score5X, score5Y);
};
答案 0 :(得分:0)
通过分配函数参数变量,您无法影响调用者的变量,因为Javascript参数是通过引用传递的。您需要返回新值,调用者可以将这些值分配给变量。
var CreatePoint = function(score1X, score1Y) {
if (x >= score1X - 10 && x <= score1X + 10 && y >= score1Y - 10 && y <= score1Y + 10){
noLoop();
points ++;
x -= 10;
y -= 10;
score1X = 0;
score1Y = 0;
loop();
}
else if (points <= 0) {
ellipse(score1X, score1Y, 20, 20);
}
return [score1X, score1Y];
};
draw = function() {
fill(255, 255, 255);
text(points + "/5", 20, 20);
var res = CreatePoint(score1X, score1Y);
score1X = res[0];
score1Y = res[1];
res = CreatePoint(score2X, score2Y);
score2X = res[0];
score2Y = res[1];
res = CreatePoint(score3X, score3Y);
score3X = res[0];
score3Y = res[1];
res = CreatePoint(score4X, score4Y);
score4X = res[0];
score4Y = res[1];
res = CreatePoint(score5X, score5Y);
score5X = res[0];
score5Y = res[1];
};