Libgdx摧毁了多个尸体

时间:2014-09-15 08:51:31

标签: libgdx destroy bullet

所以基本上我在尝试摧毁不在屏幕范围内的物体时会出现错误。当其他类型的物体被摧毁时,我的一个类型的物体开始变得奇怪(例如子弹开始向后移动)这里是代码样本:

Maingame循环类:

Array<Body> bodies = new Array<Body>(world.getBodyCount());
    world.getBodies(bodies);
    for (Body body : bodies) {
        check = 0;
        if (BodyUtils.bodyIsEnemy(body)){
            update(body);
            check = 1;
        }
        if (BodyUtils.bodyIsBullet(body) && check == 0){
            update1(body);
            check = 0;
        }
    }

private void update(Body body) {
    if (!BodyUtils.bodyInBounds(body)) {
        if (BodyUtils.bodyIsEnemy(body) && !player.isHit()) {
            createEnemy();
        }
        world.destroyBody(body);
    }
}

private void update1(Body body) {
    if (!BodyUtils.bulletInBounds(body))
        world.destroyBody(body);
}

其他课程:

public static boolean bodyInBounds(Body body) {
        UserData userData = (UserData) body.getUserData();
        switch (userData.getUserDataType()) {
        case ENEMY:
            return body.getPosition().x + userData.getWidth() / 2 > 0;
        }
        return true;
    }

    public static boolean bulletInBounds(Body body) {
        UserData userData = (UserData) body.getUserData();
        switch (userData.getUserDataType()) {
        case BULLET:
            return body.getPosition().x + userData.getWidth() < 20;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

修正了它,在world.destroyBody(body)方法之后放置了createEnemy()方法。