Problem with array
in
Programming Questions
•
2 years ago
In the chapter on classes in the processing handbook, there is a section on a "spot" class, which is implemented in a sketch to create an array of 6 spots with different speeds or rates. I tried "improving" this sketch by adding an array with different values of rate, but for some reason when I run the sketch, all the spots have the same speed, the last value in my "rate" array. Here is what I came up with (what I added is in the "rates" array plus the extra for-loop dealing with the rates array within void setup):
int numSpots = 6;
// Declare and create the array
Spot[] spots = new Spot[numSpots];
float[] rates = {0.5,0.6,0.3,0.4,0.5,.7};
void setup() {
size(100, 100);
smooth();
noStroke();
for (int i = 0; i < spots.length; i++) {
for (int j = 0; j < rates.length; j++) {
float x = 10 + i*16;
float rate = rates[j];
// Create each object
spots[i] = new Spot(x, 50, 16, rate);
}
}
}
void draw() {
fill(0, 15);
rect(0, 0, width, height);
fill(255);
for (int i = 0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
}
// Insert Spot class
class Spot {
float x, y; // X-coordinate, y-coordinate
float diameter; // Diameter of the circle
float speed; // Distance moved each frame
int direction = 1; // Direction of motion (1 is down, -1 is up)
// Constructor
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter/2)) || (y < diameter/2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
Can someone tell me why all the spots have the same speed, specifically the value of the last element in the rates array, and not different speeds?
int numSpots = 6;
// Declare and create the array
Spot[] spots = new Spot[numSpots];
float[] rates = {0.5,0.6,0.3,0.4,0.5,.7};
void setup() {
size(100, 100);
smooth();
noStroke();
for (int i = 0; i < spots.length; i++) {
for (int j = 0; j < rates.length; j++) {
float x = 10 + i*16;
float rate = rates[j];
// Create each object
spots[i] = new Spot(x, 50, 16, rate);
}
}
}
void draw() {
fill(0, 15);
rect(0, 0, width, height);
fill(255);
for (int i = 0; i < spots.length; i++) {
spots[i].move(); // Move each object
spots[i].display(); // Display each object
}
}
// Insert Spot class
class Spot {
float x, y; // X-coordinate, y-coordinate
float diameter; // Diameter of the circle
float speed; // Distance moved each frame
int direction = 1; // Direction of motion (1 is down, -1 is up)
// Constructor
Spot(float xpos, float ypos, float dia, float sp) {
x = xpos;
y = ypos;
diameter = dia;
speed = sp;
}
void move() {
y += (speed * direction);
if ((y > (height - diameter/2)) || (y < diameter/2)) {
direction *= -1;
}
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
Can someone tell me why all the spots have the same speed, specifically the value of the last element in the rates array, and not different speeds?
1