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.
IndexProgramming Questions & HelpSyntax Questions › newbie creating multiple images then editing
Page Index Toggle Pages: 1
newbie ? creating multiple images then editing (Read 672 times)
newbie ? creating multiple images then editing
Oct 11th, 2005, 6:52am
 
im new to processing still and to programming in general so thanks ahead of time for the patience.

what im trying to do is have a new leaf shaped polygon be created everytime the mouse is clicked where the mouse is clicked. ive created one sketch already that accomplished this but the problem came when i wanted them ALL to do something after being on the stage already. after a bunch  of these leaf polygons have been placed on the stage i went them all to move straight down to the bottom of the screen as if they were falling off a tree.

i attempted to do this by creating objects that are told to update whenever the mouse is clicked. then to have a counter going so that every 10 seconds it tells the objects to go to 0. this didnt work and i think im in over my head. ANY help or even a few pointers taht someone could give me would be much appreciated.


randy
what i have so far
Reply #1 - Oct 11th, 2005, 6:53am
 
HLine h1 = new HLine(mouseX, mouseY, 0);
HLine h2 = new HLine(10, 50, 2.5);


void setup()
{
 size(200, 200);
 background(204);
 framerate(30);
}


void draw() {
 background(204);
 int ref = 0;

   
 if(mousePressed) {
 ref++;
 println(ref);
 
 ("h" + ref).update();
 }
}

class HLine {
 float xpos, ypos, speed;
 HLine (float x, float y, float s) {
   xpos = x;
   ypos = y;
   speed = s;
 }
 

 
void update() {
   int sec = second();
    println(sec);
           
   if (sec == 0 || sec == 10 || sec == 20 || sec == 30 || sec == 40 || sec == 50) {
     for(ypos=ypos; ypos == 180; ypos++) {
     ypos = 180;
     }
   }  

   
   noStroke(); {  
     beginShape(POLYGON);
     vertex( 10+xpos , ypos);
     vertex( 20+xpos, 10+ypos);
     vertex( 30+xpos, 15+ypos);
     vertex( 20+xpos, 30+ypos);
     vertex( 10+xpos, 40+ypos);
     vertex( 0+xpos, 30+ypos);
     vertex( -5+xpos, 15+ypos);
     vertex( 0+xpos, 10+ypos);
     endShape();
       }
}
}
Re: newbie ? creating multiple images then editing
Reply #2 - Oct 11th, 2005, 1:34pm
 
Processing isn't like Flash (much). To iterate through a list of objects you need to stick those objects inside an array object.

I think you need to place timers inside the objects themselves. In the example I've knocked up the balls fall off screen after a few seconds sequentially. But I've put a condition inside the mouse listener to boost all timers to maximum if there's a given quantity of balls on screen.
Code:

Drop [] drop;
int threshold = 20;
void setup(){
size(200,200);
drop = new Drop[100];
for (int i = 0; i < drop.length; i++){
drop[i] = new Drop(0,200+10);
}
ellipseMode(CENTER);
smooth();
}
void draw(){
background(200);
for (int i = 0; i < drop.length; i++){
drop[i].draw();
drop[i].update();
}
}
class Drop{
float x,y;
int timer;
Drop(float x, float y){
this.x = x;
this.y = y;
timer = 0;
}
void draw(){
ellipse(x,y,10,10);
}
void update(){
if(timer < 500)
timer++;
else if (y < height+20)
y += 1.0;
}
void place(float x, float y){
this.x = x;
this.y = y;
timer = 0;
}
}
void mousePressed(){
int item = -1;
int items = 0;
for (int i = 0; i < drop.length; i++){
if (drop[i].y > height){
item = i;
}
else {
items++;
}
}
if (item > -1){
drop[item].place(mouseX,mouseY);
}
if (items > threshold){
for (int i = 0; i < drop.length; i++){
drop[i].timer = 500;
}
}
}
Re: newbie ? creating multiple images then editing
Reply #3 - Oct 12th, 2005, 6:14am
 
thanks a ton for the help. that really helped me out.

if you wouldnt mind though there are some parts of the code that i dont understand. could someone explain exactly what the particular line(s) are doing?

1) Drop [] drop;

i realize this is an array but im not used to seeing it written like this. what does this mean? and what is it doing?

2)drop = new Drop[100];

not sure what this line is doing really,


3) drop.length

why use drop.length? wouldnt 100 return the same results? or would it? doesnt the length part get the number of numbers in the array?

4) this.x

so i think this was one of my big problems. the "this" part allows it to only effect the one circle indvidualy correct?


again thanks for all the help. im really trying to figure this all out so id really appreciate the little extra help.

randy
Re: newbie ? creating multiple images then editing
Reply #4 - Oct 12th, 2005, 3:20pm
 
1 & 2) To instantiate an array on one line you would do this:

int [] num = new num[10];

Which gives us a ten part array. But what if I wanted to tell the constructor of a class that I wanted different length arrays for each object?

int [] num;

This will create an array ready to be defined in the constructor's properties. Then let's say an integer called quantity is passed to the constructor:

num = new int[quantity];

In the example I've given it doesn't look necessary, but it's how I always present code. It makes the program look like one big object (which is what it actually is). When I start dragging parts of code from one program to another, laying out everything so you only have to make a few alterations in one place becomes a real time saver.

3) Yet again I did this for readability and for portability. What if you wanted to test 200 or 300 objects? You'd have to go through the code changing every parameter. Now you only have to do it in one place.

4) Readability again. Most people will pass data to the constructor using dummy names. For me that's two lists of variables to check up when I'm trying to find out what's going into the constructor. I'd rather see the names of the actual properites being passed to the constructor than confuse myself. I can do this with this.property. It's refering to the object's property, not the new property of the same name that's just been created. Another trick which I've noticed JohnG use is to write the dummy variables with an underline before them:

class myClass{
 int variable;
 myClass(int _variable){
   variable = _variable;
 }
}

It looks like a lot more typing but I've got over a hundred sketches on my hard-drive. Dragging code from old sketches to new ones can be a real pain. Especially when you're wondering what the hell you were trying to do back then. Whenever I use parts of other peoples code I have to go through the stuff making the blocks {} tighter and putting spaces between math. That's just me and what I personally find readable.

Lastly, this.property isn't why the objects stay separate, the objects stay separate by themselves. When I wanted to affect them all I used for() to run through them all.
Page Index Toggle Pages: 1