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

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

问题描述

我检测玩家和许多相同的物体之间的碰撞(在这种情况下,壁砖)时遇到的一个问题。问题是,我的程序只检测到碰撞时,我与最后添加墙砖联系我。这是用Java编写...

我用的长方形(他们相交法)检测碰撞。

code样本:

更新砖:

 的for(int i = 0; I< wallTileArr.size();我++)
    {
        wallTile = wallTileArr.get(ⅰ);
        wallTile.update(P);
        如果(wallTile.getBounds()。相交(Screen.getBounds()))
        {
            wallTile.draw =真;
        }
        否则如果(!wallTile.getBounds()。相交(Screen.getBounds()))
        {
            wallTile.draw = FALSE;
        }
    }

下面是我的课,使墙出来的砖:

 公共墙(INT X,INT Y,INT长度,INT目录,WallTile wallTile)
     {
 this.x = X;
 this.y = Y;
 this.length =长度;
 this.wallTile = wallTile;
    的for(int i = 0; I<长度;我+ +)
    {
        如果(DIR == 0)
        {
            Methods.addWallTile(新WallTile(X +(wallTile.size * I),Y));
        }
        否则如果(DIR == 1)
        {
            Methods.addWallTile(新WallTile(X,Y +(wallTile.size * I)));
        }        否则如果(DIR!= 0 ||迪尔!= 1)
        {
            的System.out.println(在制定方向错误:0 =水平,1 =垂直您的意见是:+方向);
            //Methods.Exit(1);
        }    }

}

这是我的班,使一个房间或者房子的墙壁:

 公共HouseSmall_1(INT X,INT Y,INT大小,INT旋转,WallTile wallTile)
    {
this.x = X;
this.y = Y;
this.wallTile = wallTile;
this.size =大小;
如果(旋转== 0)
{
    Methods.addWall(新墙(X,Y,大小,1,wallTile));
    Methods.addWall(新墙(X +(尺寸* wallTile.size),Y,大小,1,wallTile));    Methods.addWall(新墙(X,Y,大小,0,wallTile));
    Methods.addWall(新墙(X,Y +(尺寸* wallTile.size),(大小)/ 2,0,wallTile));
    Methods.addWall(新墙(X + wallTile.size +((大小* wallTile.size)/ 2),Y +(尺寸* wallTile.size),(尺寸/ 2),0,wallTile));
}

}

在code,增加了瓷砖和墙壁到他们的ArrayList

 公共静态无效addWallTile(WallTile wallTile)
{
    Controller.wallTileArr.add(wallTile);
}公共静态无效addWall(墙墙)
{
    Controller.wallArr.add(墙);
}

更新code在wallTile.java文件中的冲突:

 公共无效更新(球员P)
{
    X + = Screen.movementX;
    Y + = Screen.movementY;    如果(的getBounds()。相交(p.upGetBounds()))
    {        p.walk = FALSE;    }
    其他
    {
        p.walk = TRUE;
    }
}

这是怎么回事?你怎么建议我解决这个问题?

随意问更多的code样品!


解决方案

 如果(的getBounds()。相交(p.upGetBounds()))
{    p.walk = FALSE;}
其他
{
    p.walk = TRUE;
}

问题是这样的code。要重设标志,是不是与球员发生碰撞的每瓦,因此只有最后一张牌可以与玩家成功碰撞,并没有得到该标志复位。

即 - 比方说,有2瓦和 1 的碰撞, 2 不是。

在循环的第一次迭代 -

1 的碰撞=> p.walk = FALSE;

在循环的第二次迭代 -

2 不是碰撞=> p.walk = TRUE; //值已被重置。

但是,如果 2 是一个coliding,标志不能被重置。

要解决:删除其他子句和重新设置的一次真正 < STRONG>之前的循环。

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.

Code samples:

Update tiles:

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

}

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

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 - Lets say there are 2 tiles and #1 is colliding and #2 is not.

In first iteration of the loop -

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

In second iteration of the loop -

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

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

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

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

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