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 › call parent function
Page Index Toggle Pages: 1
call parent function? (Read 892 times)
call parent function?
Jan 6th, 2010, 3:06pm
 
Can we call a parent function like this? this doesn't work it said "function cool no exist"...somebody else?

/////////////////////////////////////////////////////MAIN
myClass ismyClass;

void setup(){
 size(300,300);
 ismyClass = new myClass(this);
}

void draw(){
 this.rect(20,20,20,20);
 ismyClass.display();
}

public void cool(){
 rect(150,40,34,344);
}


/////////////////////////////////////////////////////CLASS MYCLASS

public class myClass {
 PApplet parent; // The parent PApplet that we will render ourselves onto

 myClass(PApplet p) {
   this.parent = p;
 }

 // Draw stripe
 void display() {
   parent.background(10);
   parent.cool();
 }
}

///////////////////////////THANK SO MUNCH
and happy new year and best wishes for all!
Re: call parent function?
Reply #1 - Jan 6th, 2010, 3:36pm
 
Someone else can explain the complexities of why it doesn't work (I suspect it may have something to do with how or when top-level custom methods are registered with the PApplet class); but why not call it directly without the reference to 'parent'?

Code:
void display() {
  parent.background(10);
  cool();
}


I suppose you could run into scope issues in this case, but it does work at least...  Alternatively you could perhaps create a separate class to hold methods that need to be accessed from multiple other classes.

From the Libraries documentation it looks like passing a reference to PApplet to a class is at the least intended to access built-in methods...
Re: call parent function?
Reply #2 - Jan 7th, 2010, 1:24am
 
parent is defined as a PApplet, so the compiler knows only the methods in PApplet, coming with Processing.
cool() is part of your program, which is actually inside a class created by Processing, of the name of your sketch, extending PApplet.
Something like:
Code:
class MyFabulousSketch extends PApplet {
void setup() {} // Override PApplet's setup
void cool() {} // Add a new method not in PApplet
}

Since myClass is actually a nested class inside MyFabulousSketch, as blindfish wrote, it can access all the methods of the parent without problem.
If you really want to use parent, you can do it in a convoluted way (untested):
((MyFabulousSketch) parent).cool()
ie. you tell the compiler that parent is more than a PApplet.

Libraries are another story: their classes are not nested in a PApplet, so they don't have a direct access to its methods, hence the need to pass a PApplet instance to them, eg. to draw on the surface of the sketch.
That's what you would get by putting myClass in a .java tab.

In short, for classes in .pde files (main or secondary), you don't have to pass around the 'this', it is always available.
Page Index Toggle Pages: 1