We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to increase the objects in a sketch through duplication by increasing the values in an array, but when I try to use expand(), the sketch doesn't run (NullPointerException). When I take expand() out, it runs fine.
Here is the code:
Pulse[] beat = new Pulse[1];
void setup() {
size(500,500,P3D);
//bubbles
for (int i=0; i < beat.length; i++) {
beat[i] = new Pulse (random(width), random(height), 50, random(50));
}
}
void draw() {
background(255);
noCursor();
for (int i=0; i < beat.length; i++) {
beat[i].multiply();
beat[i].quiver();
beat[i].display();
}
}
class Pulse {
float x;
float y;
float r;
float alp;
float dx = 5;
float dy = 5;
int heart = 0;
//Constructor
Pulse(float tempX, float tempY, float tempR, float tempAlp) {
x = tempX;
y = tempY;
r = tempR;
alp = tempAlp;
}
void display() {
color c = color(138, 3, 3, alp);
stroke(0, 25);
fill(c);
ellipse(x, y, r*2, r*2);
}
void quiver() {
float tx, ty;
//Keeps x coordinate within boundaries
tx = x+round(random(-dx, dx));
while (tx<0 || tx>width)
tx=x+round(random(-dx, dx));
//Keeps y coordinate within boundaries
ty=y+round(random(-dy, dy));
while (ty<0 || ty>height)
ty=y+round(random(-dy, dy));
x = tx;
y = ty;
}
void multiply() {
beat = (Pulse[]) expand(beat);
}
}
Answers
The problem is line 65.
What is the function expand()?
You are accessing
beat
there... but beat contains objects of this class. This is illegal to put it in a softer note unless you explain your intentions here.If you want to increase the size, try just adding:
r=r*1.01
orr=r+1;
these are different ways to get your effect.Kf
To clarify — I don't want to increase the size of the circle, but the number of circles in the sketch. That is, start with one, and then have the numbers double.
Replace line 65 with r=r+1...
Kf
https://forum.Processing.org/two/discussion/21508/i-m-trying-to-create-multiple-projectiles-as-if-a-spaceship-is-shooting-an-enemy
Why not just write the code to add new circles? Why rely on "expand"? It doesn't fill the newly created spots with anything.