[我试图单击鼠标时弹出随机的文本和背景,但是,当我单击时,在显示新背景并隐藏该文本之前,该文本显示的时间少于半秒钟
这是我的鼠标按下的样子:
void draw(){
image(backgrounds[currentBgNumber], 0, 0);
int range;
if (mousePressed) {
range = 50;
} else {
range = 10;
}
butterflyX = butterflyX + (int)random(-range,range);
butterflyY = butterflyY + (int)random(-range,range);
butterflyX = constrain(butterflyX,0,width);
butterflyY = constrain(butterflyY,0,width);
image(bf, butterflyX, butterflyY);
//v
image(v1, posX+500, posY);
float mx = constrain(10, 100, 200);
}
void mousePressed() {
mySoundFile.play();
int randomIndex = (int)random(lose.length);
String randomLose = lose[randomIndex];
currentBgNumber++;
if (currentBgNumber>4)
currentBgNumber=0;
String theSentence1 = randomLose;
if (random(1) < .5) {
text(theSentence1, width/2, height/2);
}
}
根据我的观察,在后台运行之后,文本应该在之后运行,但我想不是。如果您能告诉我需要解决的问题,我将不胜感激,谢谢:)
按照承诺(您可以将这一代码段复制并粘贴到Processing IDE中,然后它将运行。然后播放直到获得所需的结果并将其调整为自己的代码):
// a couple variables I'll use to manage the text's appearance
String mySentence = "";
boolean showMySentence = false;
int mySentenceTimer = 0;
void setup() {
size(500, 500);
}
void draw() {
// background paints over everything ~60 times per second (or whatever fps you did set up)
background(0);
// this would go at the end of the draw() loop
if (showMySentence) {
fill(255);
textSize(20);
text(mySentence, width/2, height/2);
showMySentence = millis() < mySentenceTimer; // flip the boolean after some time (2 seconds here)
}
}
void mousePressed() {
// this would replace the part of the mouseClicked() method where you draw the sentence
if (random(1) < .5) {
mySentence = "your sentence here";
mySentenceTimer = millis() + 2000; // show text but only for 2 seconds (I guessed that you might want that, but nevermind if that's not the case)
showMySentence = true;
}
}
如果您有其他问题,我会在身边闲逛。玩得开心!