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 › one class targeting another
Page Index Toggle Pages: 1
one class targeting another (Read 176 times)
one class targeting another
Oct 26th, 2008, 2:11am
 
Is there a way I can write an object class that interacts with another object (of a different class) without having to explicitly write the name of the object it is interacting with?

I wrote an example to show the blunt, explicit way I currently know how to make objects interact:

Code:


targetObj[] myTargets = new targetObj[2];
linkObj myLinker = new linkObj();

void setup(){
myTargets[0] = new targetObj(25,10);
myTargets[1] = new targetObj(75,90);
}

void draw(){
myLinker.update();
myTargets[0].display();
myTargets[1].display();
}

class targetObj{
float x,y;
targetObj(float tx, float ty){
x = tx;
y = ty;
}
void display(){
ellipse(x,y,2,2);
}
}

class linkObj{
linkObj(){
}
void update(){
myTargets[0].x += 1;
myTargets[0].y += 1;
myTargets[1].x -= 1;
myTargets[1].y -= 1;
}
}


I want to have a way to make the linkObj class be able to change the .x and .y variables of the target class without having to explicitly write the names of the target objects (myTargets[0].x += 1;)

Is there some way I can do this? Have I explained it clearly?
Re: one class targeting another
Reply #1 - Oct 28th, 2008, 8:44pm
 
Here is a possible way:
Code:
TargetObj[] myTargets;
LinkObj myLinker;

void setup() {
 myTargets = new TargetObj[2];
 myTargets[0] = new TargetObj(25, 10);
 myTargets[1] = new TargetObj(75, 90);
 int[] dx = { 1, -1 }, dy = { 1, -1 };
 myLinker = new LinkObj(myTargets, dx, dy);
}

void draw() {
 myLinker.update();
 for (int i = 0; i < myTargets.length; i++)
 {
   myTargets[i].display();
 }
}

class TargetObj {
 float x,y;
 TargetObj(float tx, float ty) {
   x = tx;
   y = ty;
 }
 void display() {
   ellipse(x, y, 2, 2);
 }
}

class LinkObj {
 TargetObj[] linkedTargets;
 int[] dirX, dirY;
 
 LinkObj(TargetObj[] lt, int[] dx, int[] dy) {
    linkedTargets = lt;
    dirX = dx; dirY = dy;
 }
 
 void update() {
   for (int i = 0; i < linkedTargets.length; i++)
   {
     linkedTargets[i].x += dirX[i];
     linkedTargets[i].y += dirY[i];
   }
 }
}


Here is a slightly more convoluted variant:
Code:
TargetObj[] myTargets;
LinkObj myLinker;

void setup() {
myTargets = new TargetObj[2];
myTargets[0] = new TargetObj(25, 10, 1, 1);
myTargets[1] = new TargetObj(75, 90, -1, -1);
myLinker = new LinkObj();
for (int i = 0; i < myTargets.length; i++)
{
myLinker.addTarget(myTargets[i]);
}
}

void draw() {
myLinker.update();
}

class TargetObj {
float x, y;
float dx, dy;

TargetObj(float tx, float ty, float mx, float my) {
x = tx; y = ty;
dx = mx; dy = my;
}

void display() {
ellipse(x, y, 2, 2);
}

void update() {
x += dx;
y += dy;
}
}

class LinkObj {
ArrayList linkedTargets;

LinkObj() {
linkedTargets = new ArrayList();
}

void addTarget(TargetObj linkedTarget) {
linkedTargets.add(linkedTarget);
}

void update() {
for (int i = 0; i < linkedTargets.size(); i++)
{
TargetObj to = (TargetObj) linkedTargets.get(i);
to.update();
to.display();
}
}
}

I found logical to put the direction of move in the target object, you can add a method to update the direction too. But if you prefer the linker to control directions, you can add another array list, etc. Likewise, you can separate target move and display, etc.
Page Index Toggle Pages: 1