FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Referncing Objects (oop)
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Referncing Objects (oop)  (Read 426 times)
kevin

WWW
Referncing Objects (oop)
« on: Feb 20th, 2004, 5:41pm »

Hi all,
 
Just a quick question (hopefully) How do you reference parent/sister objects in p5.
 
e.g. If have an object myBall, that's generated, and an object myBox. I want to find the properties of myBox, so myBall can know to stay within it. (they're both defined in the main program)
 
In actionscript it'd be something like
Code:
myX = _parent.myBox.x;
myY = _parent.myBox.y;

What's the bext way to approach it in processing.
 
- Kevin
« Last Edit: Feb 20th, 2004, 5:47pm by kevin »  
kevinP

Email
Re: Referncing Objects (oop)
« Reply #1 on: Feb 20th, 2004, 8:30pm »

on Feb 20th, 2004, 5:41pm, p wrote:
Hi all,
 
Just a quick question (hopefully) How do you reference parent/sister objects in p5.
[..]
In actionscript it'd be something like
Code:
myX = _parent.myBox.x;
myY = _parent.myBox.y;

What's the bext way to approach it in processing.

 
Not sure about parent/sister, but to directly access the fields of an object you could write:
Code:
myX = myBox.x;
myY = myBox.y;

 
But of course this is frowned upon in strict OOP I believe. You should instead use "getters" and "setters" (aka accessor and mutator methods):
 
Code:

myX = myBox.getX();
 
//...
 
class Box
  int x;
  Box(int _x)
  {
   x = _x;
  }
 
  // getter
  int getX()
  {
    return x;
  }
}

 
But Processing doesn't seem to respect private fields anyway, so the first idea should always work.
 
-K
 

Kevin Pfeiffer
justo


Re: Referncing Objects (oop)
« Reply #2 on: Feb 20th, 2004, 10:30pm »

notice you can access some bImage's pixel array in the same fashion...without the get. its usually just a speed issue (you cut out the overhead of method calls...which is especially important if you are accessing the pixel array) or a conveniece issue...this code isnt going into an industrial strength product and if you are making it in processing it'll probably stay pretty small...so you can be sure to follow your own rules.
 
to the OP, the only way to access an object's parent is to pass it a reference...the only heirarchy is one that you make. so, for instance, when you make myBall:
 
Ball myBall = new Ball(this);
 
"this" is a reference to the current object (presumably myBox). you probably know this if you are familiar with actionscript.
 
then to store it, the Ball class might look like this:
 
class Ball {
    Box parent;
    int myX, myY;
    public Ball(Object parentObj) {
   parent = parentObj;
   myX = parent.x;
   myY = parent.y;
    }
}
 
take note though, if you keep parent as a Box, only Boxes can make Balls.
 
hope that helps.
 
Pages: 1 

« Previous topic | Next topic »