使图像在 X 秒后消失(处理) [英] Make image disappear after X seconds (processing)

查看:66
本文介绍了使图像在 X 秒后消失(处理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个项目,我希望在 3 秒后弹出一个图像.弹出该图像后,用户必须单击该图像以弹出完成"图像,该图像将在 3 秒后自动消失.

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.

除了消失的部分,我已经完成了大部分工作.有谁知道如何让图像在 3 秒后消失?

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

推荐答案

您可以使用 millis() 函数或 frameCount 变量来检查已经过去了多少时间by,然后在 X 秒或 X 帧后做一些事情.

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.

您已经使用 showimagedone 变量完成了一些工作,但您需要使用该变量来有条件地绘制图像.

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.

相关帖子:

另请参阅处理参考了解更多信息.

Please also consult the Processing reference for more information.

如果您仍然无法使其正常工作,请在一个新问题中发布 MCVE(不是您的完整项目!)我们将从那里出发.祝你好运.

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.

这篇关于使图像在 X 秒后消失(处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆