libgdx ashley - 为什么我从引擎中删除实体后无法重新创建实体?

时间:2016-07-18 18:54:52

标签: java libgdx amazon-ecs

在下面的代码方法中,我测试了创建和删除实体,因为我打算以这种方式创建子弹池。

 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 
 }

在下面的代码示例中,我创建了一个BulletFactory,用于创建由引擎制作的子弹实体。

 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;
}

0 个答案:

没有答案
相关问题