5

I'm trying to attach a sprite to another sprite and attach it behind its parent.

This is usually very easy, and I've one it before in my code- but for some reason, in once instance, it doesn't work.

The process is usually to set the parent's Z index to some number, and assign a lower Z index to its child. Here is code where rect is the parent, and icon is attached to it; both are attached to a parent entity. Then I've tried the sortChildren() method on everything (rect, parent entity, and even then scene itself); I know this is not efficient, but I just wanted to see if something catches on. It doesn't. icon is still being drawn over rect:

for (int i=0; i<levelsList.size(); i++) {
    rect = new Sprite(i*(width+padding), 
                      0, 
                      width, 
                      height, 
                      levelSelectorSquareRed, 
                      this.getVertexBufferObjectManager());
    icon = new Sprite((rect.getWidth()-innerWidth)/2f, 
                      (rect.getHeight()-innerHeight)/2f, 
                      innerWidth,
                      innerHeight, 
                      levelIcons.get(i), 
                      this.getVertexBufferObjectManager());
    rect.setZIndex(1);
    icon.setZIndex(0);
    rect.attachChild(icon);
    rect.sortChildren();
    levelSquares.attachChild(rect);
}

levelSquares.setPosition(0, (CAMERA_HEIGHT-height)/2f);
levelSquares.sortChildren();
levelSelectorScene.attachChild(levelSquares);
levelSelectorScene.sortChildren();

Logically, this should be overkill and have it working, but it isn't/ Am I missing anything?

Thanks

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
LoneDuck
  • 1,852
  • 1
  • 21
  • 28
  • I read that when you attach a child, it reverts the child's ZIndex to zero. Have you tried attaching before setting? I can't find verification for this, so... – Geobits Oct 19 '12 at 21:07
  • Yes, but sadly, that didn't work as well. I've solved this by attaching both sprites to an entity instead, and then sorted them in the entity. – LoneDuck Oct 19 '12 at 21:18
  • Yes, I saw that right after I commented. I assumed you wanted to keep the child/parent relationship, so figured it might be worth a try anyway. Glad you got it working. – Geobits Oct 19 '12 at 21:20

2 Answers2

17

There is something magical about SO. I can get stuck hard on a problem, finally give up and ask a question on this site. Within minutes the problem is solved with ease.

The solution:

No. Children in AndEngine GLES2 cannot be drawn behind their parents. But! Instead of attaching the child sprite to the parent sprite, you can attach both to an entity, give them Z indexes and then sort the entity.

levelSquares.attachChild(rect);
levelSquares.attachChild(icon);
rect.setZIndex(1);
icon.setZIndex(0);
levelSquares.sortChildren();
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
LoneDuck
  • 1,852
  • 1
  • 21
  • 28
  • 3
    I agree about the magic thing...... I think it has to do with the fact that in order to ask a question you need to clearly state the problem you have and what you've done to try to solve it. Sometimes that helps trigger alternative forms of thinking. Good luck with the game! – boltup_im_coding Oct 19 '12 at 22:00
  • This isn't true! set z-index to minus (f.e. -1) to draw it behind it's parent! setZIndex(-1); and don't forget to call parent.sortChildren(); – sjkm Dec 07 '15 at 16:49
5

If you are using AndEngine GLES2 it is pretty easy: after attaching the child, just set its Z-Index to a negative value.

Rodrigo Dias
  • 412
  • 4
  • 13