I'm having trouble getting a "bullet/projectile" to be created at a dynamic location. In the below code, the projectiles are the bubbles of the Bubble class and is created using an array. The object should be created at the x and y coordinates of the otter (in the Otter class), but instead they are generated at what appears to be (0,0). Any suggestions?
Code:Bubble[] bubbles = new Bubble[100];
Otter otter;
int bubCount = 0;
void setup() {
size(500,500);
background(255);
smooth();
noCursor();
otter = new Otter(color(0), 20, 50, 5, 3);
for (int i = 0; i < bubbles.length; i++) {
bubbles[i] = new Bubble();
}
}
void draw() {
background(255);
println(otter.x + " " + otter.y);
otter.display();
otter.setLocation(mouseX,mouseY);
for (int i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].display();
if (i == bubbles.length) {
i = 0;
}
}
}
void mouseClicked() {
bubbles[bubCount].alive = true;
if (bubCount >= bubbles.length - 1) {
bubCount = 0;
}
bubCount++;
}
class Bubble {
color c;
int r;
int speedX;
int x;
int y;
boolean alive;
Bubble() {
r = 5;
x = otter.x;
y = otter.y;
c = color(107,184,234);
speedX = 5;
alive = false;
}
void display() {
if (alive) {
stroke(0);
fill(c);
ellipse(x,y,r,r);
}
}
void move() {
if (alive) {
x = x + speedX;
}
}
}
class Otter {
color furColor;
int h;
int w;
int x;
int y;
int speedX;
int speedY;
Otter(color tempColor, int tempH, int tempW, int tempSpeedX, int tempSpeedY) {
furColor = tempColor;
h = tempH;
w = tempW;
speedX = tempSpeedX;
speedY = tempSpeedY;
}
void display() {
noStroke();
fill(furColor);
rectMode(CENTER);
rect(x,y,w,h);
}
void setLocation(int tempX, int tempY) {
x = tempX;
y = tempY;
}
}
The println function shows that the otter.x and otter.y variables are being updated, but the coordinates of the bubbles don't seem to change.