Here's a JSFL function for returning an Array of all elements with a given instance name in whole Flash Document.
I was trying to make something like JavaScript's "getElementsById()" method.
My intent was to make this compatible with both Flash 8 and CS3 IDEs.

Note: Unfortunately, this method will not find elements that are grouped.
Let me know if this is helpful or if there is a better way to do this.

Example:

 
fl.outputPanel.clear();
var items=findObjects("mybox");
var n=0;
while(n < items.length){
	items[n].rotation=45;
	n++;
}
alert("Found "+items.length+" items.");
 

 
/*-------------------------------------------------------------------
findObjects
 
Returns an Array of elements in FLA document that match instanceName.
If nothing matches instanceName, an Array with a length of 0 is returned.
--------------------------------------------------------------------*/
function findObjects (instanceName, inInstance)
{
	var instances = [];
	var groups = [];
	var findObjectInTimeline = function (instance, name)
	{
		var timeline;
		if (instance.toString () == "[object SymbolInstance]" || instance.toString () == "[object Frame]")
		{
			timeline = instance.timeline;
		}
		if (instance.toString () == "[object Document]")
		{
			timeline = instance;
		}
		if (instance.toString () == "[object Timeline]")
		{
			timeline = instance;
		}
		if (instance == fl.getDocumentDOM ())
		{
			timeline = instance.getTimeline ();
		}
		if (timeline == undefined)
		{
			return null;
		}
		var checked = new Object ();
		var currentLayers = timeline.layers;
		var layObj;
		var frm;
		var found;
		var i = 0;
 
		while (i < currentLayers.length)
		{
			var layObj = currentLayers [i];
			var frms = layObj.frames;
			var k = 0;
			while (k < frms.length)
			{
				var selArry = frms [k].elements;
				var n = 0;
				while (n < selArry.length)
				{
					var obj = selArry [n];
					if (obj.name == name)
					{
						found = obj;
						instances.push (obj);
					}
					if (obj.libraryItem != undefined)
					{
						if (obj.libraryItem.name != undefined)
						{
							if (checked [obj.libraryItem.name])
							{
								n ++;
								continue;
							}else
							{
								checked [obj.libraryItem.name] = true;
							}
						}
						obj = findObjectInTimeline (obj.libraryItem, name);
						if (obj != null)
						{
							found = obj
						}
						obj = selArry [n];
						if (obj.libraryItem.timeline != undefined)
						{
							obj = findObjectInTimeline (obj.libraryItem.timeline, name);
							if (obj != null)
							{
								found = obj
							}
						}
					}
					n ++;
				}
				k ++;
			}
			i ++;
		}
		return found;
	}
	if (inInstance == null)
	{
		inInstance = fl.getDocumentDOM ();
	}
	findObjectInTimeline (inInstance, instanceName);
	return instances;
}
 
Vote in HexoSearch
One Response to “Finding elements with JSFL”
  1. [...] is a modification of the “findObjects” I posted earlier. This is handy if you are the type of person that names your symbol items with [...]

Leave a Comment

Thanks for visiting www.keith-hair.net