Shihab Uddin
Shihab Uddin

Reputation: 6931

set sprite on top layer in scene

I have two sprites here easyEnemy & bullDozer. easyEnemy spawning in every seconds .A bulldozer comes in a random time.

I set bulldozer Zindex = 10 & for easyEnemy I set nothing.

I want to show the bulldozer above all easyEnemy. But it not works.I call sortChildren() before calling the bulldozer on the scene. But it makes no sense.

public class Bulldozer extends PixelPerfectAnimatedSprite {

public Bulldozer(float pX, float pY,
        PixelPerfectTiledTextureRegion pTiledTextureRegion,
        VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);

    // SET z-INDEX FOR THIS
    setZIndex(20);
}  
}

In GameScene I call both easyEnemy and bullDozer. Firstly call easyEnemy then bullDozer.

Edited: Add Code

class GameScene extends Scene{
GameScene(){
// constructor 
createEasyEnemy();

CreateBullDOzer();

sortChildren();

}
public synchronized void createEasyEnemy(final float attackDuration,
            final float minTime, final float maxTime) {

        try {

            float delayTimer = attackDuration;
            aTimerHandler = new TimerHandler(delayTimer, true,
                    new ITimerCallback() {
                        @Override
                        public void onTimePassed(TimerHandler pTimerHandler) {
                            //
                            isEasyEnemyCreated = true;
                            engine.unregisterUpdateHandler(pTimerHandler);

                            final EasyEnemy aEasyEnemy = aEasyEnemyPoolObj
                                    .obtainPoolItem();
                            if (!aEasyEnemy.hasParent()) {
                                attachChild(aEasyEnemy);
                            }
                            aEasyEnemy.init(minTime, maxTime);
                            registerTouchArea(aEasyEnemy);
                            easyEnemyLinkedList.add(aEasyEnemy);
                        }
                    });

            registerUpdateHandler(aTimerHandler);
        } catch (Exception e) {
            Log.e("--CreateEasyEnemy-Error", "" + e);
        }
    }


}

How I can achieve this?

Upvotes: 1

Views: 616

Answers (1)

Renjith K N
Renjith K N

Reputation: 2633

Try to use layer concepts.This will meet your need and its really simple..
You need to add entity as layer and add spries to any layer you want
Here is a simple example

final int FIRST_LAYER = 0;
final int SECOND_LAYER = 1;

private void createLayers()
{
    scene.attachChild(new Entity()); // First Layer
    scene.attachChild(new Entity()); // Second Layer
}

Now you have two layers in scene and add sprite to any layer you like

scene.getChildByIndex(FIRST_LAYER).attachChild(yourEntity);

This one is a useful link,
One more thing
Remember that you need to add layers as the very first entities in your scene

Upvotes: 3

Related Questions