Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

parents.as

Name: parents - array of movieclips parents
Author: senocular: www.senocular.com
Date: 1899-12-31T00:56:37.700
Documentation:
MovieClip PARENTS: a property returning an array of a
movieClips parent clips starting with _parent then
_parent._parent for as many parents exist.

trace(myMovieClip.parents[0]); // traces myMovieClip's _parent
trace(myMovieClip.parents.length); // traces number nested clips until myMovieClip
trace(myMovieClip.parents[2] == myMovieClip._parent._parent._parent); // true


- For the Flash 5 version, instead of a property, its a function returning the array of parents.
NOTE: _name isnt a readOnly property so if _names change in a movie, be sure to recall parents
when needed instead of calling once and saving to a variable.

Example:
// (MX example)
trace(_root.instance1.instance2.parents);
// traces: _level0.instance1,_level0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Flash MX
MovieClip.prototype.addProperty("parents",
	function(){
		var p = this._parent;
		if (!p) return [];
		else{
			var a = [p];
			while(p && a.length < 500) a[a.length] = p = p._parent;
			return a;
		}
	},null
);

// Flash 5
MovieClip.prototype.parents = function(){
	var p = this._parent;
	if (!p) return [];
	else{
		var a = [p];
		while(p && a.length < 500) a[a.length] = p = p._parent;
		return a;
	}
	
}