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 › Reference an objects variable
Pages: 1 2 
Reference an objects variable (Read 1434 times)
Re: Reference an objects variable
Reply #15 - Mar 2nd, 2010, 9:29am
 
This would be cool,

Anyway this a bit off topic but heres my code i have written so far

the buttons havent been sorted yet so just ignore that

it seems to be measuring where collisions take place incorrectly,

if you run the code it draws a black circle where it thinks a collision occurs

Quote:
//Librarys 

import guicomponents.*;

//guicomponents library, declaring buttons and sliders

GButton btnclear, btnreset, btnstroke, btnfill, btntrails;
GVertSlider alphaslider, diameterslider, numslider;

//Declare arraylist

ArrayList balls;

//declare integers

int numcircles = 10; //number of circles created
//integer for the array index


void setup() {

  balls = new ArrayList(); //Create and empty array list

    background(255);
    size(1000,600);

  for ( int i = 0; i < numcircles; i++ )
  {    
    Circle ball = new Circle(800/2,height/2,random(-5,5),random(-5,5));//Create ball objects
    balls.add(ball); //add ball to the array list
  }

  smooth();


  //Button shit

  GComponent.globalColor = GCScheme.getColor(this,  GCScheme.BLUE_SCHEME);
  GComponent.globalFont = GFont.getFont(this, "Arial", 12);

  btnclear = new GButton(this,"Clear",825,25,70,10);
  btnreset = new GButton(this,"Reset",825,50,70,10);
  btnstroke = new GButton(this,"Stroke",825,490,70,10);
  btnfill = new GButton(this,"Fill",825,515,70,10);
  btntrails = new GButton(this,"Trails",825,540,70,10);

  //Slider shit

  alphaslider = new GVertSlider(this,825,75,20,400);
  alphaslider.setLimits(10,0,200);

  diameterslider = new GVertSlider(this,850,75,20,400);
  diameterslider.setLimits(10,0,300);

  numslider = new GVertSlider(this,875,75,20,400);
  numslider.setLimits(10,0,50);

}

void draw(){

  background(255);
  for (int i = balls.size()-1; i >=0 ;i--)
  {
    ball.moveCircle();
    ball.drawCircle();    
  }
  checkCollision();

}

void checkCollision(){

  for (int i = balls.size()-1; i >=0 ;i--){

    float CollisionCheckX = (((Circle)balls.get(i)).x);
    float CollisionCheckY = (((Circle)balls.get(i)).y);
    float BallDiameter = (((Circle)balls.get(i)).diameter);
    

    for (int j = balls.size()-1;  j >=0 ;j--) {

      float CollisionCheckX2 = (((Circle)balls.get(j)).x);
      float CollisionCheckY2 = (((Circle)balls.get(j)).y);
      float BallDiameter2 = (((Circle)balls.get(j)).diameter);
      

      if (i != j){
        
      

        if (dist(CollisionCheckX,CollisionCheckX2,CollisionCheckY,CollisionCheckY2) <= (BallDiameter/2) + (BallDiameter/2)){

          println("Ball "+i+" X "+CollisionCheckX+", Y "+CollisionCheckY+", Diameter "+BallDiameter+" || Ball "+j+" X "+CollisionCheckX2+", Y "+CollisionCheckY2+" Diameter "+BallDiameter2);
          fill(0,0,0);
          ellipse((CollisionCheckX+CollisionCheckX2)/2,(CollisionCheckY+CollisionCheckY2)/2,10,10
);

        }

      }


    }
  }
}



void handleButtonEvents(GButton button) {
  if (btnreset == button && button.eventType == GButton.CLICKED) {
    setup();
  }
}












Re: Reference an objects variable
Reply #16 - Mar 2nd, 2010, 9:34am
 
[quote author=3836333A2C2837362B3E343A2D5F0 link=1267462314/9#9 date=1267464862]
Circle getball(int i)
{
 return (Circle)balls.get(i);
}[/quote

Also this doesnt seem to be working for me

Code:
void draw(){

background(255);
for (int i = balls.size()-1; i >=0 ;i--)
{
getBall(i);
ball.moveCircle();
ball.drawCircle();
}
checkCollision();

}

Circle getBall(int _number);
{
return (Circle)balls.get(_number);
}



Re: Reference an objects variable
Reply #17 - Mar 2nd, 2010, 9:42am
 
you have to assign the results of getBall() to something (of type Ball)

ball = getBall(i);

Re: Reference an objects variable
Reply #18 - Mar 2nd, 2010, 9:47am
 
Quote:
void draw(){

  background(255);
  for (int i = balls.size()-1; i >=0 ;i--)
  {
    ball = getBall(i);
    ball.moveCircle();
    ball.drawCircle();    
  }
  checkCollision();

}



"Could not find anything named "ball" "
Re: Reference an objects variable
Reply #19 - Mar 2nd, 2010, 11:47am
 
Don't forget to declare your variables :

Code:
Ball ball = getBall(i); 



or

Code:
Ball ball;
...
ball = getBall(i);
Re: Reference an objects variable
Reply #20 - Mar 2nd, 2010, 2:48pm
 
...or you can just not create a new variable at all and do:

getBall(i).moveCircle();
getBall(i).drawCircle();  

Pages: 1 2