【问题标题】:Image background keep overlapping the text?图像背景一直与文字重叠?
【发布时间】:2020-05-19 03:26:07
【问题描述】:

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

这是我按下鼠标的样子:

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;
            }

      }

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

【问题讨论】:

  • 那是因为draw() 循环会立即覆盖它。为了使您的文本绘制得更长,它也必须在 draw() 循环中重新绘制。我会给你写一个代码 sn-p 来展示你可以解决这个问题的方法。
  • 哦,有道理。谢谢,我试试看!

标签: image-processing processing


【解决方案1】:

如所承诺的(您可以将此 sn-p 复制并粘贴到处理 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;
  }
}

如果您还有其他问题,我会随时待命。玩得开心!

【讨论】:

  • 您好,谢谢!我现在正在尝试在 mousePressed 循环中将文本串在一起,但文本列表没有显示,而是只显示“lose.txt!”而且我似乎无法将我的 randomLose 文件添加到句子语句中。再次感谢您的帮助:) ``` mySentence = "lose.txt" + "!"; ```
  • @SamanthaSalanga 请更新您的帖子,提供新信息和您想要的内容,这样我会更容易解决。
  • 我想我快搞定了!我不得不移动并修复我的一些代码。再次感谢你! :)
  • 再次感谢您的帮助!
猜你喜欢
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 2020-07-31
  • 1970-01-01
相关资源
最近更新 更多