arrays - How to use callback function in processing? -
how phone call array after amount of time creates object @ intervals. want array create sphere every 3 seconds.
ball [] ball; void setup() { size(600,600,p3d); ball = new ball[3]; ball[0] = new ball(); ball[1] = new ball(); ball[2] = new ball(); } void draw() { background(255); (int = 0; < ball.length; i++) { ball[i].drawball(); } } class ball { float spheresize = random(30, 90); float spherex = random(-2200, 2800); float spherey = 0; float spherez = random(-2200, 2800); void drawball() { translate(spherex, spherey, spherez); sphere(spheresize); spherey +=1; } }
easiest way store time in variable using time keeping function millis().
the thought simple:
store previous time , delay amount keep track of time continuously if current time greather stored time , delay, delay interval has passed.here's simple sketch illustrate idea:
int now,delay = 1000; void setup(){ = millis(); } void draw(){ if(millis() >= (now+delay)){//if interval passed //do cool here println((int)(framecount/framerate)%2==1 ? "tick":"tock"); background((int)(framecount/framerate)%2==1 ? 0 : 255); //finally update store time = millis(); } }
and integrated code:
int ballsadded = 0; int ballstotal = 10; ball [] ball; int now,delay = 1500; void setup() { size(600,600,p3d);spheredetail(6);nostroke(); ball = new ball[ballstotal]; = millis(); } void draw() { //update based on time if(millis() >= (now+delay)){//if current time greater previous time+the delay, delay has passed, hence update @ interval if(ballsadded < ballstotal) { ball[ballsadded] = new ball(); ballsadded++; println("added new ball: " + ballsadded +"/"+ballstotal); } = millis(); } //render background(255); lights(); //quick'n'dirty scene rotation translate(width * .5, height * .5, -1000); rotatex(map(mousey,0,height,-pi,pi)); rotatey(map(mousex,0,width,pi,-pi)); //finally draw spheres (int = 0; < ballsadded; i++) { fill(map(i,0,ballsadded,0,255));//visual cue sphere's 'age' ball[i].drawball(); } } class ball { float spheresize = random(30, 90); float spherex = random(-2200, 2800); float spherey = 0; float spherez = random(-2200, 2800); void drawball() { pushmatrix(); translate(spherex, spherey, spherez); sphere(spheresize); spherey +=1; popmatrix(); } }
arrays arraylist processing
No comments:
Post a Comment