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 › question about oop
Page Index Toggle Pages: 1
question about oop (Read 425 times)
question about oop
Mar 30th, 2006, 9:29am
 
hello list, im trying to understand oop in processing... and im having some problems....
i trying to create an animated dot using oop, i want that everytime i create an object an animated dot appears in my sketch with a random position , but it doesnt work, my dot always appear in the same position, can anybody explain me why is this happening and how can i fix this? many thanx in advance... here is the code:

punto objeto;

void setup()
{
 size(200, 200);
 stroke(255);
 framerate(30);

objeto=new punto(200,200);
}
void draw()
{
background(51);
objeto.display();
}
class punto
 {
   int x;
   int y;
punto( int xa, int ya){
float xf=random(xa);
float yf=random(ya);
int x= int(xf);
int y= int(yf);
print(x);

   }
 
 void display(){
    x = x - 1;
 if (x < 0) {
   x = height;
 }
   point(x,y);
   }  

}
Re: question about oop
Reply #1 - Mar 30th, 2006, 11:19am
 
The problem is you're re-declaring x and y inside your class constructor:

Code:

class punto
{
int x; <<-- initial declaration
int y;
punto( int xa, int ya){
float xf=random(xa);
float yf=random(ya);
int x= int(xf); <<-- This actually creates a different 'x'
int y= int(yf); <<-- remove the first int...
print(x);
}


So you should just need to remove the first int on the lines where you assign x and y values.
Page Index Toggle Pages: 1