Object Depth [AS3]

10 posts

Flag Post

I’m sure there is a simple way to do this but I can’t figure it out.

I have a loop that adds several movieclips at a random x and y position. How can I make it so that after they are all added the movieclips depth will sort so that the higher the y the higher the depth of the object so that the ones at 0y for instance are behind the ones at 10y and the ones at 70y are behind the ones at 100 etc etc?

 
Flag Post

Depth in AS3 depends on the index of the child relative to the parent. (aka, order that you addChild() em, with the most recent on top).

So you can just sort the objects based on their y values and then swap their indices.

 
Flag Post

Yah so how do I go about that?

 
Flag Post
Originally posted by evan999333:

Yah so how do I go about that?

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee5.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7fa4

 
Flag Post

I know how arrays work NineFiveThree… I do not however understand how to swap objects depths based off their position in an array.

 
Flag Post
Originally posted by evan999333:

I know how arrays work NineFiveThree… I do not however understand how to swap objects depths based off their position in an array.

re-addChild them with addChildAt.

Read the manual.

 
Flag Post

Or the As3 manual, i don’t think the book qwerber suggested really helps in programming.

 
Flag Post

setChildIndex is relevant too and probably makes more sense then using addChildAt

 
Flag Post

If you break the problem down into pieces, you’ll see it’s not so daunting.
Here’s some pseudo-code on how you may want to start approaching the problem:

for each child of the current object (hint: numChildren)
     add the child to an array (hint: getChildAt)

sort the objects in the array based on their "y" property (hint: read up on sorting methods)
(alternate hint: check the manual page NineFiveThree posted and look up Array's built-in sort method)

for each object in the array
     set the index of the object to its new array position (hint: setChildIndex)

Those 3 steps should be nothing scary individually, and the overarching problem will just be a simple matter of connecting it all together.

 
Flag Post

Thanks BobTheCoolGuy and Khronosis that’s just what I needed – I knew it would be something simple.

What I did is

myArray.sortOn("thisObjectY", Array.NUMERIC);
for (var i=0; i<levelChunkVar.treeArray.length; i++)
			{
                                setChildIndex(myArray[i], i);
                        }