Hi,
For educational purpose I want to write a cleanly structured Pong game in processing.
For the Ball I’d liked to use PShape inside a class and still have access to all the PShape methods such as setVisibility() etc. Why do I want a wrapper? Because I’d like the Ball to behave like all the other objects in my game, with *.update() and *.display() methods etc.
I looked a the solution from GoToLoop. This is very smart, however, I’d like to stay in processing and rather don’t extend with *.java. Is there another pattern I can use?
Just a note for others in the future – the thread is called “Extend PShape”, but the solution(s) were to encapsulate PShape (put it inside a class) rather than extend it (https://processing.org/reference/extends.html). Encapsulating is usually the better way to go.
I encountered the same problem and could not immediately find a solution. I had to look into it, and this is what I came up with.
You can easily extend a PShape, but you shall provide a PGraphics instance of your PApplet to constructor and then pass it to the superclass. So you have to call PShape(PGraphics g, int family) superclass constructor. A quick example:
Shape myshape;
void setup(){
size(400, 400);
// here is the fun: 'this' is actual PApplet,
// and 'g' is a field with PGraphics instance
myshape = new Shape(this.g);
myshape.beginShape();
myshape.vertex(0, 0);
myshape.vertex(100,0);
myshape.vertex(100,100);
myshape.vertex(0, 100);
myshape.fill(200, 20, 20);
myshape.endShape(CLOSE);
}
void draw(){
shape(myshape, 10,10);
}
class Shape extends PShape{
Shape(PGraphics g){
super(g, GEOMETRY);
}
// and here comes all your weird stuff you want to add
}
UPDATE: This will work for the standard renderer. But if you want to use something else, you’ll have to create your Shape objects in a completely different way.