图像背景不断重叠文本?



我试图在单击鼠标时弹出随机文本和背景,但是,当我单击时,文本显示不到半秒,然后出现新背景并隐藏文本

这是我按下鼠标的样子:

String mySentence = "Lose.txt";
String[] lose;
float mx = 20;
int posX = 0;
int posY = 0;
int butterflyX = 100;
int butterflyY = 100;
PImage v1;
PImage bf;
float xpos, ypos;
boolean playing = false;
//sentence
boolean showMySentence = false;
int mySentenceTimer = 0;
PImage [] backgrounds = new PImage[5];
int bg;
int currentBgNumber = 0;
void setup(){
size(800,501);

backgrounds = new PImage[5];
backgrounds[0] = loadImage("field.jpg");
backgrounds[1] = loadImage("galaxy.jpg");
backgrounds[2] = loadImage("tokyo.jpg");
backgrounds[3] = loadImage("water.jpg");
backgrounds[4] = loadImage("paris.jpg");
// mySentence = loadStrings(loseFile);
PFont myFont;
myFont = createFont("Futura", 30,true);
textFont(myFont);
fill(255);

}
void draw(){  
image(backgrounds[currentBgNumber], 0, 0);

if (showMySentence) {
fill(255);
textSize(20);
text(mySentence, width/2, height/2);
showMySentence = millis() < mySentenceTimer; 
}
void mousePressed() {
currentBgNumber++;
if (currentBgNumber>4)
currentBgNumber=0;

if (random(1) < .5) {
mySentence = "lose.txt" + "!";
mySentenceTimer = millis() + 3000; 
showMySentence = true;
}
}

据我所知,在后台运行后,文本应该运行之后,但我想不是。如果您能告诉我需要修复的内容,我将不胜感激,谢谢:)

正如承诺的那样(您可以将此代码段复制并粘贴到处理 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;
}
}

如果你有进一步的问题,我会闲逛。玩得愉快!

最新更新