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 › How to access an object variable from outside
Page Index Toggle Pages: 1
How to access an object variable from outside (Read 460 times)
How to access an object variable from outside
Oct 29th, 2006, 1:37am
 
Hi,

I am still a newbie to the 'this.xx' method. I have constructed an array of objects that looks like this:

MyObject [] myObject = new MyObject[9];

I can address each object from the outside by calling up the specific member of the array like this, so I got the connection working:

if(keyPressed) {
   if (key == 'q' || key == 'Q') {
     myObject[8].r = 100;
   }

However, I cannot just contact _any_ of the members in the array - i.e. I would like to do something like this:

if(keyPressed) {
   if (key == 'q' || key == 'Q') {
     myObject.this.r = 100;
   }

I know that the above is not the right way to do it, but I would like to know how to address any of the members in the array of objects - i.e. if a square collides with any of the objects in the array, they should be red.

thanks for looking at this!
izmir
Re: How to access an object variable from outside
Reply #1 - Oct 29th, 2006, 1:04am
 
You can only ever access an element of the array via its index.  You can do this individually (as you did):

Code:

myObjects[8].r = 100;


Or you can access all (or some) of the elements of an array with a loop.

Code:

for (int i = 0; i < myObjects.length; i++) {
myObjects[i].r = 100;
}


If you want to test all of the elements of the array against some condition, you can do this with an if statement inside a loop, i.e.:

Code:

for (int i = 0; i < myObjects.length; i++) {
if (myObject[i].r > 50) {
myObjects[i].callSomeMethod();
}
}

Page Index Toggle Pages: 1