Make image disappear after X seconds (processing)

2019-08-23 09:56发布

I'm currently working on a project in which I want an image to pop up after 3 seconds. Once that image has popped up the user has to click on the image to make a "done" image pop up that will disappear automatically after 3 seconds.

I've got most of it working except for the disappearing part. Does anyone know how I can time the image to disappear after 3 seconds?

PImage medic;
PImage medicD;

float time;
float startTime;

final int waitpopup = 3000;
final int DISPLAY_DURATION = 3000;

boolean showimage = true;
boolean showclock = true;
boolean showimagedone = true;
boolean hasClicked;

Clock clock;

void setup (){
size (1080, 1920);

 medic = loadImage("medic.png");
 medicD = loadImage("medicD.png");
 clock = new Clock(width /2, height /2);
 time = millis();
} 

void draw() {
 background (0);
 imageMode(CENTER);

 if (showclock) clock.display();

 if (showimage && millis() - time > waitpopup) {
   image(medic, width/2, height/2, 540, 540);
   } if (hasClicked == true) {
       showimage = false;
       image(medicD, width/2, height/2, 540, 540);
     } if (millis() > startTime + DISPLAY_DURATION) {
       showimagedone = false;
     }     
}

 void mousePressed() {
   hasClicked = true; 
   startTime = time;
 }

标签: processing
1条回答
老娘就宠你
2楼-- · 2019-08-23 10:21

You can use the millis() function or the frameCount variable to check how much time has gone by, then do something after X seconds or after X frames.

You're already doing some of the work with the showimagedone variable, but you need to use that variable to conditionally draw your image.

I recommend starting with a simpler example and getting that working. Here's one example:

int clickedFrame;
boolean on = false;
int duration = 60;

void draw(){

  if(on){
    background(255);
    if(frameCount > clickedFrame + duration){
     on = false; 
    }
  }
  else{
   background(0); 
  }
}

void mousePressed(){
  clickedFrame = frameCount;
  on = true;
}

This code show a white background for one second whenever the user clicks the mouse. You need to do something similar with your images.

Related posts:

Please also consult the Processing reference for more information.

If you still can't get it working, please post a MCVE (not your full project!) in a new question and we'll go from there. Good luck.

查看更多
登录 后发表回答