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
Class Confusion (Read 529 times)
Class Confusion
Sep 21st, 2008, 11:09pm
 
Hi,

I just started working though the chapter on Classes and Objects in the Processing book. The below example is basically a modification of example 43-04 from the Processing book. Essentially, I'm trying to use objects to create random spheres to appear. I'm unsure of how to reference the sp=new Spot to continue to draw spots via the void draw command. Without classes, this would be an easy task. Any help would be greatly appreciated. Thanks!

Spot sp;

void setup () {
 size (600,600);
 smooth ();
 noStroke();
 sp = new Spot(random (0,500), random (0,500),33);
}
 

void draw() {
 background (0);
 sp.display ();
}

class Spot {
 float x, y;
 float diameter;



 Spot (float xpos, float ypos, float dia) {
   x = xpos;
   y = ypos;
   diameter = dia;
   }


 void display () {
   ellipse (x,y, diameter, diameter);
 }
}
Re: Class Confusion
Reply #1 - Sep 22nd, 2008, 4:38pm
 
You can create an array of spots, just as you would create an array of numbers in any procedural language.  The draw() method then simply iterates through this array, drawing each spot in turn. The class Spot remains unchanged.

Quote:
Spot[] spots; 
 
void setup () {
  size (600,600);
  smooth ();
  noStroke();
 
  int numSpots = 25;
  spots = new Spot[numSpots];
  
  for (int i=0; i<numSpots; i++) {
    spots[i] = new Spot(random (0,500), random (0,500),33);
  }

    
void draw() {
  background (0);
  for (int i=0; i<spots.length; i++)  {
    spots[i].display (); 
  }

 
class Spot {
  float x, y;
  float diameter;
   
  Spot (float xpos, float ypos, float dia) {
    x = xpos; 
    y = ypos; 
    diameter = dia; 
    } 
  
  void display () {
    ellipse (x,y, diameter, diameter);
  } 
}

Re: Class Confusion
Reply #2 - Sep 23rd, 2008, 7:54am
 
First of all, thank you for taking the time to answer that question. Frankly, as someone new to Processing I haven’t dealt much with arrays yet either.  Anyway, this is the perfect chance to learn.

However, the gist of my question was about timing, which I wasn’t too clear about. Is there a way to use a command like frameRate() in void setup (), so that it iterates through the array one ellipse at a time, rather than instantly?

My question probably demonstrates my lack of understanding of arrays.

Thanks for your help.
Re: Class Confusion
Reply #3 - Sep 23rd, 2008, 8:31am
 
Hey Dialogic,

I'm not completely sure what you mean by 'continue to draw spots by the void draw command.' Any code inside of draw(){} will be executed repeatedly until you exit the program. If you want a single Spot drawn every frame at a random position, you'll need to change the Spot's position every frame.

One way to do this would be to manually set the x and y values of your Spot every frame. This kind of defeats the purpose of encapsulating values in your class.
Code:

void draw(){
spot.x = random(0, width);
spot.y = random(0, height);
spot.display();
}


Instead, you could define a way for your Spot to move inside the Spot class, and simply tell the Spot to move every frame. Since the Spot knows how it moves about, you don't have to worry about the details of how it gets to its new position. In the future, you'll be able to create a lot of Spots (like in the array example) and never worry about there position values, instead just telling them each to move() and display().

Code:

//in your Spot class, add a move method
class Spot{
...

void move(){
x = random(0,width);
y = random(0,height);
}

...
}

//in your draw() function, call the move method

void draw(){
spot.move(); //this is our new method
spot.display();
}


Have fun experimenting with different implementations of the move() function. Try adding (or subtracting) one to the x value on every move, or changing the range the Spot bounces around in.
Re: Class Confusion
Reply #4 - Sep 25th, 2008, 6:45pm
 
Hi,

I have a similar problem (I think) so I'm adding to this thread rather than creating a new one. For my own personal edification and enrichment, I'm making a clone of a flash game called gravity (link: http://adamatomic.com/gravity/ ). Right now I've succeeded in generating random mines (called "anchors") and spreading 6 of them throughout the stage. These anchors are each objects of a Class Anchor (basically a sprite).

Now I have a "rope" that is SUPPOSED to draw a line from itself to the first child of Anchor class "anc1" but for some reason it's not working. All I get is the below screenshot. Looking at my code, it seems that there are some serious redundancy issues, but I don't know how to solve it. Some help would be great. I have also since ordered the orange-colored processing book off Amazon.

Code:

//Class Anchor has 6 children.

Anchor anc1;
Anchor anc2;
Anchor anc3;
Anchor anc4;
Anchor anc5;
Anchor anc6;

Rope rp; // A single line that draws from nearest anchor to mouse.

PImage anchor; //We have a sprite that will be implemented later.

float xpos;
float ypos; //I am unsure if I need these.

void setup()
{
size(240,480);
background (110, 110, 120);
stroke(25,30,0);
line (0, 470, 240, 470); //this is the "ground"
noLoop(); //if this is removed, the program will never stop adding anchors.
}

void draw()
{

xpos = random(10,200);
ypos = random(20,440); //generate random location.
anc1 = new Anchor(xpos, ypos); //puts a new anchor at this location.
anc1.display (); //anc1 child uses display function.
rp = new Rope(xpos, ypos, mouseX, mouseY); // New rope? it should draw from anc1 to the mouse.
rp.display (); //display the rope.

xpos = random(10,200);
ypos = random(20,440);
anc2 = new Anchor(xpos, ypos);
anc2.display ();

xpos = random(10,200);
ypos = random(20,440);
anc3 = new Anchor(xpos, ypos);
anc3.display ();

xpos = random(10,200);
ypos = random(20,440);
anc4 = new Anchor(xpos, ypos);
anc4.display ();

xpos = random(10,200);
ypos = random(20,440);
anc5 = new Anchor(xpos, ypos);
anc5.display ();

xpos = random(10,200);
ypos = random(20,440);
anc6 = new Anchor(xpos, ypos);
anc6.display ();


}

class Anchor {

float x, y;

Anchor (float xpos, float ypos) {
x = xpos;
y = ypos;
}

void display () {
anchor = loadImage("anchorgrn.gif");
image (anchor, xpos, ypos);
}
}

class Rope {

float x1, y1, x2, y2;

Rope (float x1, float y1, float x2, float y2) {
x1 = anc1.x;
y1 = anc1.y;
x2 = mouseX;
y2 = mouseY;
}

void display () {
stroke (200);
line(x1, y1, 50, 100);
println(y1);
}
}


results:

...

On earlier builds, the rope wouldn't draw at all, which is why I inserted constants in the display function. Anyways, some pointers would be great.

(i hope php tags work)
Re: Class Confusion
Reply #5 - Sep 25th, 2008, 8:55pm
 
You shouldn't have to use noLoop() to stop drawing anchors. The reason it keeps adding them is because you're instantiating them in the draw() function.

Move all your
ancX = new Anchor(xpos, ypos);

statements to the setup() function.

As for the rope, it doesn't work because you have noLoop(). Without the loop, it will never get new mouse coordinates. and redraw the line to whereever the mouse is.
For the rope, you should move it's instantiation to setup() just like the anchors.  Then, in your draw() function you sill have rp.display(), but what happens here? it simply draws the line, it never updates the x2,y2 coords to mouseX/mouseY.  you can update those in the display function, or create an .update() function and run both
rp.update()
rp.display() in draw()

Hope that makes sense.

Oh, and maybe you want to add a
background(255);
to the top of your draw() function.
Re: Class Confusion
Reply #6 - Sep 26th, 2008, 4:08am
 
alecks wrote on Sep 25th, 2008, 8:55pm:
Hope that makes sense.

Oh, and maybe you want to add a
background(255);
to the top of your draw() function.


Dang, this has been REALLY helpful advice, overall, and I was able to get things rolling along a lot better. Of course, with progress comes new obstacles :/ (which I will ask about when I get the time/give up trying to figure it out myself)

Thanks for the super-awesome help!!
Re: Class Confusion
Reply #7 - Oct 5th, 2008, 11:53pm
 
alecks wrote on Sep 25th, 2008, 8:55pm:
 Then, in your draw() function you sill have rp.display(), but what happens here it simply draws the line, it never updates the x2,y2 coords to mouseX/mouseY.  you can update those in the display function, or create an .update() function and run both
rp.update()
rp.display() in draw()

Hope that makes sense.



Could you perhaps clarify the update function or how to make it I've got a build that "works" in the sense that it draws an anchor and draws a line from-mouse-to-anchor but it does so every millisecond. That is to say, every frame it places the anchor somewhere else. In a different build, it wouldn't redraw every frame, and the mouse-line worked as intended, but adding another anchor (anc2) simply draws them on top of eachother.

Any help would be greatly appreciated:

Code:

//Gravity Clone, Cassidy Liston 9-23-08

//Class Anchor has 6 children.

Anchor anc1;
Anchor anc2;
Anchor anc3;
Anchor anc4;
Anchor anc5;
Anchor anc6;

Rope rp; // A single line that draws from nearest anchor to mouse.


PImage anchor; //We have a sprite that will be implemented later.

float xpos;
float ypos; //I am unsure if I need these.

void setup()
{

size(240,480);


anc1 = new Anchor(xpos, ypos);
rp = new Rope(xpos, ypos, mouseX, mouseY);

anc2 = new Anchor(xpos, ypos);

anc3 = new Anchor(xpos, ypos);
anc4 = new Anchor(xpos, ypos);
anc5 = new Anchor(xpos, ypos);
anc6 = new Anchor(xpos, ypos);

}

void draw()
{
xpos = random(0,240);
ypos = random(20,450);
background (110, 110, 120);
stroke(25,30,0);
line (0, 470, 240, 470); //this is the "ground"

anc1.display ();
rp.display ();

// anc2.display (); //This anchor draws over the first one...
/* The rest is disabled until I can get two separate Anchors.
anc3.display ();

xpos = random(10,200);
ypos = random(20,440);

anc4.display ();

xpos = random(10,200);
ypos = random(20,440);

anc5.display ();

xpos = random(10,200);
ypos = random(20,440);

anc6.display ();

frameRate(10);
*/
}

class Anchor {

float x, y;

Anchor (float x, float y) {
x = xpos;
y = ypos;
}

void display () {
anchor = loadImage("anchorgrn.gif");
image (anchor, xpos, ypos);

}
}

class Rope {

float x1, y1, x2, y2;

Rope (float x1, float y1, float x2, float y2) {
x1 = xpos;
y1 = xpos;
x2 = mouseX;
y2 = mouseY;
}

void display () {
stroke (200);
line((xpos + 7), (ypos + 7), mouseX, mouseY);

}
}


...
Page Index Toggle Pages: 1