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
using layers? (Read 566 times)
using layers?
Oct 21st, 2006, 7:08pm
 
hey folks
i'm writing a programm which shows images that move across the screen, every image makes a line but the problem is that i dont know how to make the images appear above the lines. the lines always hide the images :/

i hope anybody can help me and sorry for my bad english Smiley
Re: using layers?
Reply #1 - Oct 21st, 2006, 9:24pm
 
hey belton,

you have to draw the lines before you draw the images... it sounds like you're drawing and image then its lines, then another image then its lines....

what you need to do is: draw an image's lines, then draw another images lines, then draw all the images...

if you post your code i can have a look at it.

cheers,
tk
Re: using layers?
Reply #2 - Oct 21st, 2006, 11:38pm
 
hey cool thats my confusing codepart Cheesy


int m = (int) random (5);
if(m == 0){ ypos-- ; }
if(m == 1){ ypos++ ; }
if(m > 1 ){ xpos-- ; }
xposit[p] = xpos;
yposit[p] = ypos;
p = p + 1;
   
   
for (i=1; i<p; i = i+1) {
if(xpos > 20 && ypos > 110 && xpos< 580 && ypos<480 ){
image(img,xpos-10,ypos-10);
if(xposit[i] > 20 && yposit[i] > 110 && xposit[i] < 380 && yposit[i] <380 ){
           
fill (104,71,33);
ellipse(xposit[i], yposit[i],7,7);
}
}
}
     
Re: using layers?
Reply #3 - Oct 22nd, 2006, 12:58am
 
Hey Belton,

Here's what I came up with.

Notice it draws all the tails, and then all the images.

Processing layers things in the order they are drawn. For example, image 4 will always be below image 5;

Quote:
import processing.opengl.*;

MyImage[] images;

void setup()
{
 size(640, 480, OPENGL);       //size of window and render mode
 images = new MyImage[10];
 images[0] = new MyImage(5,8,60,40,30,"0.gif");
 images[1] = new MyImage(9,2,60,40,30,"1.gif");
 images[2] = new MyImage(2,6,60,40,30,"2.gif");
 images[3] = new MyImage(4,3,60,40,30,"3.gif");
 images[4] = new MyImage(6,9,60,40,30,"4.gif");
 images[5] = new MyImage(7,4,60,40,30,"5.gif");
 images[6] = new MyImage(5,6,60,40,30,"6.gif");
 images[7] = new MyImage(8,7,60,40,30,"7.gif");
 images[8] = new MyImage(3,1,60,40,30,"8.gif");
 images[9] = new MyImage(1,5,60,40,30,"9.gif");
}

void draw()
{
 background(33);
 frameRate(15);
 update();
 display();
}

void update(){
 for(int i = 0; i < images.length; i++){
   images[i].update();
 }
}

void display(){
 noStroke();
 for(int i = 0; i < images.length; i++){
   images[i].drawTail();
 }
 for(int i = 0; i < images.length; i++){
   images[i].drawImg();
 }
}

class MyImage{
 int[][] pos;
 int dx;
 int dy;
 int w;
 int h;
 int l;
 PImage img;

 MyImage(int dx, int dy, int w, int h, int l, String imgName){
   this.dx = dx;
   this.dy = dy;
   this.w = w;
   this.h = h;
   this.l = l-1;
   pos = new int[this.l+1][2];
   img = loadImage(imgName);
 }

 void update(){
   checkBounds();
   shiftPos();
   addPos();
 }

 void drawTail(){
   for(int i = 0; i < l; i++){
     fill(255,255*i/l);
     rect(pos[i][0],pos[i][1],w*i/l,h*i/l);
   }
 }
 
 void drawImg(){
   image(img,pos[l][0],pos[l][1]);
 }
 
 private void checkBounds(){
   if(pos[l][0] < 0 || pos[l][0] > width-w)
   dx*=-1;
   if(pos[l][1] < 0 || pos[l][1] >     height-h)
   dy*=-1;
 }

 private void shiftPos(){
   for(int i = 1; i < l+1; i++){
     pos[i-1][0] = pos[i][0];
     pos[i-1][1] = pos[i][1];
   }
 }

 private void addPos(){
   pos[l][0] = pos[l-1][0]+dx;
   pos[l][1] = pos[l-1][1]+dy;
 }
}
Page Index Toggle Pages: 1