bioscribble
YaBB Newbies
Offline
Posts: 11
Re: Combining multiple sketches
Reply #1 - May 31st , 2007, 4:41am
Hey Ilias, I imagine you've used flash before and nested movie clips within each other. There isn't an identical structure in Processing that I'm aware of, but combining different sketches is as easy as separating their functionality out into different classes. You can then add instances of each class to your processing sketch and combine many, many different "sketches." It's not the same as loading a movie clip, but once you get a hang of the concept, you'll likely find it just as easy to do. Also, classes behave in a much more predictable fashion than nested movie clips. Best of luck. The following example uses two classes within a single sketch, one is a springy square, the other an easing circle: //make variables for the "sketches" we want to use //these "sketches" are actually just classes defined later in the code //I'd recommend using tabs to organize the different classes coolSketchOne sketch; coolSketchTwo sketch2; void setup(){ //setup works as usual size(400,400); background(255); //instatiate our classes sketch = new coolSketchOne(width, height); sketch2 = new coolSketchTwo(width, height); } void draw(){ background(255); //call the happen function (also called a method) within each sketch sketch.happen(); sketch2.happen(); } void mousePressed(){ //call the reset function within each sketch sketch.reset(); sketch2.reset(); } //here is where the different drawing behaviors are defined //each class contains a set of behaviors for itself public class coolSketchOne{ private int w, h; private float x, y; private float vx, vy; coolSketchOne(int w, int h){ this.w = w; this.h = h; } public void move(){ //easing motion vx = (mouseX - x)/6; vy = (mouseY - y)/6; x += vx; y += vy; } public void render(){ noFill(); ellipse(x, y, 4, 4); } public void happen(){ move(); render(); } public void reset(){ //move to bottom of screen x = mouseX; y = height; } } public class coolSketchTwo{ private int w, h; private float x, y; private float vx, vy; private float ax, ay; coolSketchTwo(int w, int h){ this.w = w; this.h = h; } public void move(){ //springing motion ax = (mouseX - x)/32; ay = (mouseY - y)/32; vx += ax; vy += ay; x += vx; y += vy; vx *= .9; vy *= .9; } public void render(){ noFill(); rect(x-4, y-4, 8, 8); } public void happen(){ move(); render(); } public void reset(){ //move to a random position x = random(w); y = random(h); } }