简体   繁体   English

使用setTimout()在Processing.js中创建延迟将不起作用

[英]Creating a delay in Processing.js with setTimout() won't work

I am writing a small javascript application that simulates passing books around a circle. 我正在编写一个小型javascript应用程序,用于模拟绕一圈传书。 The code can be found here: http://anura28.github.io/HCF/ 可以在这里找到代码: http : //anura28.github.io/HCF/

If a box receives the book, it is coloured green. 如果有盒子收到这本书,它会变成绿色。

Currently, when you click a number, the passing-of-the-book itself is not very visible. 当前,当您单击数字时,帐簿本身并不十分明显。 Instead, every box that will receive the book highlights at the same time. 取而代之的是,将同时收到该书的每个框都会突出显示。 This is because I haven't figured out how to create a delay between when a box is coloured green and the next box is identified. 这是因为我还没有弄清楚如何在一个绿色框和一个下一个框被标识之间创建延迟。

This is the code that draws the green boxes: 这是绘制绿色框的代码:

void drawGreenSquares(int number)
{
  userDefinedSetup();

  int hcf = gcd(totalBoxes, number);
  int i;
  float theta;
  float arclength = 0;
  float boxSize = 0.036615134 * width;
  float radius = ((totalBoxes) * (boxSize))/ (2 * PI);
  //  start = millis();
  for (i = 0; i < (totalBoxes/hcf) ; i++)
  {
    theta = (arclength * number)/ radius;
    pushMatrix();
    translate(width/2, height/2);
    translate(radius*cos(theta), radius*sin(theta));
    rotate(theta);
    fill(156, 255, 0, 100);
    rectMode(CENTER);
    rect(0, 0, boxSize, boxSize);
    popMatrix();
    arclength += boxSize;
  }
}

setTimeout(function(){ }, delay) throws an error in processing saying "{ is unexpected}" setTimeout(function(){},delay)在处理中引发错误,提示“ {是意外}”

How do I create a delay between drawing each green rectangle? 如何在绘制每个绿色矩形之间创建延迟?

The rest of my code can be found at https://github.com/anura28/anura28.github.com/blob/master/HCF/jam_modulus.pde 我的其余代码可以在https://github.com/anura28/anura28.github.com/blob/master/HCF/jam_modulus.pde中找到

I think you're mixing js with Processing in a way that it doesn't support it. 我认为您正在以不支持js的方式将js与Processing混合使用。 I've never personally used the setTimout function to do this kind of task. 我从来没有亲自使用setTimout函数来执行此类任务。 Here's what you should think about when doing this: 这是您执行此操作时应考虑的事项:

If what you want is a "gradual" coloring of the box, then what you want to do is set a gradient in color from your starting color to the end, and you increase it little by little on each step. 如果您想要的是框的“渐变”着色,那么您想要做的是设置从起始颜色到末尾的颜色渐变,并在每个步骤中一点一点地增加它。

However, I think what you mean is literally wait a couple of seconds before going from one color to the next. 但是,我认为您的意思是从一种颜色切换到另一种颜色之前要等待几秒钟。 In this case, I suggest using frameCount or millis() as a conditional and do something like this: 在这种情况下,我建议使用frameCount或millis()作为条件,并执行以下操作:

pseudo-code: 伪代码:

void setup() {
    ...
    double initial_time = millis();
    boolean delay_box = false;
    ...
}
void draw() {
    ...
    if (box event gets activated) {
        initial_time = millis();
        delay_box = true;
    }
    ...
    other stuff
    ...
    if (delay_box && millis() > initial_time + 5000) {
        draw_delayed_box();
        delay_box = false;
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM