ashley
ashley copied to clipboard
Why I cannot recreate entity after removing entity from the engine?
In this below code method, I've tested creating and removing entity, because I'm planning to create bullet pool this way.
public void build () {
Entity bulletTest1 = createBullet(0,0); // works perfectly but
removeEntity(bulletTest1); // after engine remove this bulletTest1 entity then
Entity bulletTest2 = createBullet(0,0); // recreate it, it doesn't work anymore
}
In this below code example I've create a BulletFactory that creates bullet entity made from engine.
public Entity createBullet(float x, float y) {
Entity entity = createEntity(Constants.Flags.BULLET);
BulletComponent bullet = engine.createComponent(BulletComponent.class);
SizeComponent size = engine.createComponent(SizeComponent.class);
TextureComponent texture = engine.createComponent(TextureComponent.class);
PhysicsComponent physics = engine.createComponent(PhysicsComponent.class);
TransformComponent transform = engine.createComponent(TransformComponent.class);
MovementComponent movement = engine.createComponent(MovementComponent.class);
texture.region = GameAssets.cannonball;
transform.position.set(x, y);
size.width = 0.25f;
size.height = 0.25f;
transform.origin.set(size.width / 2, size.height /2);
float bodyPosX = transform.position.x + transform.origin.x;
float bodyPosY = transform.position.y + transform.origin.y;
physics.body = createBulletBody(entity, bodyPosX, bodyPosY);
entity.add(bullet);
entity.add(size);
entity.add(texture);
entity.add(physics);
entity.add(transform);
entity.add(movement);
engine.addEntity(entity);
return entity;
}
public Entity createEntity(Constants.Flags flag) {
Entity e = engine.createEntity();
e.flags = flag.ordinal();
return e;
}
Could you please define "it doesn't work anymore"? I couldn't possibly know what the problem is.
Also, if you think this is a bug, could you please write a failing jUnit test so we can reproduce and fix the issue?
The "it doesn't work anymore" means on the second creation of bullet, there's no texture and body created ,so I assume it doesn't work. Let me try the failing jUnit test.