0

我有一组形状,需要确定哪些形状与哪些形状相交。我目前使用两个增强for循环(一个在另一个循环中),但它没有按预期工作。它似乎只是将 aShape与自身进行比较。我添加了一个println()用于调试目的,控制台输出在这个问题的末尾。

编辑:我已经为我的界面添加了代码,Drawables以显示如何公开intersects(Rectangle rectangle)Shape.getBounds()公开。我还添加了我的Collisions类来展示它的方法的作用。

编辑2:我不敢相信我忽略了:drawables是一个集合!drawables.add()表现得像Set.add()

*编辑 8564985294238424: *我从不初始化 Set collisions。我已经解决了这个问题。现在可以了。


类 CollisionMonitor(包括问题方法:Set<Collision> getCollisions()这是类中的最后一个)

package platformer;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


public class CollisionMonitor
{

private Set<Collision> collisions;
private Set<Drawable> drawables;

public CollisionMonitor(Set<Drawable> drawables)
{
    this.drawables = drawables;
}

public Set<Drawable> getDrawables()
{
    return drawables;
}

public void setDrawables(Collection<? extends Drawable> drawables)
{
    if (drawables != null)
        this.drawables = (new HashSet<Drawable>(drawables));
}

public Set<Drawable> addDrawables(Collection<? extends Drawable> drawables)
{
    if (drawables != null && drawables.isEmpty() == false)
        this.drawables.addAll(drawables);
    return this.drawables;
}

public Set<Collision> getCollisions()    //Problem method!
{
    Set<Drawable> temp = drawables;

    for (Drawable s : drawables)
    {
        for (Drawable ss : temp)
        {
            System.out.println(s + ", " + ss);
            /*if (s.intersects(ss.getBounds()))
            {
                collisions.add(new Collision(s, ss));
            }*/
        }
    }

    return collisions;
}

}

界面可绘制:

public interface Drawable extends Shape
{
    Color color = new Color(0, 0, 0);

    public Color getColor();
}

类冲突

public class Collision
{

    Drawable drawable1, drawable2;

    public Collision(Drawable drawable1, Drawable drawable2)
    {
        this.drawable1 = drawable1;
        this.drawable2 = drawable2;
    }

}

输出:

platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@26dea769, platformer.Bullet@26dea769
platformer.Bullet@f1e9457d, platformer.Bullet@f1e9457d
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@11bd2f5a, platformer.Bullet@11bd2f5a
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@70a511a, platformer.Bullet@70a511a
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@6ada16bc, platformer.Bullet@6ada16bc
platformer.Bullet@6ba127e8, platformer.Bullet@6ba127e8
4

1 回答 1

0

temp和 没有什么不同drawables。它指向同一个对象。

如果要排除完全相同的元素,请使用:

for (Drawable s : drawables)
{
    for (Drawable ss : temp)
    {
        if (s == ss) continue;  // don't compare the object to itself
于 2013-05-20T05:09:12.643 回答