That works -- although you probably don't need to redraw all your layers 30 times a second. Keep in mind that you can also translate layers either using image() offsets or with layer[i].translate().
However, note that an offset or translated PGraphics is still just a pre-rendered image -- if you drew things off the edge of the PGraphics, they won't become visible if you move it around.
Here are demonstrations of some of these concepts -- drawing once, and moving pre-drawn images.
This gets even easier if you use classes instead of arrays.
PGraphics[] layers = new PGraphics[5];
PVector[] off = new PVector[5];
int xdir = 1;
void setup() {
size(200, 200);
for (int i = 0; i < off.length; i++) {
off[i] = new PVector(0,0);
}
for (int i = 0; i < layers.length; i++) {
layers[i] = createGraphics(width, height);
}
layerSetup();
}
void draw(){
background(255);
for (int i = 0; i < layers.length; i++) {
image(layers[i],off[i].x,0);
}
off[1].x = off[1].x + xdir;
off[2].x = off[2].x + xdir;
if(off[2].x > width || off[2].x < 0){
xdir *= -1;
}
}
void layerSetup(){
layers[0].beginDraw();
layers[0].fill(0);
layers[0].rect(0,0,width/2,height);
layers[0].endDraw();
layerEllipse(layers[1], 55, 55,150, 55, color(125,125,125));
layerEllipse(layers[2], 55, 55, 55, 55, color( 0,125,125));
}
void layerEllipse(PGraphics pg, float a, float b, float c, float d, color col){
pg.beginDraw();
pg.fill(c);
pg.ellipse(a,b,c,d);
pg.endDraw();
}
Answers
;-)
You really nee to learn to write longer text to ask a question.
This is important in real life and here in the forum.
Follow at least these steps:
Chrisir
Some approaches to layers:
There are many ways to do layers. Some are very simple, some are very complex. It depends on what you want to do.
like this ? :D
well, a lot of this can go into setup() - I fixed that
counting starts with 0 not with 1 (so why 1 in line 13 above) - I didn't fix that
Good job!
That works -- although you probably don't need to redraw all your layers 30 times a second. Keep in mind that you can also translate layers either using image() offsets or with layer[i].translate().
However, note that an offset or translated PGraphics is still just a pre-rendered image -- if you drew things off the edge of the PGraphics, they won't become visible if you move it around.
Here are demonstrations of some of these concepts -- drawing once, and moving pre-drawn images.
This gets even easier if you use classes instead of arrays.