Java Box2D重新创建实体

Java Box2D重新创建实体,java,libgdx,box2d,Java,Libgdx,Box2d,在我的游戏中,我有一个Bullet类,负责在每次开火时制造新子弹。创建时,项目符号被添加到项目符号类,该类负责跟踪所述项目符号以及其余项目符号。我遇到了一个奇怪的行为: 在杀死敌人,然后再次射击后,这种新型子弹具有以下特点: 项目符号与项目符号相同(与相同代码id中的项目符号相同) 杀死了敌人。(即,如果id为: com.badlogic.gdx.physics.box2d。Body@fc7157f,则它将是 完全相同的id。) 子弹看起来会卡在原地,它的精灵不会移动,但根据游戏,它会有一个速度

在我的游戏中,我有一个
Bullet
类,负责在每次开火时制造新子弹。创建时,项目符号被添加到
项目符号
类,该类负责跟踪所述项目符号以及其余项目符号。我遇到了一个奇怪的行为:
在杀死敌人,然后再次射击后,这种新型子弹具有以下特点:

  • 项目符号与项目符号相同(与相同代码id中的项目符号相同) 杀死了敌人。(即,如果id为:
    com.badlogic.gdx.physics.box2d。Body@fc7157f
    ,则它将是 完全相同的id。)
  • 子弹看起来会卡在原地,它的精灵不会移动,但根据游戏,它会有一个速度,但位置保持不变。唯一可见的移动是当您启用
    box2d
    时,您可以看到身体向下移动,直到撞到地面,此时他“远程移动”后退,然后慢慢下降
  • 被卡住的子弹数量等于杀死的敌人数量
  • 这是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();
    }
    
    创建项目符号的代码已作为
    项目符号
    类发布。
    项目符号
    -是项目符号实体的
    数组。
    bulletGroundCollision
    -是一个int的
    数组
    ,它跟踪子弹在i位置(即索引)撞击地面的次数。

    更新2

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

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

    boolean enemyDead;
    
    if(damage<=0)
    {
     bulletClass.updateBulletLocation();
    }
    
    boolean-enemyDead;
    
    如果(损坏您是否使用
    子弹
    类来重用
    子弹
    ?另外,请显示
    子弹
    击中敌人以及创建
    子弹
    时处理的代码。我编辑了这篇文章,创建
    子弹`时的
    代码是
    子弹
    类。@RedDeadRedemption,你到底是怎么知道的你在检测碰撞吗?你在使用Box2D吗?我问你是因为你提到了小炮弹和“精灵”之间的碰撞。Box2D肯定不知道。另外,你能创建SSCCE吗?我在使用Box2D进行碰撞检测。