I was working through depth management in PowerSDK and discovered something useful with the enumeration within a for..in loop.
It seems that the for..in loop has a very predictable order that is quite useful.
For..in Loop order:
1. Loop through variables/references in reverse creation order
2. Loop through MCD’s in depth order
The reverse creation order seems to imply that you are looping through the raw memory structure, but at some point the processing moves to MCD’s directly (also in memory but organized differently). Not references to MCD’s but the actual MCD’s themselves.
This is especially useful in regard to depth management as one can predictably gather depth information within a MCD for all child MCD’s and avoid references. Here is a sample:
a = 1
b = []
c = {}
d = new Color()
e = _root.createEmptyMovieClip(‘e_MCD’,0)
f = _root.createEmptyMovieClip(‘f_MCD’,-20)
g = _root.createEmptyMovieClip(‘g_MCD’,20)
for(var prop in _root){
trace(prop)
}
// output:
g //reference to g_MCD … Added last, so first in loop of variable/references
f
e
d
c
b
a
$version //added at player initialization, so first in loop of variable/references
g_MCD //Actual MCD … Highest depth = 20
e_MCD //Actual MCD … Middle depth = 0
f_MCD //Actual MCD … Lowest depth = -20
Once the loop processes the $version, it switches to a depth basis and process the MCD’s. If there is a reference to a MCD it is processed in the upper portion of the loop, see g.
So if one wanted to know the child MCD’s of an MCD in depth order, you could do the following:
MovieClip.prototype.getDepths = function(){
var a = {}
for(var prop in this){
// is movieClip? && is the parent the same?
if (typeof(this[prop]) == ‘movieclip’ && this[prop]._parent == this){
//depth of current prop being added
var d = this[prop].getDepth()
// delete references from a prior to adding valid MCD
if(a[d] != undefined){delete a[d]}
// add MCD to object in memory order
a[d] = {depth:d, name:this[prop]._name, ref:this[prop]}
}
}
return a
}
It is subtle, but I am using an object and adding the values in memory order. Using an array, I would have to do a lot of leg work to filter out references as Arrays do not allow negative depths. Ultimately using an array would force me to loop more than one time or do unnecessary work, but this is disputable. If I come across duplicate reference (these are processed before MCD’s), I delete it so that the memory loop order is correct. This allows me to use an object like an array but allow for negative depths while filtering duplicates (references). What is returned is an object that contains the movieClips in order of depth. If I loop through the object, the objects will be returned in the order they were added or in this case ranked by depth.
Cheers,
ted
