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
snow (Read 565 times)
snow
Dec 23rd, 2007, 5:33pm
 
hello!
i´m a complete beginner, tried to make falling snow, now there´s 2 things missing:
1. don´t know how to put a limit, if(x>600) ... which statement i need to put, that the sequence starts from the beginning?
and 2. how i do it without having to draw all the snowflakes? i think with an array (which i don´t understand in the manual)....
somebody can please, please, help me with the code?
Smiley please ?
carmen


float x, y;

void setup()
{
 size(800,600);
 smooth();
 noStroke();
 frameRate(60);
}

void draw()
{
 background(102);
 
 x = x + 0.06;
 y = y + 0.2;
 

 
 translate(x, y);
 fill(255);



ellipse(x+50, y+10, 5, 5);
 ellipse(x, y, 4, 4);
  ellipse(x+110, y+40, 5, 5);
 ellipse(x+200, y+150, 4, 4);
 
  ellipse(x+550, y+210, 5, 5);
 ellipse(x+500, y, 4, 4);
  ellipse(x+510, y+40, 5, 5);
 ellipse(x+500, y+150, 4, 4);
 
  ellipse(x+250, y+30, 5, 5);
 ellipse(x, y, 4, 4);
  ellipse(x+210, y+20, 5, 5);
 ellipse(x+240, y+50, 4, 4);
 
  ellipse(x+550, y+210, 5, 5);
 ellipse(x+500, y+20, 4, 4);
  ellipse(x+510, y+140, 5, 5);
 ellipse(x+500, y+150, 4, 4);
 
  ellipse(x+750, y+110, 5, 5);
 ellipse(x+700, y+59, 4, 4);
  ellipse(x+580, y+60, 5, 5);
 ellipse(x+650, y+150, 4, 4);
 
  ellipse(x+670, y+210, 5, 5);
 ellipse(x+600, y, 4, 4);
  ellipse(x+610, y+40, 5, 5);
 ellipse(x+580, y+150, 4, 4);
}

Re: snow
Reply #1 - Dec 23rd, 2007, 9:57pm
 
hi carmen,

1. use the constrain method to limit x :
http://processing.org/reference/constrain_.html

Code:
ellipse(constrain(x+50, 0, 600), y+10, 5, 5);
// instead of :
ellipse(x+50, y+10, 5, 5);


2. since your snowflakes have different position and size, you should create a custom class with these parameters :

Code:
class Snowflake {

float x, y; // each snowflake has its own position
int size; // and size

// this is a simple constructor
public class Snowflake(float x, float y, int size) {
this.x = x; this.y = y; this.size = size;
}

}


then, create new snowflakes in the setup() method and store them in a array :

Code:
// declare an array of 24 Snowflake objects
Snowflake[] snowflakes = new Snowflake[24];

// fill the array with new snowflakes :
snowflakes[0] = new Snowflake(50, 10, 5);
// ...
snowflakes[23] = new Snowflake(580, 150, 4);


int the draw() method, move and display the snowflakes using a for-loop :

Code:
for (int i = 0; i < snowflakes.length; i++) {
snowflakes[i].x += x;
snowflakes[i].y += y;
ellipse(constrain(snowflakes[i].x, 0, 600), snowflakes[i].y, snowflakes[i].size, snowflakes[i].size);
}
Page Index Toggle Pages: 1