Quantcast
Channel: Keith Hair » JSFL
Viewing all articles
Browse latest Browse all 4

Finding elements with JSFL

$
0
0

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:

  1.  
  2. fl.outputPanel.clear();
  3. var items=findObjects("mybox");
  4. var n=0;
  5. while(n < items.length){
  6. items[n].rotation=45;
  7. n++;
  8. }
  9. alert("Found "+items.length+" items.");
  10.  

  1.  
  2. /*-------------------------------------------------------------------
  3. findObjects
  4.  
  5. Returns an Array of elements in FLA document that match instanceName.
  6. If nothing matches instanceName, an Array with a length of 0 is returned.
  7. --------------------------------------------------------------------*/
  8. function findObjects (instanceName, inInstance)
  9. {
  10. var instances = [];
  11. var groups = [];
  12. var findObjectInTimeline = function (instance, name)
  13. {
  14. var timeline;
  15. if (instance.toString () == "[object SymbolInstance]" || instance.toString () == "[object Frame]")
  16. {
  17. timeline = instance.timeline;
  18. }
  19. if (instance.toString () == "[object Document]")
  20. {
  21. timeline = instance;
  22. }
  23. if (instance.toString () == "[object Timeline]")
  24. {
  25. timeline = instance;
  26. }
  27. if (instance == fl.getDocumentDOM ())
  28. {
  29. timeline = instance.getTimeline ();
  30. }
  31. if (timeline == undefined)
  32. {
  33. return null;
  34. }
  35. var checked = new Object ();
  36. var currentLayers = timeline.layers;
  37. var layObj;
  38. var frm;
  39. var found;
  40. var i = 0;
  41.  
  42. while (i < currentLayers.length)
  43. {
  44. var layObj = currentLayers [i];
  45. var frms = layObj.frames;
  46. var k = 0;
  47. while (k < frms.length)
  48. {
  49. var selArry = frms [k].elements;
  50. var n = 0;
  51. while (n < selArry.length)
  52. {
  53. var obj = selArry [n];
  54. if (obj.name == name)
  55. {
  56. found = obj;
  57. instances.push (obj);
  58. }
  59. if (obj.libraryItem != undefined)
  60. {
  61. if (obj.libraryItem.name != undefined)
  62. {
  63. if (checked [obj.libraryItem.name])
  64. {
  65. n ++;
  66. continue;
  67. }else
  68. {
  69. checked [obj.libraryItem.name] = true;
  70. }
  71. }
  72. obj = findObjectInTimeline (obj.libraryItem, name);
  73. if (obj != null)
  74. {
  75. found = obj
  76. }
  77. obj = selArry [n];
  78. if (obj.libraryItem.timeline != undefined)
  79. {
  80. obj = findObjectInTimeline (obj.libraryItem.timeline, name);
  81. if (obj != null)
  82. {
  83. found = obj
  84. }
  85. }
  86. }
  87. n ++;
  88. }
  89. k ++;
  90. }
  91. i ++;
  92. }
  93. return found;
  94. }
  95. if (inInstance == null)
  96. {
  97. inInstance = fl.getDocumentDOM ();
  98. }
  99. findObjectInTimeline (inInstance, instanceName);
  100. return instances;
  101. }
  102.  

Viewing all articles
Browse latest Browse all 4

Trending Articles