在我的游戏中,我有一个Bullet
课程,每次开枪时都要负责制作新的子弹。在创建之后,子弹被添加到Bullets
类,该类负责跟踪所述子弹以及其余的子弹。我遇到了一个奇怪的行为:
在杀死敌人,然后再次射击后,新子弹具有以下特征:
com.badlogic.gdx.physics.box2d.Body@fc7157f
,那就是了
完全相同的身份。)Box2DDebugRenderer
时,你可以看到身体向下移动直到击中地面,此时他会传送"然后慢慢退下来。这是子弹类:
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
我注意到,在子弹被卡住之后,与子弹的碰撞不会发生在身体上,而是只有在与精灵发生碰撞时才会发生碰撞。
答案 0 :(得分:0)
您的代码有点难以阅读。但是,您需要确保在敌人死亡后,您需要使用子弹类中的更新方法再次更新子弹类。
boolean enemyDead;
if(damage<=0)
{
bulletClass.updateBulletLocation();
}
在你杀死敌人之后,我对你来说更新你的子弹类并不是我的看法,因此,子弹就会停留在原地。