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 › Accessing nested objects
Page Index Toggle Pages: 1
Accessing nested objects (Read 380 times)
Accessing nested objects
Jun 2nd, 2009, 11:15am
 
I've tried the Composite references and I might be missing it, but I'm can't find how to address an object that was created inside another object.

I have a class called bSprites that I've designed to work almost exactly like movieclips in actionscript (it's what I'm comfortable with).

So playing around I've created a class called 'ball'. Originally I extended bSprite with ball but it makes some of my other code less elegant (all sprites are added to an array, a for loop walks through the array and updates/draws/etc all the sprites. Having ball extend sprite means I had to add another array and for loop...)

Now I'm:

Code:
class ball{
bSprite base;
bSprite shade;
bSprite shadow;
float _x;
float _y;
float _xspeed;
float _yspeed;
ball (float tempXpos, float tempYpos, float tempXspeed, float tempYspeed){
_x = tempXpos;
_y = tempYpos;
_xspeed = tempXspeed;
_yspeed = tempYspeed;
shade = new bSprite("dbc.png", _x,_y,_xspeed,_yspeed);
base = new bSprite("dbb.png", _x,_y,_xspeed,_yspeed);
shadow = new bSprite("dba.png", _x+10,_y+10,_xspeed,_yspeed);
}
}


The problem is I don't know how to change the fields of the sprites I created in the ball (shade, base and shadow). Coming from actionscript I wanna do something like ball[shade]._x++, but obviously that doesn't fly.

Ultimately, I want all objects to be built of sprites and all sprites to be handled and controlled through a single set of statements (so I don't have to have an array for n types of sprites, since I'll also have arrays for object types).

Any advice?
Re: Accessing nested objects
Reply #1 - Jun 2nd, 2009, 11:57am
 
I think I got it by adding an update() method to the ball class.

I'd like to hear if there are still any more elegant solutions than this one.
Re: Accessing nested objects
Reply #2 - Jun 2nd, 2009, 12:38pm
 
There are several ways to do this.
One way is doing an update() method... as you did.
Another way is to make several methods (one per field), using so called setters:
void setShadePos(float x, float y)
etc.
A third way is to use direct access to fields. While it is discouraged in classical Java, it might be OK in Processing for simple sketches.
The syntax is:

balls[i].shade._x++

where balls is an array of ball objects and i an index.
Re: Accessing nested objects
Reply #3 - Jun 2nd, 2009, 4:10pm
 
PhiLho  wrote on Jun 2nd, 2009, 12:38pm:
There are several ways to do this.
One way is doing an update() method... as you did.
Another way is to make several methods (one per field), using so called setters:
void setShadePos(float x, float y)
etc.
A third way is to use direct access to fields. While it is discouraged in classical Java, it might be OK in Processing for simple sketches.
The syntax is:

balls[i].shade._x++

where balls is an array of ball objects and i an index.



Thank you very much:

Code:

balls[i].shade._x++


is most similar to what I'm used to. It's good to know what is commonly accepted as best practices... I'll stick with the update method for now, but keep the direct manipulation in the back of my mind for simple uses and just-in-cases.
Page Index Toggle Pages: 1