将Sprite添加到要使用的池中

时间:2012-08-30 06:12:18

标签: java android andengine

好吧所以我遇到了一个小困境。

我有一个通用池,允许我回收和重用我的精灵。问题是,我有6个纹理,我想加载到池中,以确保他们每个都有机会被拉出池。

现在正在发生的是同一个精灵正在被回收并一遍又一遍地使用。这是我的泳池代码。

public class ObjectPool extends GenericPool<Sprite> {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================
private TextureRegion           texture1;
private TextureRegion           texture2;
private TextureRegion           texture3;
private TextureRegion           texture4;
private TextureRegion           texture5;
private TextureRegion           texture6;

private final Scene                     Gamescene;

private final VertexBufferObjectManager spriteVertexBuffer;

private Sprite                          fruit;

private final float                     CAMERA_WIDTH;
private final float                     CAMERA_HEIGHT;
public static boolean                   sprite_touched;
private final Camera                    camera;
// Variables

private final float                     minDuration = 3.1f;
private final float                     maxDuration = 5.1f;
private final TextureRegion             blueParticleRegion;

private final Sound                     PersonTouchedSound;

private PointParticleEmitter            particleEmitter;
private SpriteParticleSystem            particleSystem;
    private Random random = new Random();
// ===========================================================
// Constructors
// ===========================================================
public ObjectPool(final TextureRegion Region1,final TextureRegion Region2,final TextureRegion Region3,final TextureRegion Region4,final TextureRegion Region5, final TextureRegion Region6,
        VertexBufferObjectManager spriteBufferObject, Camera camera,
        Sound sound, Scene scene, TextureRegion particle) {

this.texture1 = Region1;
this.texture2 = Region2;
this.texture3 = Region3;
this.texture4 = Region4;
this.texture5 = Region5;
this.texture6 = Region6;



this.CAMERA_HEIGHT = camera.getHeight();
this.CAMERA_WIDTH = camera.getWidth();
this.camera = camera;
this.PersonTouchedSound = sound;
this.blueParticleRegion = particle;
Gamescene = scene;
initTrail(-70, -70);

this.spriteVertexBuffer = spriteBufferObject;

}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected Sprite onAllocatePoolItem() {

    int sprite = random.nextInt(1)+6;

    switch(sprite){
    case 1:
fruit = new Sprite(210, -10, 60, 100, this.texture1,
        this.spriteVertexBuffer) {
    @Override
    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
            final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
    // Detects if player is outside of bounds
    final float width = this.getWidth();
    final float height = this.getHeight();
    float x = pSceneTouchEvent.getX() - width / 2;
    float y = pSceneTouchEvent.getY() - height / 2;

    if (x < 0)
        x = 0;
    if (y < 0)
        y = 0;
    if (x > (CAMERA_WIDTH - width))
        x = CAMERA_WIDTH - width;
    if (y > (CAMERA_HEIGHT - height))
        y = (CAMERA_HEIGHT - height);

    moveTrail(x, this.getY());
    if (pSceneTouchEvent.isActionDown()) {

    this.setScale(2f);
    sprite_touched = true;
    PersonTouchedSound.play();
    }

    if (pSceneTouchEvent.isActionUp()) {
    this.setScale(1f);
    particleEmitter.setCenter(-70, -70);
    sprite_touched = false;
    }

    this.setPosition(x, this.getY());

    return true;

    }
};
break;

    case 2:

        fruit = new Sprite(210, -10, 60, 100, this.texture2,
                this.spriteVertexBuffer) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            // Detects if player is outside of bounds
            final float width = this.getWidth();
            final float height = this.getHeight();
            float x = pSceneTouchEvent.getX() - width / 2;
            float y = pSceneTouchEvent.getY() - height / 2;

            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            if (x > (CAMERA_WIDTH - width))
                x = CAMERA_WIDTH - width;
            if (y > (CAMERA_HEIGHT - height))
                y = (CAMERA_HEIGHT - height);

            moveTrail(x, this.getY());
            if (pSceneTouchEvent.isActionDown()) {

            this.setScale(2f);
            sprite_touched = true;
            PersonTouchedSound.play();
            }

            if (pSceneTouchEvent.isActionUp()) {
            this.setScale(1f);
            particleEmitter.setCenter(-70, -70);
            sprite_touched = false;
            }

            this.setPosition(x, this.getY());

            return true;

            }
        };
        break;
    case 3:
        fruit = new Sprite(210, -10, 60, 100, this.texture3,
                this.spriteVertexBuffer) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            // Detects if player is outside of bounds
            final float width = this.getWidth();
            final float height = this.getHeight();
            float x = pSceneTouchEvent.getX() - width / 2;
            float y = pSceneTouchEvent.getY() - height / 2;

            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            if (x > (CAMERA_WIDTH - width))
                x = CAMERA_WIDTH - width;
            if (y > (CAMERA_HEIGHT - height))
                y = (CAMERA_HEIGHT - height);

            moveTrail(x, this.getY());
            if (pSceneTouchEvent.isActionDown()) {

            this.setScale(2f);
            sprite_touched = true;
            PersonTouchedSound.play();
            }

            if (pSceneTouchEvent.isActionUp()) {
            this.setScale(1f);
            particleEmitter.setCenter(-70, -70);
            sprite_touched = false;
            }

            this.setPosition(x, this.getY());

            return true;

            }
        };
        break;
    case 4:
        fruit = new Sprite(210, -10, 60, 100, this.texture4,
                this.spriteVertexBuffer) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            // Detects if player is outside of bounds
            final float width = this.getWidth();
            final float height = this.getHeight();
            float x = pSceneTouchEvent.getX() - width / 2;
            float y = pSceneTouchEvent.getY() - height / 2;

            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            if (x > (CAMERA_WIDTH - width))
                x = CAMERA_WIDTH - width;
            if (y > (CAMERA_HEIGHT - height))
                y = (CAMERA_HEIGHT - height);

            moveTrail(x, this.getY());
            if (pSceneTouchEvent.isActionDown()) {

            this.setScale(2f);
            sprite_touched = true;
            PersonTouchedSound.play();
            }

            if (pSceneTouchEvent.isActionUp()) {
            this.setScale(1f);
            particleEmitter.setCenter(-70, -70);
            sprite_touched = false;
            }

            this.setPosition(x, this.getY());

            return true;

            }
        };
        break;
    case 5:
        fruit = new Sprite(210, -10, 60, 100, this.texture5,
                this.spriteVertexBuffer) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            // Detects if player is outside of bounds
            final float width = this.getWidth();
            final float height = this.getHeight();
            float x = pSceneTouchEvent.getX() - width / 2;
            float y = pSceneTouchEvent.getY() - height / 2;

            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            if (x > (CAMERA_WIDTH - width))
                x = CAMERA_WIDTH - width;
            if (y > (CAMERA_HEIGHT - height))
                y = (CAMERA_HEIGHT - height);

            moveTrail(x, this.getY());
            if (pSceneTouchEvent.isActionDown()) {

            this.setScale(2f);
            sprite_touched = true;
            PersonTouchedSound.play();
            }

            if (pSceneTouchEvent.isActionUp()) {
            this.setScale(1f);
            particleEmitter.setCenter(-70, -70);
            sprite_touched = false;
            }

            this.setPosition(x, this.getY());

            return true;

            }
        };
        break;
    case 6:
        fruit = new Sprite(210, -10, 60, 100, this.texture6,
                this.spriteVertexBuffer) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            // Detects if player is outside of bounds
            final float width = this.getWidth();
            final float height = this.getHeight();
            float x = pSceneTouchEvent.getX() - width / 2;
            float y = pSceneTouchEvent.getY() - height / 2;

            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            if (x > (CAMERA_WIDTH - width))
                x = CAMERA_WIDTH - width;
            if (y > (CAMERA_HEIGHT - height))
                y = (CAMERA_HEIGHT - height);

            moveTrail(x, this.getY());
            if (pSceneTouchEvent.isActionDown()) {

            this.setScale(2f);
            sprite_touched = true;
            PersonTouchedSound.play();
            }

            if (pSceneTouchEvent.isActionUp()) {
            this.setScale(1f);
            particleEmitter.setCenter(-70, -70);
            sprite_touched = false;
            }

            this.setPosition(x, this.getY());

            return true;

            }
        };
        break;
    }

return fruit;

}

@Override
protected void onHandleObtainItem(final Sprite pItem) {
pItem.reset();

}

@Override
protected void onHandleRecycleItem(final Sprite pItem) {
pItem.setVisible(false);
pItem.setPosition(210, -5);
pItem.setIgnoreUpdate(true);
particleEmitter.setCenter(-70, -70);
pItem.clearEntityModifiers();
}

private void initTrail(float x, float y) {

this.particleEmitter = new PointParticleEmitter(x, y);

this.particleSystem = new SpriteParticleSystem(particleEmitter, 100, 100,
        360, this.blueParticleRegion, spriteVertexBuffer);

particleSystem.addParticleInitializer(new AlphaParticleInitializer<Sprite>(
        1));
particleSystem
        .addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(
                GLES20.GL_BLEND_COLOR, GLES20.GL_ONE));
particleSystem
        .addParticleInitializer(new VelocityParticleInitializer<Sprite>(0));
particleSystem
        .addParticleInitializer(new ExpireParticleInitializer<Sprite>(.6f));
particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 1,
        1, 0));

Gamescene.attachChild(particleSystem);
}

public void moveTrail(float x, float y) {
particleEmitter.setCenter(x, y);
}

private void fillPool(){

}

}

1 个答案:

答案 0 :(得分:0)

您必须创建6个池而不是一个或使用TiledTextureRegion并在获取项目时切换到所需的tile。