We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, i'd like to periodically(as in every x number of seconds) randomatise the integer 'direction' to give me a value between 0 and 3 so that the circles change direction and don't just get stuck in the walls... Does anyone know how to do this?
Cell[] Normal;
void setup() {
size(500, 500);
Normal = new Cell [50];
for (int i = 0; i < Normal.length; i++)
Normal[i] = new Cell(random(0, 500), random(0, 500), int(random(0, 4)));
}
void draw() {
background(255);
stroke(0);
strokeWeight(1);
for (int i = 0; i < Normal.length; i++)
Normal[i].desplay();
for (int j = 0; j < Normal.length; j++)
Normal[j].update();
}
class Cell {
float xpos;
float ypos;
int direction;
Cell(float tempXpos, float tempYpos, int tempDirection) {
xpos = tempXpos;
ypos = tempYpos;
direction = tempDirection;
}
void desplay() {
ellipse(xpos, ypos, 15, 15);
}
void update() {
if(xpos < width && ypos < height && xpos > 0 && ypos > 0) {
if(direction == 1) {
++xpos;
}
if(direction == 2) {
--ypos;
}
if(direction == 3) {
++ypos;
}
if(direction == 0) {
--xpos;
}
println(direction);
}
}
}
Answers
Take a look at the timer examples in this thread below:
http://forum.processing.org/two/discussion/3397/comparing-values-over-time
I'm afraid to admit i can't make hide or hair of the formally addressed examples...? :-/
Could you wright something simpler? I tried this using framerates... (line 52-56) For some reason it just stops after it's randomised 'direction' once... Any help you could offer?
In case you just wanted to change the direction of the balls then you could use something like this, no need for a timer.
Few notes of advice as well. Always close your for() cycles. Objects by convention are written with lower case. Also you can add multiple constructors. To change the direction you need to invert the speed of the balls. That is done with speed *= -1.