PDA

View Full Version : Bitmap Has Higher Index Than Sprite?



jebego
November 10th, 2009, 10:04 PM
I've just switched over to AS3, and I'm completely stumped with something.

I'm working on a project where I keep adding children to a MC already on the screen. There are two types of these children: Sprites that I use to draw lines using moveTo, lineTo, ect. Then there's sprites that have children inside of THEM that are bitmaps.

For some strange reason, the Sprites with the bitmaps on them always have a higher depth/index (AS3 terminology is still weird for me) than the normal Sprites.

Here's a diagram of my MC/children/children layers:

---------------=> (Sprites that draw lines)
(MC on Stage) =>
---------------=> (Sprites that hold bitmaps) => (Bitmap)
__________________________________________________ ____

I'll post my code if need be. I'm just hoping there's a simple answer!

Thanks!

Shaedo
November 10th, 2009, 10:23 PM
If I have understood what you have said, I would say not:


var mc:MovieClip = new MovieClip();
addChild(mc); // this is supposed to be your "MC already on the screen"

//some sprites, some contain bitmaps some don't:
//note the name correspond to the order they are added to the mc

var sp1:Sprite = new Sprite();//will contain bitmap
sp1.name='1';
mc.addChild(sp1);

var sp2:Sprite = new Sprite();//will NOT contain bitmap
sp2.name='2';
mc.addChild(sp2);

var sp3:Sprite = new Sprite();//will contain bitmap
sp3.name='3';
mc.addChild(sp3);

var sp4:Sprite = new Sprite();//will NOT contain bitma
sp4.name='4';
mc.addChild(sp4);

//bitmaps created and added to the relevant sprites

var bmd1:BitmapData = new BitmapData(80, 30, false, 0xFF0000);
var bm1:Bitmap = new Bitmap(bmd1);
sp1.addChild(bm1);

var bmd3:BitmapData = new BitmapData(80, 30, false, 0xFF9999);
var bm3:Bitmap = new Bitmap(bmd3);
sp3.addChild(bm3);

// run through the children of the mc and trace their names in order
for(var i:uint=0;i<mc.numChildren;i++)
{
trace(mc.getChildAt(i).name);
}
//traces:
//1
//2
//3
//4

might have to paste some code....

Luck,
S

Krilnon
November 10th, 2009, 10:25 PM
I don't think that there's any particular reason why that should be happening (I mean, other than that something you're doing in your code is making them do that).

If you use DisplayObjectContainer#addChild, you're adding to the top of that display list, so those objects will overlap items that were added earlier. addChildAt(someChild, 0) will add the child to the bottom of the list, so that everything else will be covering up the newly added child if they overlap. Using just those two methods, you could guarantee that whatever you wanted would be on top, but you can also use swapChildren and swapChildrenAt.

You should also be careful to make sure that you're not adding all of your bitmaps to a single container (instead of multiple containers) that is being placed below all of your line-drawing Sprites.

Edit: You now apparently have your choice between two responses!

jebego
November 10th, 2009, 10:33 PM
Thanks for the responses! I'll look through what you two said and see if my problem is solved =)

jebego
November 10th, 2009, 10:41 PM
Well, I looked through you two's responses, and they really got my brain wrapped around the idea of children is AS3. In the end, I just put the sprites that drew the lines in other container sprites, and it now works perfectly!

Thanks a ton!

Krilnon
November 10th, 2009, 10:43 PM
*cracks open a bottle of strawberry banana fruit smoothie blend in celebration, offers some to Shaedo and jebego*

Shaedo
November 10th, 2009, 10:59 PM
@ jebego nice. good work!
@ Krilnon lol hell yeah! now you are talking my language!