问题
These are my 2 enemies
en = new Enemy(700, 150);
en2 = new Enemy (980, 150);
I want to have my program contain several enemies, would i just have to recreate them and with unit collision
if (d.intersects(r1) && en.visible == true &&
en.isAlive == false && !p.hitting){
hitmang(hit);
p.hitting = true;
}
if (d.intersects(r2) && en.visible == true &&
en.isAlive == false && !p.hitting){
hitmang(hit);
p.hitting = true;
}
if (!d.intersects(r1) && !d.intersects(r2)){
p.hitting = false;
}
Do I have to recreate every instance I with another enemy?
These are my enemies plus their boundaries in the game
Rectangle r1 = en.getBounds();
Rectangle r2 = en2.getBounds();
(I have them as rectangles)
public Rectangle getBounds(){
return new Rectangle(x, y, 114, 134);
}
回答1:
I dont exactly understand what you are trying to do?
Do you want to add your enemies to an array and then cycle through them to check if the enemies bounding box intersects with the players?
public List<Rectangle> enBoundingBoxes = new ArrayList<Rectangle>();
then add the enemy bounding boxes to the arraylist.
enBoundingBoxes.add(en.getBounds());
enBoundingBoxes.add(en2.getBounds());
cycle through them using a for loop :)
for (int i = 0; i < enBoundingBoxes.size(); i++) {
Rectangle tempBBox = enBoundingBoxes.get(i);
if (d.intersects(tempBBox) && en.visible == true && !en.isAlive == false && !p.hitting) {
hitmang(hit);
p.hitting = true;
}
}
etc etc...
is that what you were after?
Good luck i hope it helped :)
EDIT: I have not checked if this works, I wrote it inside the browser...
来源:https://stackoverflow.com/questions/14064426/how-would-i-put-enemies-into-an-arraylist