ArrayList 只检测最后添加的对象的碰撞 [英] ArrayList only detects collision of last added object

查看:20
本文介绍了ArrayList 只检测最后添加的对象的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检测玩家与许多相同物体(在本例中为墙砖)之间的碰撞时遇到了问题.问题是我的程序仅在与最后添加的 墙砖接触时才检测到碰撞.这是用java写的...

I've run into a problem when detecting collision between the player and many of the same objects(in this case wall tiles). The problem is that my program only detects the collision when I'm in contact with the last added wall tile. This is written in java...

我正在使用矩形(它们的相交方法)来检测碰撞.

I am using rectangles(their intersects method) to detect collision.

代码示例:

更新磁贴:

        for (int i = 0; i < wallTileArr.size(); i++)
    {
        wallTile = wallTileArr.get(i);
        wallTile.update(p);
        if(wallTile.getBounds().intersects(Screen.getBounds()))
        {
            wallTile.draw = true;
        }
        else if(!wallTile.getBounds().intersects(Screen.getBounds()))
        {
            wallTile.draw = false;
        }
    }

这是我用瓷砖制作墙的课程:

Here is my class that makes a wall out of the tiles:

     public Wall(int x, int y, int length, int dir, WallTile wallTile)
     {
 this.x = x;
 this.y = y;
 this.length = length;
 this.wallTile = wallTile;


    for(int i = 0; i < length; i++)
    {
        if (dir == 0)
        {
            Methods.addWallTile(new WallTile(x+(wallTile.size*i), y));
        }
        else if (dir == 1)
        {
            Methods.addWallTile(new WallTile(x, y+(wallTile.size*i)));
        }

        else if (dir != 0 || dir != 1)
        {
            System.out.println("Error in setting direction: 0 = horizontal, 1 = vertical. Your input is: "+dir );
            //Methods.Exit(1);
        }

    }

}

这是我的班级,用墙壁建造房间或房子:

And here is my class that makes a room or house with walls:

     public HouseSmall_1(int x, int y,int size, int rotation, WallTile wallTile)
    {
this.x = x;
this.y = y;
this.wallTile = wallTile;
this.size = size;


if (rotation == 0)
{
    Methods.addWall(new Wall(x, y, size, 1, wallTile));
    Methods.addWall(new Wall(x+(size*wallTile.size), y, size, 1, wallTile));

    Methods.addWall(new Wall(x, y, size, 0, wallTile));
    Methods.addWall(new Wall(x, y+(size*wallTile.size), (size)/2, 0, wallTile));
    Methods.addWall(new Wall(x+wallTile.size+((size*wallTile.size)/2), y+(size*wallTile.size),(size/2), 0, wallTile));
}

}

将瓷砖和墙壁添加到它们的arrayList中的代码:

The code that adds the tiles and walls into their arrayList:

public static void addWallTile(WallTile wallTile)
{
    Controller.wallTileArr.add(wallTile);
}

public static void addWall(Wall wall)
{
    Controller.wallArr.add(wall);
}

更新 wallTile.java 文件中碰撞的代码:

Update code for the collision in the wallTile.java file:

    public void update(Player p)
{
    x+=Screen.movementX;
    y+=Screen.movementY;

    if(getBounds().intersects(p.upGetBounds()))
    {

        p.walk = false;

    }
    else
    {
        p.walk = true;
    }
}

为什么会这样?你建议我如何修复它?

Why is this happening? And how do you suggest I fix it?

欢迎索取更多代码示例!

Feel free to ask for more code samples!

推荐答案

if(getBounds().intersects(p.upGetBounds()))
{

    p.walk = false;

}
else
{
    p.walk = true;
}

问题是这段代码.您正在为每个未与玩家碰撞的瓷砖重置标志,因此只有最后一个瓷砖才能与玩家成功碰撞并且不会重置标志.

The problem is this code. You are resetting the flag for every tile that is NOT colliding with the player, and thus only last tile can collide successfully with the player and not get the flag reset.

i.e - 假设有 2 个瓦片并且 #1 正在碰撞而 #2 没有.

i.e - Lets say there are 2 tiles and #1 is colliding and #2 is not.

在循环的第一次迭代中 -

In first iteration of the loop -

#1 正在碰撞 => p.walk = false;

#1 is colliding => p.walk = false;

在循环的第二次迭代中 -

In second iteration of the loop -

#2 没有碰撞 => p.walk = true;//值已被重置.

#2 is NOT colliding => p.walk = true; //value has been reset.

但是,如果 #2 是一个冲突,则标志无法重置.

However if #2 was the one coliding, the flag cannot get reset.

修复:删除 else 子句并将其一次重置为 true before for 循环.

To fix : remove the else clause and reset it once to true before the for loop.

这篇关于ArrayList 只检测最后添加的对象的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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