How to change Sprite's layer during runtime in andengine Scene?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With