TomPac
03-10-2008, 10:27 AM
hey,
Ive searched for a long time for a code that shows how to loop through all instances that are on stage and couldnt find any, so i just made one:
I've wrapped it in a class and seperated the logic into a different method but it can be in the constructor. the code doesnt look for texts but it can easily be changed
hope this will save you some trouble.
p.s Its not bug proof ;)
instancesFinder= function (){
this.symbolsVisited = new Object();
this.placeHolders=new Array();
}
instancesFinder.prototype.symbolsVisited;
instancesFinder.prototype.instancesHolder;
instancesFinder.prototype.findAllInstances = function(tTimeline,instanceFullPath) {
for (var i=0;i<tTimeline.layers.length;i++) {
var tLayer=tTimeline.layers[i];
for (var j=0;j<tLayer.frames.length;j++) {
var tFrame=tLayer.frames[j];
for (var k=0;k<tFrame.elements.length;k++) {
var tElement=tFrame.elements[k];
if (tElement.elementType=="instance") {
this.instancesHolder.push(instanceFullPath+tElemen t.name); // instances Holder
if (tElement.instanceType=="symbol") {
var tLib=tElement.libraryItem;
if (tLib.itemType=="movie clip"||tLib.itemType=="component"||tLib.itemType=="graphic") {
if (this.symbolsVisited[instanceFullPath+tLib.name]==null || this.symbolsVisited[instanceFullPath+tLib.name]!="true"){ // We havent been into that item yet
this.symbolsVisited[instanceFullPath+tLib.name] = "true"; // mark the item as true
if (tElement.name!="")
{
this.findAllInstances(tLib.timeline,instanceFullPa th+tElement.name + "."); // If this object dont have a name we dont need to add any thing to the path of the instance }
else
{
this.findAllInstances(tLib.timeline,instanceFullPa th);
}
}
}
}
}
}
}
}
}
var instancesExtractor= new instancesFinder();
var tl = fl.getDocumentDOM().getTimeline();
instancesExtractor.findAllInstances(tl,"");
instancesExtractor.instancesHolder; //<-- will now contain an array with all the instances
Ive searched for a long time for a code that shows how to loop through all instances that are on stage and couldnt find any, so i just made one:
I've wrapped it in a class and seperated the logic into a different method but it can be in the constructor. the code doesnt look for texts but it can easily be changed
hope this will save you some trouble.
p.s Its not bug proof ;)
instancesFinder= function (){
this.symbolsVisited = new Object();
this.placeHolders=new Array();
}
instancesFinder.prototype.symbolsVisited;
instancesFinder.prototype.instancesHolder;
instancesFinder.prototype.findAllInstances = function(tTimeline,instanceFullPath) {
for (var i=0;i<tTimeline.layers.length;i++) {
var tLayer=tTimeline.layers[i];
for (var j=0;j<tLayer.frames.length;j++) {
var tFrame=tLayer.frames[j];
for (var k=0;k<tFrame.elements.length;k++) {
var tElement=tFrame.elements[k];
if (tElement.elementType=="instance") {
this.instancesHolder.push(instanceFullPath+tElemen t.name); // instances Holder
if (tElement.instanceType=="symbol") {
var tLib=tElement.libraryItem;
if (tLib.itemType=="movie clip"||tLib.itemType=="component"||tLib.itemType=="graphic") {
if (this.symbolsVisited[instanceFullPath+tLib.name]==null || this.symbolsVisited[instanceFullPath+tLib.name]!="true"){ // We havent been into that item yet
this.symbolsVisited[instanceFullPath+tLib.name] = "true"; // mark the item as true
if (tElement.name!="")
{
this.findAllInstances(tLib.timeline,instanceFullPa th+tElement.name + "."); // If this object dont have a name we dont need to add any thing to the path of the instance }
else
{
this.findAllInstances(tLib.timeline,instanceFullPa th);
}
}
}
}
}
}
}
}
}
var instancesExtractor= new instancesFinder();
var tl = fl.getDocumentDOM().getTimeline();
instancesExtractor.findAllInstances(tl,"");
instancesExtractor.instancesHolder; //<-- will now contain an array with all the instances