Jump to content
Search Community

Grabbing Item Depths [SOLVED]

dave test
Moderator Tag

Recommended Posts

Hi there, me again!

In a nutshell, I want to grab all of the objects in my TransformManager instance but I want to make sure I've got all the depths correct. I've tried this...


var tmObjects : Array = transformManager.targetObjects;
for (var i : int = 0; i < tmObjects.length; i++) 



However this returns odd results including not spitting them out in their correct z-order (although this isn't a biggie, I can sort the array myself). However, the results with two items on stage are often things like "2, 0" (with two items being on stage) or "0, 2". It appears that TransformManager adds an extra Sprite in their somewhere. Am I right? I absolutely love "transformManager.targetObjects" but yeah, either I'm doing something stupid or there's something missing.


Basically, I'm just not getting the depths being returned properly. :(



Link to comment
Share on other sites

You're right - TransformManager actually adds 2 Sprites to the container. One is always invisible (I call it "_dummyBox" in the class and the sprite name is "__dummyBox_mc"), and the other is the actual selection handles/box. The depth of _dummyBox doesn't matter, but the selection Sprite ( named "__selection_mc") is always brought to the very top of the display list of that container when something is selected. That's pretty important actually - otherwise your selection handles might be behind other objects.


Does that clear things up for you?

Link to comment
Share on other sites

Sort of...

I think the problem is that the array which gets returned from targetObjects doesn't update it's order when you send things back and forward in the z-order. I want to loop through this array in the order of my items z-order, not in the order they were added to the TransformManager as I think it is now.


Also, regarding your sprites, I am right in thinking that TransformManager's sprite is ALWAYS at depth 1? When I loop through targetObjects and trace out their indexes, I never get "1" displaying.


Maybe I need to do a sort of my array which contains targetObjects by each item's depth each time I grab the data...

Link to comment
Share on other sites

1) The __dummyBox_mc isn't necessarily always at depth 1. It just gets added to the top of the display list when TransformManager is instantiated. And it doesn't necessarily stay at the same depth either because if you have the forceSelectionToFront set to true, it'll adjust the depths as you select things. So if one of your items is below it, the depth will be changed when you select that item as it comes to the top of the display list.


2) The targetObjects Array isn't always re-sorted to reflect the display list z-sorting - that would degrade performance slightly and it's usually not required. If your application requires the Array to be sorted in a particular way, you'll need to do that sorting. If you have any trouble with it, let me know. It should be relatively easy to do.

Link to comment
Share on other sites

Yes, that all makes sense.

I've just written a script to sort them. I create an "unsorted" array and pushed objects containing each item in the TransformManager along with it's depth. Then I do an "sortOn" using the depth of each object in the unsorted array and voila, an array respecting the z-index of the items.


Maybe something to include in a future update of TransformManager...? I know what you mean by it being a bit sluggish if you had to do that every time but maybe a method we could optionally trigger that would just sort and update everything would be nice.

Link to comment
Share on other sites

  • 7 months later...


sorry to reopen this post, but i've tried to write a function like you said, i mean to sort an array based on depth's object by i don't have success.

could you please post an example or give a hint?

thanks a lot.

Link to comment
Share on other sites

Try this. This is really old code and I don't know if it's depending on other things but should give you the general idea. Basically, you want to create an array which you can sort but to do so, you have to have a value that you can sort "by". So I have an array of objects; each object contains a reference to an item in my transform manager and also it's depth. This way, I can just sort on the depths...


var tmObjectsUnsorted : Array = transformManager.targetObjects;

var tmObjectsUnsorted_objects : Array = new Array();

for (var j : Number = 0; j < tmObjectsUnsorted.length; j++) {

var thisObject : Object = new Object();

thisObject['itemDepth'] = itemsContainer.getChildIndex(tmObjectsUnsorted[j]);

thisObject['item'] = tmObjectsUnsorted[j];




var tmObjectsSorted : Array = tmObjectsUnsorted_objects.sortOn('itemDepth');

Link to comment
Share on other sites

thanks dave

yes i think is a valid idea

that's what i wrote this afternoon (i live in italy, different time) while... waiting :)

i didn't think to declare a new Object but suppose is the same

var clips_array = manager.targetObjects;
var newArray:Array = new Array ()
	for (var i : int = 0; i < clips_array.length; i++) {
		newArray.push({depth:container.getChildIndex(clips_array[i]), item:clips_array[i]});
var mc:MovieClip;
for (var j = 0; j < clips_array.length; j++) {
	mc = newArray[j].item;
	// ... etc etc

what i don't like is the twice for, but didn't find a better way, i need to save the position of the objects and reload with corretc info,

anyway, thanks again, really appreciate your help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...