Actionscript Document Class XML Load Template


/ Published in: ActionScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import mx.utils.Delegate;
  2. dynamic class classes.MainController extends MovieClip
  3. {
  4. private var __xml:XML;
  5. public var xmlPath:String = "xml/settings.xml";
  6.  
  7. private var __tiles:Array = new Array();
  8.  
  9. /** -----------------------------------------------------------
  10.   * Constructor
  11. * -----------------------------------------------------------
  12. */
  13. public function MainController(){
  14. trace("Document Class loaded");
  15. };
  16.  
  17. public static function main(target:MovieClip):Void {
  18. target.__proto__ = MainController.prototype;
  19. Function(MainController).apply(target, null);
  20. };
  21.  
  22. private function onLoad():Void{
  23.  
  24. loadSettings();
  25. };
  26.  
  27. /** -----------------------------------------------------------
  28. * Properties
  29. * -----------------------------------------------------------
  30. */
  31.  
  32. /** -----------------------------------------------------------
  33. * Public Methods
  34. * -----------------------------------------------------------
  35. */
  36.  
  37.  
  38. /** -----------------------------------------------------------
  39. * Private Methods
  40. * -----------------------------------------------------------
  41. */
  42. private function loadSettings():Void{
  43.  
  44. __xml = new XML();
  45. __xml.ignoreWhite = true;
  46. __xml.onLoad = Delegate.create(this, onXMLLoad);
  47. __xml.load(xmlPath);
  48. };
  49.  
  50. private function onXMLLoad(success:Boolean):Void{
  51. if (success){
  52. parseXML();
  53. }
  54. };
  55.  
  56. private function parseXML():Void{
  57. __tiles = filterXMLAssocArray("tile", __xml.firstChild.childNodes);
  58. };
  59.  
  60.  
  61.  
  62.  
  63. // Filters specified tags from a supplied XML Node resource by tag name.
  64. // Returns a new associative array with the filtered tags' attributes.
  65. private function filterXMLAssocArray(filterNodeName:String, xmlNodes:Array):Array{
  66.  
  67. var tmpArray:Array = new Array();
  68.  
  69. for (var i:Number = 0; i<xmlNodes.length; i++){
  70. if (xmlNodes[i].nodeName == filterNodeName){
  71. tmpArray[i] = new Array();
  72. tmpArray[i] = xmlNodes[i].attributes;
  73. }
  74. }
  75. return tmpArray;
  76. };
  77.  
  78. private function print_r( obj, indent ) {
  79.  
  80. if (indent == null) indent = "";
  81. var out = "";
  82. for ( item in obj ) {
  83. if (typeof( obj[item] ) == "object" )
  84. out += indent+"[" + item + "] => Object\n";
  85. else
  86. out += indent+"[" + item + "] => " + obj[item]+"\n";
  87. out += print_r( obj[item], indent+" " );
  88. }
  89. return out;
  90. }
  91. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.