本文介绍了我如何将敌人放入 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的两个敌人

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();

(我把它们作为矩形)

public Rectangle getBounds(){
    return new Rectangle(x, y, 114, 134);
}

推荐答案

我不太明白你想要做什么?

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());

使用 for 循环遍历它们 :)

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...

这就是你所追求的吗?

祝你好运,我希望它有所帮助:)

Good luck i hope it helped :)

我没有检查这是否有效,我是在浏览器中写的...

I have not checked if this works, I wrote it inside the browser...

这篇关于我如何将敌人放入 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:05