问题
我注意到draw()循环被事件精化所打断。在下面的示例中,圆圈动画将在鼠标单击时停止,直到elaborating_function()结束。
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
void mouseClicked() {
elaborating_function();
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}当然,运行精化而不停止动画的简单解决方案可以是thread("elaborating_function");。
但我的问题是:是否有可能将抽签周期运行到一个独立的线程中?
溶液
我找到了一个可能的解决方案,解决了我的问题,并创建了一个与draw并行的“独立循环”。在此周期内,可以运行任何函数,并且不会干扰绘制执行。用户触发的每个事件只需要设置一个特定的变量,以便在周期内激活(一次或多次)函数。
int circle_x = 0;
int animation_speed = 5;
boolean call_elaborating_function = false;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
IndependentCycle independentCycle = new IndependentCycle();
independentCycle.setFrequency(1);
new Thread(independentCycle).start();
}
void draw(){
background(#818B95);
//simple animation
fill(0,116,217);
circle(circle_x, 200, 50);
circle_x += animation_speed;
if(circle_x>800){ circle_x = 0; }
}
public class IndependentCycle implements Runnable{
private int frequency; //execution per seconds
public IndependentCycle(){
frequency = 0;
}
public void setFrequency(int frequency){
this.frequency = 1000/frequency;
println(this.frequency);
}
public void run(){
while(true){
print(".");
delay(this.frequency);
//DO STUFF HERE
//WITH IF EVENT == ture IN ORDER TO RUN JUST ONCE !!
if(call_elaborating_function){
call_elaborating_function = false;
elaborating_function();
}
}
}
}
void mouseClicked() {
call_elaborating_function = true;
}
void elaborating_function(){
println("elaboration start");
delay(1000);
println("elaboration end");
}发布于 2021-06-05 15:20:35
据我所知,处理有自己的AnimationThread。
您提出的线程elaborating_function()解决方案很棒。如果需要更多的控制,您可以拥有一个implements Runnable的基本类。在这个线程并行运行的情况下,处理的主要动画线程应该沿着它的一侧运行,不会暂停呈现。
这个选项听起来比试图处理处理的AnimationThread简单得多,而且可能需要处理意想不到的行为。你想要达到的实际目标是什么?
https://stackoverflow.com/questions/67848214
复制相似问题