We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › "Bullet" creation in an array
Page Index Toggle Pages: 1
"Bullet" creation in an array (Read 664 times)
"Bullet" creation in an array
Nov 30th, 2009, 4:45pm
 
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.
Re: "Bullet" creation in an array
Reply #1 - Nov 30th, 2009, 11:35pm
 
you set the bubble x and y ONLY in the Bubble constructor which you do in setup(), just after initialising the Otter.

you need to create the Bubble (or just set the x and y on an existing Bubble) on mousePressed and pick up the Otter x and y then, not at the beginning.
Re: "Bullet" creation in an array
Reply #2 - Dec 1st, 2009, 2:13pm
 
You the man  Grin
Page Index Toggle Pages: 1