We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
loop array (Read 1269 times)
loop array
May 5th, 2010, 1:52am
 
hi,

I'm working with the http://processing.org/learning/2darray/ example. How can I loop the rectangles I have tried with translate, but it moves all the cubes and don't loop them.

Any help will be much appreciated.

Cheers
Re: loop array
Reply #1 - May 5th, 2010, 4:36am
 
What do you call "loop the rectangles"? Making them to rotate? Displaying them in sequence?
Re: loop array
Reply #2 - May 5th, 2010, 5:12am
 
Hi PhiLho,

displaying them in sequence is what I pretend. (I think, sorry I'm a noob)

Actually I'm moving the rectangles from left to right, but they disappear gradually and appear again once the last rectangle is out of the scene. But I want to repaint them on the left side.

Code:

void setup(){
 size(400,400);
}

int s = 40;
int f;

void draw(){
 movement();
 frameRate(30);
 background(255);
 for(int i=0; i<width; i++){
   if(i==width){i=0;}
   fill(random(125));
   rect(i*s+mov_fact, 150, s, s);}
   
}

float mov_fact;

void movement(){
 mov_fact = mov_fact + 10;
 if (mov_fact > width + s) {
   mov_fact = 0;
   }
}  


thanks!
Re: loop array
Reply #3 - May 5th, 2010, 7:06am
 
Quote:
frameRate(30);
Better to move that to setup(). That's the default anyway.
Quote:
if(i==width){i=0;}
Don't do that in a for loop!
Quote:
if (mov_fact > width + s)
You probably want if (mov_fact > width - s)
Re: loop array
Reply #4 - May 5th, 2010, 7:11am
 
Maybe you want to do something like this:
Code:
void setup(){
size(400,400);
frameRate(10);
}

int s = 40;
int boxNb = 7;
int f;

void draw(){
movement();
background(255);
for(int i=0; i<boxNb; i++){
fill(random(125));
int pos = (i*(s + 5) + mov_fact) % (width - s);
rect(pos, 150, s, s);
}
}

int mov_fact;

void movement(){
mov_fact += 10;
}
Re: loop array
Reply #5 - May 5th, 2010, 11:27am
 
Thanks a lot for the example! This comes very close to what I pretend. Just one thing, why are the rectangles jumping into the scene? It's possible to make a smooth fade?

I have changed (width - s) to (width) and now they are fading on the right side, but they are still jumping on the left..

Code:
void setup(){
size(400,400);
frameRate(10);
}

int s = 40;
int boxNb = 2;
int f;

void draw(){
movement();
background(255);
for(int i=0; i<boxNb; i++){
fill(random(125));
int pos = (i*(s + 5) + mov_fact) % (width);
rect(pos, 150, s, s);
}
}

int mov_fact;

void movement(){
mov_fact += 10;
}
Re: loop array
Reply #6 - May 6th, 2010, 1:21am
 
Code:
void setup() {
size(400,400);
frameRate(10);
}

int s = 40;
int boxNb = 7;
int f;

void draw() {
movement();
background(255);
for(int i = 0; i < boxNb; i++) {
fill(random(125));
int pos = (i*(s + 5) + mov_fact) % (width + s) - s;
rect(pos, 150, s, s);
}
}

int mov_fact;

void movement() {
mov_fact += 10;
}
Re: loop array
Reply #7 - May 6th, 2010, 2:14am
 
perfect! thanks a lot for your patience!
cheers
_J
Page Index Toggle Pages: 1