处理循环
问题描述:
int objectX = width/2;
int objectY = height/2;
int snelheidY = 1;
int score = 0;
int richting = 1;
int positiebal;
int bal = ellipse(objectX, objectY, 50, 50);
lost = false;
void setup() {
size(400, 400);
positiebal = height/2;
textSize(12);
}
void draw() {
background(0, 0, 0);
ellipse(positiebal, objectY, 50, 50);
if(objectY > 375)
snelheidY = -snelheidY;
if(objectY<25)
snelheidY = -snelheidY;
objectY = objectY + snelheidY;
text("score = " +score,4,10);
if (score < 0)
{ textSize(20);
text("play again",50,50);
noLoop();
lost = true;
textSize = 13;
}
}
void mousePressed() {
int distance = dist(200, objectY, mouseX, mouseY);
if (distance<=25)
//score hoger maken met 1 punt
{ score=score+1;
if (snelheidY < 0)
{ snelheidY = snelheidY -1; }
{ snelheidY = snelheidY+1; }
}
// score met 1 punt lager maken
else
{ score = score - 1;
if (snelheidY > 1)
{ snelheidY = snelheidY -1; }
}
if (lost == true)
{ snelheidY = 1;
score = 0;
positiebal = height/2;
richting = 1;
lost= false;
loop();}
}
我为整个过程做了一个循环,在得分< 0之后重新启动,但第二次不起作用。它第一次工作正常,但第二次只停止游戏而没有显示重新启动文本,也没有重新启动。处理循环
答
我不会为此使用loop()
和noLoop()
。相反,将您的状态存储在一组变量中,并使用这些变量绘制每个帧。这里有一个简单的例子:
boolean start = true;
boolean play = false;
boolean end = false;
void draw() {
background(0);
if (start) {
text("start", 20, 20);
} else if (play) {
text("play", 20, 20);
} else if (end) {
text("end", 20, 20);
}
}
void mousePressed() {
if (start) {
start = false;
play = true;
} else if (play) {
play = false;
end = true;
} else if (end) {
end = false;
start = true;
}
}
感谢您的帮助,但我们必须使用循环来解决我们的问题,这是该项目的一部分... – Beastmaster64
@ Beastmaster64你肯定是指“使用循环解决你的问题”使用'loop'和'noLoop()'或者它可能意味着使用'for'循环?如果您需要帮助,请发布[mcve]而不是整个项目。 –
所以这是一个学校项目?另外,'loop'和'noLoop'函数在哪里? –