2015-11-23 66 views
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); 
} 
} 

回答

1

你的代碼是幾乎沒有,但有似乎被下滑的幾個陷阱:

  1. 您繪製在鼠標座標(ellipse(mouseX, mouseY, radius, radius);)的球員,但你不要更新在碰撞檢測中使用的玩家的x,y座標。這樣可以使你的距離計算常數,因此碰撞代碼返回false
  2. 如果碰撞返回true,則現有的代碼被移除第一元素(things.remove(0);),但它應該刪除當前東西碰撞
  3. 的碰撞測試是使用兩個物體半徑的總和,但物體使用半徑作爲直徑繪製:或者檢查半徑的一半,或者使用半徑繪製橢圓* 2

這是什麼我的意思是代碼:

ArrayList<Thing> things = new ArrayList(); 
Player person; 

boolean testMode = true; 

void setup() { 
    size(600, 600); 
    //for loops can help you avoid repeating yourself in code 
    for(int i = 0 ; i < 8; i++){ 
    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.x = mouseX; 
    person.y = mouseY; 
    person.display(); 

    for (int i = 0 ; i < things.size(); i++) {//regular for loop to avoid ConcurrentModificationException (which is through usin using for :) 
    Thing t = things.get(i);  
    t.display(); 

    if (collision(person, t) == true) { 
     //text("OUCH!", person.x, person.y-30); //this will probably appear for a slit second 
     println("OUCH!"); 
     //things.remove(0);//don't remove the first, remove the thing that collided 
     things.remove(t); 
    } 
    } 
} 

///////////////////////////////////// 

boolean collision (Player p, Thing t) { 
    float d = dist(p.x, p.y, t.xPos, t.yPos); 
    if ((p.radius + t.radius) * .5 > d) {//.5 is the same as /2 -> although you named the properties of player and thing radius, you are drawing the diameter (twice the size) 
    // 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);//you're drawing the player at it's updated positions but the x,y properties used in collision detection aren't updated 
    ellipse(x, y, radius, radius); 
    } 
}