Creating new objects on click/ using get()
in
Programming Questions
•
2 years ago
Hello,
I'm trying to adapt this example on ArrayLists to instead display a series of expanding and contracting circles. My code won't run and is yelling at me about using balloons.get and I'm just completely stumped. Any advise would be immensely appreciated. Here is my complete code:
I'm trying to adapt this example on ArrayLists to instead display a series of expanding and contracting circles. My code won't run and is yelling at me about using balloons.get and I'm just completely stumped. Any advise would be immensely appreciated. Here is my complete code:
- /*Every time the mouse is clicked a circle expands from that spot
until the mouse is unclicked. At this point the circle contracts
and new circles may be created */
ArrayList balloons;
void setup(){
size(640, 480);
fill(100, 55, 25);
stroke(255);
strokeWeight(8);
//create an empty ArrayList
balloons = new ArrayList();
//start by adding one element
balloons.add(new Balloon(width/2, height/2));
}
void draw(){
background(0);
for (int i=balloons.size()-1; i >=0; i--){
Balloon balloons = (Balloon) balloons.get(i);
balloons.grow();
balloons.display();
if((balloons.growing == false) && (balloons.diameter < 1)){
balloons.remove(i);
}
}
}
void mousePressed(){
//a new balloon object is added to the ArrayList (by default to the end)
balloons.add(new Balloon(mouseX, mouseY));
}
class Balloon {
boolean growing; //used to check if balloon is growing. May not be neccessary
float diameter; //diameter of balloon
int xPos, yPos; //x and y position of balloon
float rate; //growth/decay rate
Balloon(int tempX, int tempY){ //constructor. Same as setup()
growing = true;
diameter = 5;
xPos = tempX;
yPos = tempY;
rate = 1.1;
}
void display(){
ellipse(xPos, yPos, diameter, diameter);
}
void grow(){
if((mousePressed)&&(growing == true)){
diameter = diamater * rate;
}else{
growing = false;
diameter = diameter/rate;
}
}
}
1