box2d重建实体

kmbjn2e3  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(336)

在我的游戏中我有一个 Bullet 班级,负责在每次开枪时制造新子弹。创建时,项目符号将添加到 Bullets 班级,负责跟踪所说的子弹,以及其余的子弹。我遇到了一个奇怪的行为:
在杀死一个敌人,然后再次射击后,这种新型子弹有以下特点:
子弹和杀死敌人的子弹是同一颗(在同一代码id中)(i、 e.如果id是: com.badlogic.gdx.physics.box2d.Body@fc7157f ,则将是完全相同的id。)
子弹看起来会卡在原地,它的精灵不动,但根据游戏它会有一个速度,但位置保持不变。唯一可见的移动是启用 Box2DDebugRenderer ,你可以看到身体向下移动,直到碰到地面,这时他“传送”回来,然后慢慢地倒下。
卡住的子弹数量等于杀死的敌人数量。
这是bullet类:

public class Bullet {

private Body bullet;

public Bullet(final float force, final int bulletDmg, final Weapon weapon,
        final World world) {
    System.out.println("Position " + weapon.getPosition() + ", Angle: "
            + weapon.getAngle());
    final BodyDef bulletDef = new BodyDef();
    bulletDef.type = BodyDef.BodyType.DynamicBody;
    bulletDef.angle = weapon.getAngle();
    bulletDef.position.set(
            weapon.getPosition().x
                    + (float) (2.5 * MathUtils.cos(weapon.getAngle())),
            weapon.getPosition().y
                    + (float) (2.5 * MathUtils.sin(weapon.getAngle())));
    bulletDef.angle = weapon.getAngle();
    PolygonShape bulletShape_1 = new PolygonShape();
    bulletShape_1.setAsBox(0.34375f, 0.34375f);
    CircleShape bulletShape_2 = new CircleShape();
    bulletShape_2.setPosition(new Vector2(0.34375f, 0));
    bulletShape_2.setRadius(0.34375f);
    final FixtureDef bulletFixture_1 = new FixtureDef();
    bulletFixture_1.density = 1f;
    bulletFixture_1.shape = bulletShape_1;
    bulletFixture_1.friction = 0.25f;
    bulletFixture_1.restitution = 0.75f;
    final FixtureDef bulletFixture_2 = new FixtureDef();
    bulletFixture_2.density = 1;
    bulletFixture_2.shape = bulletShape_2;
    bulletFixture_2.friction = 0.25f;
    bulletFixture_2.restitution = 0.75f;
    final Timer creationTimer = new Timer();
    creationTimer.scheduleTask(new Task() {

        @Override
        public void run() {
            if (!world.isLocked()) {
                System.out.println(bullet);
                bullet = world.createBody(bulletDef);
                bullet.createFixture(bulletFixture_1);
                bullet.createFixture(bulletFixture_2);
                System.out.println(bullet);
                bullet.applyForceToCenter(
                        force * MathUtils.cos(weapon.getAngle()), force
                                * MathUtils.sin(weapon.getAngle()), true);
                Sprite sprite = new Sprite(new Texture(
                        "sprites\\Weapon\\bullet_standard.png"));
                sprite.setSize(1.03125f, 0.6875f);
                sprite.setOrigin((float) (sprite.getWidth() / 2 - 0.12f),
                        (float) (sprite.getHeight() / 2));
                bullet.setUserData(sprite);
                Bullets bullets = Bullets.getInstance(world);
                bullets.addBullet(bullet);
                bullets.setDmg(bulletDmg);
                System.out.println("Create bullet number: " + bullet);
                creationTimer.stop();
            }
        }

    }, 0, 1);
    creationTimer.start();
}

}
我已经面对这个问题很长一段时间了,现在不能解决这个问题,我希望能得到一些帮助。提前谢谢!

更新1:

我不重复使用任何创建的项目符号。
这是处理与敌人碰撞的代码:

public void onCollision(final Body collidedBody, final String bodyHit,
        final int index) {
    assert instance != null;
    final Timer timer = new Timer();
    timer.scheduleTask(new Task() {
        @Override
        public void run() {
            if (!world.isLocked()) {
                Circles circles = Circles.getInstance();
                if (bodyHit.equalsIgnoreCase("ground")) {
                    if (bulletGroundCollision.get(index) == 5) {
                        if (bullets.get(index) != null) {
                            world.destroyBody(bullets.get(index));
                            bullets.removeIndex(index);
                            bulletGroundCollision.removeIndex(index);
                        }
                    } else
                        bulletGroundCollision.set(index,
                                (bulletGroundCollision.get(index) + 1));
                } else if (bodyHit.equalsIgnoreCase("enemy")) {
                    Circle enemy = circles
                            .findAccordingToCode(collidedBody);
                    enemy.damaged(bulletDmg);
                    System.out.println("Hit at: "
                            + bullets.get(index).getPosition());
                    if (bullets.get(index) != null) {
                        world.destroyBody(bullets.get(index));
                        bullets.removeIndex(index);
                        bulletGroundCollision.removeIndex(index);
                    }
                } else if (bodyHit.equalsIgnoreCase("player")) {
                    if (bullets.get(index) != null) {
                        world.destroyBody(bullets.get(index));
                        bullets.removeIndex(index);
                        bulletGroundCollision.removeIndex(index);
                    }
                    Square square = Square.getInstance(world);
                    square.damaged(bulletDmg);
                }
                timer.stop();
            }
        }

    }, 0, 1);
    timer.start();
}

项目符号创建的代码已作为 bullet 班级。 bullets -是一个 Array 子弹的尸体。 bulletGroundCollision -是一个 Array 跟踪子弹在i位置(即索引)撞击地面的次数的整数。

更新2

我注意到子弹被卡住后,与子弹的碰撞不会根据身体发生,而是只有在与精灵发生碰撞时才会触发碰撞。

mbyulnm0

mbyulnm01#

你的代码有点难读。但是,您需要确保在敌人死后,您需要使用bullet类中的update方法再次更新bullet类。

boolean enemyDead;

if(damage<=0)
{
 bulletClass.updateBulletLocation();
}

在我看来,你在杀死敌人之后,并没有更新你的子弹等级,正因为如此,子弹才留在原处。

相关问题