Removing objects from an Array List
in
Programming Questions
•
11 months ago
Code below.
Simple question. How does one remove an object from an Array List so that it isn't in the game anymore? This would mean it only affects that individual object of the list, and doesn't just become invisible so it no longer affects the game.
Simple question. How does one remove an object from an Array List so that it isn't in the game anymore? This would mean it only affects that individual object of the list, and doesn't just become invisible so it no longer affects the game.
- class Missile {
float endX; // End X position
float endY; // End Y position
float endvarX; // Line gradient X
float endvarY; // Line gradient Y
float startX; // Start of missile line
float startY; // start of missile line
color c;
Missile(float init_endvarX, float init_endvarY, float init_startX, float init_startY,
color init_c)
{
endX = init_startX;
endY = init_startY;
endvarX = init_endvarX;
endvarY = init_endvarY;
startX = init_startX;
startY = init_startY;
c = init_c;
}
void render() {
{
smooth();
stroke(c);
line(startX, startY, endX, endY);
}
}
void update() {
endY = endY + endvarY;
endX = endX + endvarX;
if (dist(endX, endY, posX, posY) <= 15){
background(255,0,0);
} else{
}
}
} // class Missile -
int framestore; // frame last missile was created on
int Mdelay; //delay between next fire time
int posX; //mouse X position of player
int posY; //mouse Y position of player
float fireX; //starting X co-ordinate of player missile
int fireY; //starting Y co-ordinate of player missile
Missile randomMissile() {
return new Missile(random(-2.0, 2.0), random(0.5, 2.5),
random (200, 600), 0,
color(random(0, 256),
random(0, 256),
random(0, 256))
);
}
ArrayList<Missile> missilelist; // list in null initially
void setup() {
size(700, 500);
framestore = 0;
Mdelay = (int)random(60, 150);
missilelist = new ArrayList<Missile>(); // creates and initially empty ArrayList of missile objects
frameRate(30);
}
void draw() {
background(255);
if (Mdelay < (frameCount - framestore) ) {
for (int i = 0; i < (int)random(3); i = i+1) { // gives the initial value of 0
// then randomized between 3 maximum values. This means there can only be between 1 and
// 3 missiles active
// then adds +1 to the value of i, but cannot be more than 3.
// code courtesy of Jacques
missilelist.add(randomMissile());
}
framestore = frameCount;
}
// render all the missiles.
for (Missile b : missilelist) {
b.update();
b.render();
}
fill(255, 80, 80);
triangle(350, 480, 335, 500, 365, 500);
}
void mousePressed() {
fireX = 350;
fireY = 480;
posX = mouseX;
posY = mouseY;
smooth();
strokeWeight(2.5);
stroke(255,0,0);
line(fireX, fireY, posX, posY);
}
1