Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Sprite layer in andengine

How to change Sprite's layer during runtime in andengine Scene?

like image 392
stackuser Avatar asked Aug 20 '11 05:08

stackuser


2 Answers

It depends on what you understand by changing layer. If you mean z index of entity, then you can use entity.setZIndex(int) and you need to call parent.sortChildren() manually on the parent of your sprite.

If by changing layer you mean detaching from one parent and attaching to another parent, then that is another problem.

like image 81
Ludevik Avatar answered Oct 17 '22 01:10

Ludevik


I have been recently working on this issue. It is frustrating maintaining the zorder of sprites using sortChidren and setZOrder, especially if you have a complex arrangment of layers(Scene Entitys).

I found that adding this override to every Entity derived layer you have.

@Override
protected void onDrawChildren(GL10 pGL, Camera pCamera) {
    getParent().sortChildren();
    super.onDrawChildren(pGL, pCamera);
}

This lets you set the Z order sprite.setZOrder(order++) and you are guaranteed your entity's a drawn in the sprite order you have set at least.

I'm guessing this adds quite a bit of overhead, but I don't notice on my galaxy ace. (yet to try profiling it)

The problem is that when you call setZOrder(...) you need to call sortChildren after attaching every subsequent sprite/entity added to the parent, otherwise any higher zorder entity will get drawn over by newly added entities as the setZOrder does not actually set the z axis of the 3d object. Only the order in which the object is drawn. (all andengine objects have the same z axis value regardless of the mZIndex value in Entity) Hence you must sort the children whenever a new entity is added to the parent entity.

This is what I have so far. Please correct me if I'm wrong or have any better solutions.

like image 2
Justin Avatar answered Oct 17 '22 01:10

Justin