We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to figure out how to set a PVector as an arguement for my Spawnball class, but "Spawnball(int d, PVector loc)" gives an error. Also I'd like to add a spawn function that draws multiple ellipses on top of the original ball. I'd like to do this in a way that calls the class within itself, but I have no idea how to structure the code for that. Thanks!
Spawnball sb = new Spawnball(50);
Spawnball sb2 = new Spawnball(30);
void setup() {
size(850, 650);
noCursor();
}
void draw() {
background(0);
sb.update();
sb.display();
noFill();
stroke(200);
ellipse(mouseX, mouseY, 8, 8);
}
class Spawnball {
int dia;
PVector loc;
PVector vel;
PVector acc;
Spawnball(int d) {
dia = d;
loc = new PVector(0, 0);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
}
void update() {
PVector pos = new PVector(mouseX, mouseY);
pos.sub(loc);
pos.setMag(.1);
acc = pos;
vel.add(acc);
loc.add(vel);
acc.mult(0);
vel.limit(3);
}
void display() {
fill(10, 175, 3*dia);
noStroke();
ellipse(loc.x, loc.y, dia, dia);
}
}
Answers
Can you post the code that gives an error?
This may help you:
both of the examples that you shared create recursions through functions, I'd like to call an instance of my class within the class itself. Is that not possible?
If I create the ball as a function, it doesn't animate. Is there a way I can fix that?
void setup() { size(850, 650); noCursor(); }
I don't know, but I've tried to create a Spawnball object inside of the Spawnball class and the skecth didn't run so I don't think so.
But what exactly do you want to do? Isn't it possible to do with only the method that displays the ball being recursive? Or a recursive function outside of the class used to create Spawnball objects? Or maybe creating another class that contains some Spawnball objects, like a particle system?
Ok so I tried creating a separate class "subSpawn" calls the spawnball class but when I run the sketch the balls don't move. I also tried just generating a bunch of balls within the spawnball class with a for loop, but only one of the objects reacts properly to the mouse
second class: int diameter;
for loop:
The balls don't move because each time you call
sbS1.spawn()
you're recreating them at their initial position.There is no such thing as a 'recursive' class, but it is possible for class to have fields/attributes of the same type, this is commonly used to create tree data structures. This is not necessary in this case.
I have created an example sketch based on your code which I believe does what you want. Adapt it in any way you wish.