How would I put enemies into an ArrayList

我怕爱的太早我们不能终老 提交于 2019-12-24 08:13:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!