本文介绍了碰撞时从 ArrayList 中删除项目(Java)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为前言,我搜索了论坛,但没有找到与我的具体情况相关的内容.我大约一周前才开始学习 Java,这是我第一次涉足面向对象编程.

As a preface, I have searched the forums but found nothing relating to my specific situation. I just started learning Java about a week ago, and this is my first foray into object oriented programming.

我正在构建一个基本游戏(在机制方面有点像 Space Invaders).我有一个Projectile"类,一个FallingThings"类(它是我所拥有的对象掉落(金钱,朋友,敌人)类的父类).发射的每个射弹都存储在一个 ArrayList 中,Money、Friend 和 Enemy 的每个实例(每个都在它们自己的 ArrayList 中)也是如此.

I'm building a basic game (think somewhat like Space Invaders when it comes to mechanics). I have a "Projectile" class, a "FallingThings" class (which is the parent class to the classes I have for objects falling down (Money, Friend, Enemy)). Each projectile that is shot is stored in an ArrayList, and as is every instance of Money, Friend, and Enemy (each in their own ArrayList).

问题发生在我实现碰撞检测时.首先,需要多个子弹才能使碰撞机制起作用(我想我可能只是在这里弄乱了一些数字,但我不是 100% 确定).现在,有时在我发射多颗子弹(它们早已消失)后,检测到碰撞而不发射另一颗子弹并且 FallingThings 对象从屏幕上消失.此外,在其他时候,多个对象会同时消失.最奇怪的是,所有这些都是不一致的,而且并不总是以相同的方式重现.但是,我与玩家角色的碰撞效果很好.

The problem happens when I implement collision detection. First off, it takes multiple bullets to make the collision mechanism work (I think I might have just messed up some numbers here but I'm not 100% sure). Now, sometimes after I fire multiple bullets (and they are long gone), a collision is detected without me firing another bullet and a FallingThings object disappears from the screen. Also, at other times, multiple objects disappear at once. The weirdest thing is that all this is inconsistent, and not always reproducible in the same fashion. However, my collisions with the player character work perfectly.

有人对我如何解决这个问题有一些想法吗?我的代码如下:

Anyone have some ideas as to how I can fix this? My code is below:

FallingThings"类中的方法(ArrayLists 定义在StartingClass"(主类)中).

Method in the "FallingThings" class (the ArrayLists are defined in the StartingClass (the main class)).

   public void checkBulletCollision(Rectangle rectangle) {

    for (int j = 0; j < Character.getProjectiles().size(); j++) {
        Projectile p = (Projectile) Character.getProjectiles().get(j);
        if (p.isVisible() == true) {

            for (int i = 0; i < StartingClass.getMoneys().size(); i++) {
                Money m = (Money) StartingClass.getMoneys().get(i);
                if (m.getR().intersects(rectangle)) {
                    m.remove(i);
                    System.out.println("bullet collision");
                }
            }
            for (int i = 0; i < StartingClass.getEnemies().size(); i++) {
                Enemy e = (Enemy) StartingClass.getEnemies().get(i);
                if (e.getR().intersects(rectangle)) {
                    e.remove(i);
                }
            }
            for (int i = 0; i < StartingClass.getFriends().size(); i++) {
                Friend f = (Friend) StartingClass.getFriends().get(i);
                if (f.getR().intersects(rectangle)) {
                    f.remove(i);
                }
            }
        }
    }
}

我的弹丸更新方法:

public void update() {
    y -= speedY;
    if (y < 0) {
        visible = false;
    }
    rectangle.setBounds(getRectangle());
}

我一整天都在努力解决这个问题,但仍然无法得到正确的实施.我曾尝试使用 ListIterator,但这会导致程序冻结并抛出类型转换错误.

I've been trying to fix this for the entire day, and still can't get a proper implementation. I have tried using ListIterator, but that caused the program to freeze and a typecasting error to be thrown.

非常感谢您的帮助!=)

Thank you so much for the help! =)

推荐答案

我怀疑您遇到了问题,因为您在循环遍历该列表时正在按索引从列表中删除项目.删除项目会导致索引计数器失去对齐.尝试使用实际的 Iterator.甚至不需要考虑索引.Iterator 有针对这些情况的 remove 方法..

I suspect you are having problems because you are removing items from the list by index while looping over that list. Removing items causes the index counter to get out of allignment. Try use an actual Iterator. No need to even think about indexes. The Iterator has a remove method for these situations..

Iterator<Enemy> iterator = StartingClass.getEnemies().iterator();
while (iterator.hasNext()) {
    Enemy e = iterator.next();
    if (e.getR().intersects(rectangle)) {
        iterator.remove();
    }
}

这篇关于碰撞时从 ArrayList 中删除项目(Java)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:43