0

Scenario:

The more Y value, the more "in front" a movie clip is.

So if I have two movie clips.

Movieclip1.y is 50
Movieclip2.y is 20

Then movieclip1 should be infront of movieclip2.

If

movieclip1.y = 20
movieclip2.y = 20
movieclip3.y = 56
movieclip4.y = 921

then

movieclip4 should be most in front movieclip3 should be second most in front and the two others should be on equal depth

something like

this.z = this.y?

But I have no idea how the code is.

rene
  • 41,474
  • 78
  • 114
  • 152
  • You're thinking of z-index: http://stackoverflow.com/questions/3865426/arrange-z-order-of-objects-in-flash-with-actionscript-3 – adaam Mar 07 '14 at 23:32
  • @adaam It's not called `z-index` in as3. – Cilan Mar 07 '14 at 23:52
  • @TheWobbuffet Apologies - it's been quite a while since I programmed in AS3! – adaam Mar 07 '14 at 23:53
  • @adaam Yeah, I only code in as3 because I make apps and it's an easy code language, but I got most of the knowledge from JavaScript. – Cilan Mar 08 '14 at 00:31
  • I've re-read the question. Are you saying that the value of the y-position of the clip should affect the depth of it? – net.uk.sweet Mar 08 '14 at 00:38

2 Answers2

0

You can layer them using addChild:

this.addChild(movieclip1);
this.addChild(movieclip2); // is stacked above movieclip1;

And you can use the setChildIndex method to change the order:

this.setChildIndex(movieclip1, 1);
this.setChildIndex(movieclip2, 0); // switch the depths
net.uk.sweet
  • 12,444
  • 2
  • 24
  • 42
  • but if I want them to change the z-axis depending on where they move? so it isn't like this forever, so that sometimes movieclip 1 can go in front of movieclip 4 etc – user3123633 Mar 07 '14 at 23:34
0

You can use this snippet I made that changes the MovieClip's depth according to it's z value (pseudo code, you might have to edit this a bit):

addEventListener(Event.ENTER_FRAME, changeDepth);

function changeDepth():void
{
    var movs = [movieclip1, movieclip2];
    var movsVal = [movieclip1.z, movieclip2.z];
    var a = movsVal.indexOf(Math.max.apply(movsVal));
    if(movs.indexOf(a) === 0)
    {
        swapChildren(a, movs[movsVal.indexOf(a) + 1])
    }else
    {
        if(movs.indexOf(a) === 1)
        {
            swapChildren(a, movs[movsVal.indexOf(a) - 1])
        }
    }
    movs = [movieclip3, movieclip4];
    movsVal = [movieclip3.z, movieclip4.z];
    a = movsVal.indexOf(Math.max.apply(movsVal));
    if(movs.indexOf(a) === 2)
    {
        swapChildren(a, movs[movsVal.indexOf(a) + 1])
    }else
    {
        if(movs.indexOf(a) === 3)
        {
            swapChildren(a, movs[movsVal.indexOf(a) - 1])
        }
    }
}

Basically check the largest z (3d coordinate) and swap it with whatever's behind it.

Cilan
  • 13,101
  • 3
  • 34
  • 51