[处理/Java]可见性/分层问题
我正在处理一个小草图,其中我使用时间函数制作一个“时钟”,并根据毫秒、秒和分钟在画布上绘制椭圆。我使用 for 循环来绘制所有省略号,每个 for 循环都位于其自己的方法内。我在绘图函数中调用每个方法。然而,由于某种原因,只有被调用的第一个方法被绘制,理想情况下我希望它们都被可见地渲染。
//setup program
void setup() {
size(800, 600);
frameRate(30);
background(#eeeeee);
smooth();
}
void draw(){
milliParticles();
secParticles();
minParticles();
}
//time based particles
void milliParticles(){
for(int i = int(millis()); i >= 0; i++) {
ellipse(random(800), random(600), 5, 5 );
fill(255);
}
}
void secParticles() {
for(int i = int(second()); i >= 0; i++) {
fill(0);
ellipse(random(800), random(600), 10, 10 );
}
}
void minParticles(){
for(int i = int(minute()); i >= 0; i++) {
fill(50);
ellipse(random(800), random(600), 20, 20 );
}
}
I'm working on a small sketch in processing where I am making a "clock" using the time functions and drawing ellipses across the canvas based on milliseconds, seconds and minutes. I'm using a for loop to draw all of the ellipses and each for loop is inside its own method. I'm calling each of these methods in the draw function. However for some reason only the first method that is called is being drawn, when ideally I would like to have them all being visibly rendered.
//setup program
void setup() {
size(800, 600);
frameRate(30);
background(#eeeeee);
smooth();
}
void draw(){
milliParticles();
secParticles();
minParticles();
}
//time based particles
void milliParticles(){
for(int i = int(millis()); i >= 0; i++) {
ellipse(random(800), random(600), 5, 5 );
fill(255);
}
}
void secParticles() {
for(int i = int(second()); i >= 0; i++) {
fill(0);
ellipse(random(800), random(600), 10, 10 );
}
}
void minParticles(){
for(int i = int(minute()); i >= 0; i++) {
fill(50);
ellipse(random(800), random(600), 20, 20 );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第一个方法是唯一被执行的方法,因为 for 循环停止必须为 false 的条件始终为 true(如果每次循环时向其添加 1,i 将始终 >= 0)。
我认为你想像这样修改你的 for 循环:
这样, i 最初将为 0-59,并且会减少直到 -1,此时 i >= 0 将为 false。然后执行将退出 for 循环并传递到下一个方法。
我认为这是您的初衷,除非您希望每个方法同时且无限期地运行(在这种情况下您应该使用线程)。
Your first method is the only one being executed because the condition which must be false in order for your for loop to stop is always true (i will always be >= 0 if you add 1 to it each time you loop).
I think you want to modify your for loops like so:
This way, i will initially be 0-59, and will decrease until it is -1, at which point i >= 0 will be false. Execution will then exit the for loop and pass to the next method.
I think this was your original intent, unless you wanted each method to run simultaneously and indefinitely (in which case you should use threads).