I have a game where the level is generated out of block objects, each block has it’s own health (3hp at the moment), and when shot will lose 1 health. And when health is <= 0, then the block will be ‘destroyed’. Or at least that is what’s supposed to happen. Instead upon hitting one block with one bullet, all the blocks on screen end up getting destroyed. I’m not sure if the issue lies in the collision, or the creation of the blocks themselves.
I have the code for creating the blocks:
[java]public lvlBlock(AssetManager manage, float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
blockNd = new Node();
blk_phy = new RigidBodyControl();
bulletAppState = new BulletAppState();
manager = manage;
initMaterials();
initBlock();
}
private void initMaterials()
{
blk_mat = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
blk_mat.setColor("Color", ColorRGBA.Blue);
}
private void initBlock()
{
block = new Box(0.5f, 0.5f, 0.5f);
block.scaleTextureCoordinates(new Vector2f(1f, 1f));
//block = new Box(.5f, .5f, .5f);
geom = new Geometry("Box", block);
geom.setLocalTranslation(x, y, z);
blk_phy = new RigidBodyControl(0.0f);
blk_phy.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_03);
geom.addControl(blk_phy);
Material mat = new Material(manager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
blockNd.attachChild(geom);
blockNd.setName("lvlBlk");
Main.bulletAppState.getPhysicsSpace().add(blk_phy);
Main.bulletAppState.getPhysicsSpace().addCollisionListener(this);
}[/java]
And it’s collision:
[java]@Override
public void collision(PhysicsCollisionEvent event)
{
if(event.getNodeA().getName().equals("Bullet"))
{
System.out.println("Hit with bullet, healt = " + Health);
Health = Health - 1;
}
else if(event.getNodeB().getName().equals("Bullet"))
{
System.out.println("Hit with bullet, healt = " + Health);
Health = Health - 1;
}
if(this.Health <= 0)
{
this.destroyBlock();
}
} [/java]
And then the block’s destroyBlock() method:
[java]private void destroyBlock()
{
System.out.println("Destroying block: " + this);
Main.bulletAppState.getPhysicsSpace().remove(blk_phy);
blockNd.removeFromParent();
}[/java]
For some reason once a bullet object hits a block, all the block’s Health variables begin to decrement continuously, even after the bullet has been removed from the game. The bullet object’s removal method is the same as the destroyBlock() method, it’s collision() method is also the same format, only it just calls destroyBullet() when it collides with a block.
Any help/advice would be appreciated.