0

好的,所以我遇到了一个小难题。

我有一个通用池,可以让我回收和重复使用我的精灵。问题是我有 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(){

}

}
4

1 回答 1

0

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

于 2012-09-06T20:39:45.220 回答