Arrays part 1
in
Programming Questions
•
10 months ago
I have this code almost finished, but I am stuck at one part. I don't understand how to make the balloon disappear when the mouse is over the balloon. Here is some information,
http://evc-cit.info/cit020/arrays.html
Here is my code;
Balloon [] balloons = new Balloon[30];
void setup()
{
float x;
float y;
float w;
float h;
color c;
size(400, 400);
frameRate(20);
for (int i = 0; i < balloons.length; i++)
{
w = random(20, 45);
h = w + random(5, 10); /* h = random(25, 55); // doesn't work */
x = random(w/2, width-w/2);/* so that balloon is on screen */
y = random(h/2, height-h/2);/* so that balloon is on screen */
c = color(random(255), random(255), random(200), random(128, 200));
balloons[i] = new Balloon(x, y, w, h, c);/* random color */
}
}
void draw()
{
background(255);
for (int i = 0; i < balloons.length; i++)
{
if (balloons[i].hit(mouseX, mouseY)) // the parameters will be the point we want to test to see if it's "inside"
}
else
{
balloons[i].display();
balloons[i].move(); //move the balloon at index number i*/
// if the mouse "hits" balloon at index number i
// then pop balloon at index number i
}
}
class Balloon
{
float x;
float y;
float w;
float h;
color c;
float xSpeed;
float ySpeed;
Balloon(float bx, float by, float bw, float bh, color bc)
{
x = bx;
y = by;
w = bw;
h = bh;
c = bc;
xSpeed = 3;
ySpeed = 7;
}
void display()
{
fill(c, 150);
ellipse(x, y, w, h);
}
boolean hit (int pX, int pY)
{
float a = w / 2.0;
float b = h / 2.0;
float ex = pX - x; // calculate relative to center of ellipse
float ey = pY - y;
float r = (ex * ex) / (a * a) + (ey * ey) / (b * b);
return (r < 1);
}
void pop()
{
x= -1000;
y= -1000;
xSpeed = 0;
ySpeed = 0;
}
void move()
{
x += xSpeed;
if (x < w/2 || x > width-w/2)
{
xSpeed = -xSpeed;
}
y += ySpeed;
if (y < h/2 || y > height-h/2)
{
ySpeed = -ySpeed;
}
}
}
Here is my code;
Balloon [] balloons = new Balloon[30];
void setup()
{
float x;
float y;
float w;
float h;
color c;
size(400, 400);
frameRate(20);
for (int i = 0; i < balloons.length; i++)
{
w = random(20, 45);
h = w + random(5, 10); /* h = random(25, 55); // doesn't work */
x = random(w/2, width-w/2);/* so that balloon is on screen */
y = random(h/2, height-h/2);/* so that balloon is on screen */
c = color(random(255), random(255), random(200), random(128, 200));
balloons[i] = new Balloon(x, y, w, h, c);/* random color */
}
}
void draw()
{
background(255);
for (int i = 0; i < balloons.length; i++)
{
if (balloons[i].hit(mouseX, mouseY)) // the parameters will be the point we want to test to see if it's "inside"
}
else
{
balloons[i].display();
balloons[i].move(); //move the balloon at index number i*/
// if the mouse "hits" balloon at index number i
// then pop balloon at index number i
}
}
class Balloon
{
float x;
float y;
float w;
float h;
color c;
float xSpeed;
float ySpeed;
Balloon(float bx, float by, float bw, float bh, color bc)
{
x = bx;
y = by;
w = bw;
h = bh;
c = bc;
xSpeed = 3;
ySpeed = 7;
}
void display()
{
fill(c, 150);
ellipse(x, y, w, h);
}
boolean hit (int pX, int pY)
{
float a = w / 2.0;
float b = h / 2.0;
float ex = pX - x; // calculate relative to center of ellipse
float ey = pY - y;
float r = (ex * ex) / (a * a) + (ey * ey) / (b * b);
return (r < 1);
}
void pop()
{
x= -1000;
y= -1000;
xSpeed = 0;
ySpeed = 0;
}
void move()
{
x += xSpeed;
if (x < w/2 || x > width-w/2)
{
xSpeed = -xSpeed;
}
y += ySpeed;
if (y < h/2 || y > height-h/2)
{
ySpeed = -ySpeed;
}
}
}
1