简体   繁体   English

发生碰撞时如何消失图像?

[英]How to disappear the image when there is a collision?

I have two images that move in a horizontal direction. 我有两个水平移动的图像。 Whenever there is a collision between the two images, I want them to disappear and that is my problem. 每当两个图像之间发生冲突时,我希望它们消失,这就是我的问题。 Could someone help me how to do that? 有人可以帮我怎么做吗?

By the way, this is my code. 顺便说一句,这是我的代码。 You can check it if you like: 您可以根据需要检查它:

private boolean checkCollisions()
{
    for (Sprite r1 : z_sorted_sprites)
    {
        for (Sprite r2 : z_sorted_sprites)
        {
            if (r1 == r2)
            {
                continue;
            }

            RectangleX me = r1.getBounds();
            RectangleX other = r2.getBounds();

            if (me.intersects(other))
            {
                collision = true;

                System.out.println("collision : " + r1.getName() + " with " + r2.getName());

                // disappear(me,other);
            }
            else
            {
                collision = false;
                System.out.println("no collision");
            }
        }

    }
    return collision;
}

Try the following: 请尝试以下操作:

In your paint(Graphics g) method, do the following: paint(Graphics g)方法中,执行以下操作:

if(!checkCollisions()){
   //draw your sprites
   //g.drawImage(...);
}

And in your checkCollisions() method, call repaint when you detect a collision. 并且在您的checkCollisions()方法中,当检测到碰撞时调用repaint。

if (me.intersects(other))
            {
                collision = true;

                System.out.println("collision : " + r1.getName() + " with " + r2.getName());

                repaint();
            }

Because you're iterating over the same collection twice, I'm pretty sure you have to tackle it by building a list of items to remove, then removing them once all the collisions have been detected. 因为您要遍历同一集合两次,所以我很确定您必须通过构建要删除的项目列表来解决它,然后在检测到所有冲突后将其删除。

Like so: 像这样:

private boolean checkCollisions()
{
    ArrayList<Sprite> toRemove = new ArrayList<Sprite>();

    for (Sprite r1 : z_sorted_sprites)
    {
        for (Sprite r2 : z_sorted_sprites)
        {
            // ...
            if (me.intersects(other))
            {
                // ...
                toRemove.add(r1);
                toRemove.add(r2);
            }
            else
            {
                // ...
            }
        }
    }

    z_sorted_sprites.removeAll(toRemove);

    return collision;
}

Note that when two sprites collide, they each get added to the toRemove list twice. 请注意,当两个精灵碰撞时,它们分别被添加到toRemove列表两次。 You can prevent this and make your loop more efficient by changing your for loops like so (assuming z_sorted_sprites is a List ): 您可以通过这样更改for循环来防止这种情况并使循环更有效(假设z_sorted_sprites是一个List ):

for (int i = 0; i < z_sorted_sprites.size(); i++)
{
    Sprite r1 = z_sorted_sprites.get(i);
    for (int j = i + 1; j < z_sorted_sprites.size(); j++)
    {
        Sprite r2 = z_sorted_sprites.get(j);
        // ...
    }
}

As an aside, are you sure your return value is returning what you think it is? 顺便说一句,您确定您的返回值正在返回您认为的样子吗? Currently it will return false unless the last two sprites in the list happen to collide. 当前,除非列表中的最后两个精灵碰巧碰撞,否则它将返回false。 If you want it to return true if there are any collisions, get rid of the line collision = false; 如果希望它在发生任何冲突时返回true,则摆脱collision = false; It might be more useful to return a count of the number of collisions, but I leave that to you... 返回碰撞次数的计数可能更有用,但是我留给您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM