我将如何把敌人变成一个ArrayList [英] How would I put enemies into an ArrayList

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

问题描述

这是我的2个敌人

  EN =新敌人(700,150);
EN2 =新敌人(980,​​150);

我想有我的计划包含几个敌人,将我只是要重新创建它们,并与单位的碰撞

 如果(d.intersects(R1)及和放大器; en.visible ==真放;&安培;
        en.isAlive ==假放;&安培; !p.hitting){
    hitmang(HIT);
    p.hitting = TRUE;
}如果(d.intersects(R2)及&放大器; en.visible ==真放大器;&放大器;
        en.isAlive ==假放;&安培; !p.hitting){
    hitmang(HIT);
    p.hitting = TRUE;
}如果(!d.intersects(R1)及&放大器;!d.intersects(R2)){
    p.hitting = FALSE;
}

我必须重新创建每个实例我和另一个敌人?

这是我的敌人,再加上其边界在游戏

 矩形R1 = en.getBounds();
矩形R2 = en2.getBounds();

(我有他们为矩形)

 公共矩形的getBounds(){
    返回新的Rectangle(X,Y,114,134);
}


解决方案

我不明白究竟是什么你正在尝试做的?

你想通过他们你的敌人添加到一个数组,然后循环检查,如果敌人包围盒与玩家相​​交?

 公开名单<矩形> enBoundingBoxes =新的ArrayList<矩形>();

那么敌人边框添加到ArrayList

  enBoundingBoxes.add(en.getBounds());
enBoundingBoxes.add(en2.getBounds());

通过他们循环使用for循环:)

 的for(int i = 0; I< enBoundingBoxes.size();我++){
矩形tempBBox = enBoundingBoxes.get(ⅰ);如果(d.intersects(tempBBox)及和放大器; en.visible ==真放;&安培;!en.isAlive ==假放;&安培;!p.hitting){
    hitmang(HIT);
    p.hitting = TRUE;
   }
}等等...

是什么你是后?

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

编辑:如果这个工程我没有检查,我写它在浏览器内...

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

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆