views:

200

answers:

2

I havn't been able to find the answer to this, and I hope theres an easy and obvious answer i just havn't found yet...

Within flex (i.e. using actionscript and mxml), given two Sprites, is there a way to force one to be displayed on top of the other when they overlap?

Thanks!

+3  A: 

Yep, it all depends where they are in the display list.

so in this example clip 2 is on top

var container : Sprite = new Sprite();
var clip1 : Sprite = new Sprite();
var clip2 : Sprite = new Sprite();

container.addChild(clip1);
container.addChild(clip2);

and in this example clip 1 is on top

var container : Sprite = new Sprite();
var clip1 : Sprite = new Sprite();
var clip2 : Sprite = new Sprite();

container.addChild(clip2);
container.addChild(clip1);

Just think of it as a big old stack of cards. Take one from the middle and put it on top and that's the one you'll see.

James Hay
A: 

Assumption: Both objects either lie on the stage together or within the same DisplayObject.

private function checkOverlap(obj1:Sprite, obj2:Sprite):void {
    //Forces obj1 to appear on top of obj2
    if (obj1.parent.getChildIndex(obj1) < obj2.parent.getChildIndex(obj2)) {
     obj1.parent.swapChildren(obj1, obj2);
    }
}
Tegeril