0
以下是我目前使用的代碼。我怎麼能這樣做,所以當我將鼠標與一個圓圈相撞時,它會被刪除?我相信我可以使用things.remove(0);但我不知道該把它放在哪裏。處理 - 如何在碰撞後移除陣列中的物體
任何幫助將是偉大的,謝謝!
ArrayList<Thing> things = new ArrayList();
Player person;
boolean testMode = true;
void setup() {
size(600, 600);
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
things.add(new Thing());
person = new Player();
}
void draw() {
background(255);
noStroke();
noCursor();
person.display();
for (Thing t : things) {
t.display();
if (collision(person, t) == true) {
text("OUCH!", person.x, person.y-30);
things.remove(0);
}
}
}
/////////////////////////////////////
boolean collision (Player p, Thing t) {
float d = dist(p.x, p.y, t.xPos, t.yPos);
if (p.radius + t.radius > d) {
// we have a collision
return true;
} else {
return false;
}
}
////////////////////////
class Thing {
//fields
float xPos;
float yPos;
float radius = 30;
//constructor
Thing() {
xPos = random(500);
yPos = random(500);
}
//methods
void display() {
float objDist = dist(xPos, yPos, mouseX, mouseY);
if (objDist < radius) {
fill(0, 0, 255, 128);
} else {
fill(0, 255, 0, 70);
}
ellipse(xPos, yPos, radius, radius);
}
}
//////////////////////////////////
class Player {
float x;
float y;
float radius = 30;
void display() {
fill(255,0,0,70);
ellipse(mouseX,mouseY,radius, radius);
}
}