Commits

Jake Albano committed 2869d1f

Refactored XML and library loading to be event driven instead;
Library and Input now add their event listeners to the document class instead of the stage;

  • Participants
  • Parent commits 7b1d9b7

Comments (0)

Files changed (4)

File src/com/thaumaturgistgames/flakit/Engine.as

 			
 			if ((this.flags & Library.USE_EMBEDDED) && resourceClass)
 			{
-				Library.init(stage, flags);
+				Library.init(this, flags);
 				
 				new resourceClass;
 				
-				Input.init(stage);
+				Input.init(this);
 				
 				init();
 			}
 		private function load(e:Event = null):void 
 		{
 			removeEventListener(Event.ADDED_TO_STAGE, load);
-			addEventListener(Event.ENTER_FRAME, loadLibrary);
+			this.addEventListener("libraryLoaded", loaded);
 			
-			Library.init(stage, flags);
+			Library.init(this, flags);
 			
-			Input.init(stage);
+			Input.init(this);
 		}
 		
-		private function loadLibrary(e:Event):void 
+		private function loaded(event:Event):void
 		{
-			beginLoadingScreen();
-			
-			if (Library.loaded)
-			{
-				removeEventListener(Event.ENTER_FRAME, loadLibrary);
-				init();
-			}
+			removeEventListener("libraryLoaded", loaded);
+			init();
 		}
 		
 		public function init():void
 		{
-			endLoadingScreen();
-			
 			//	Entry point
 		}
-		
-		public function beginLoadingScreen():void 
-		{
-			//	Override this
-		}
-		
-		
-		public function endLoadingScreen():void 
-		{
-			//	Override this
-		}
-		
 	}
 	
 }

File src/com/thaumaturgistgames/flakit/Library.as

 package com.thaumaturgistgames.flakit
 {
 	import flash.display.Bitmap;
-	import flash.display.Stage;
 	import flash.events.Event;
 	import flash.media.Sound;
 	import XML;
 		private static var totalSounds:uint;
 		private static var loadedImages:uint;
 		private static var loadedSounds:uint;
-		private static var _loaded:Boolean = false;
 		private static var imageResources:Vector.<imageResource>;
 		private static var soundResources:Vector.<soundResource>;
 		private static var isInitialized:Boolean;
-		private static var stage:Stage;
+		private static var engine:Engine;
 		private static var loader:XMLLoader;
 		
 		public static const USE_IMAGES:int = 2;
 		
 		/**
 		 * Initialize the Library so it can be accessed
-		 * @param	s		A reference to the stage, for internal event management
+		 * @param	engine	A reference to the document class, for event tracking
 		 * @param	flags	Which components to initialize
 		 */
-		public static function init(s:Stage, flags:int):void
+		public static function init(parent:Engine, flags:int):void
 		{
-			stage = s;
+			engine = parent;
 			
 			imageResources = new Vector.<imageResource>;
 			soundResources = new Vector.<soundResource>;
 			
 			loadFlags = flags;
 			
-			stage.addEventListener(Event.ENTER_FRAME, xmlLoaded);
-			
-			loader = new XMLLoader;
+			loader = new XMLLoader(xmlLoaded);
 			
 			isInitialized = true;
 		}
 			checkInit();
 			
 			imageResources.push(new imageResource(image, name));
-			loadedImages++;
+			if (++loadedImages >= totalImages && loadedSounds >= totalSounds)
+			{
+				engine.dispatchEvent(new Event("libraryLoaded"));
+			}
 		}
 		
 		/**
 			checkInit();
 			
 			soundResources.push(new soundResource(sound, name));
-			loadedSounds++;
+			
+			if (++loadedSounds >= totalSounds && loadedImages >= totalImages)
+			{
+				engine.dispatchEvent(new Event("libraryLoaded"));
+			}
 		}
 		
 		/**
 			throw new Error("The sound \"" + name + "\" does not exist in the library.");
 		}
 		
-		/**
-		 * Whether all media has been loaded into the Library
-		 */
-		public static function get loaded():Boolean
+		private static function xmlLoaded(e:Event):void
 		{
-			checkInit();
+			if ((loadFlags & USE_IMAGES) > 0)
+			{
+				for each (var imagename:XML in loader.XMLData.images.image) 
+				{
+					new ImageLoader(imagename);
+					totalImages++;
+				}
+			}
 			
-			return _loaded;
+			if ((loadFlags & USE_AUDIO) > 0)
+			{
+				for each (var soundname:XML in loader.XMLData.sounds.sound) 
+				{
+					new SoundLoader(soundname);
+					totalSounds++;
+				}
+			}
 		}
 		
 		/**
-		 * The load status of the Library, for loading screens
+		 * Make sure Library.init() has been called already
 		 */
-		public static function get loadPercentage():Number
-		{
-			checkInit();
-			
-			var result:Number = loadedImages + loadedSounds / totalImages + totalSounds * 100;
-			return isNaN(result) ? 0 : result;
-		}
-		
-		//	Listeners
-		static private function imagesLoaded(e:Event):void 
-		{
-			_loaded = (totalImages == imageResources.length && totalSounds == soundResources.length);
-		}
-		
-		private static function xmlLoaded(e:Event):void
-		{
-			if (loader.loaded)
-			{
-				stage.removeEventListener(Event.ENTER_FRAME, xmlLoaded);
-				
-				if ((loadFlags & USE_IMAGES) > 0)
-				{
-					for each (var imagename:XML in loader.XMLData.images.image) 
-					{
-						new ImageLoader(imagename);
-						totalImages++;
-					}
-				}
-				
-				if ((loadFlags & USE_AUDIO) > 0)
-				{
-					for each (var soundname:XML in loader.XMLData.sounds.sound) 
-					{
-						new SoundLoader(soundname);
-						totalSounds++;
-					}
-				}
-				
-				stage.addEventListener(Event.ENTER_FRAME, imagesLoaded);
-			}
-		}
-		
-		//	Make sure Library.init(stage) has been called already
 		private static function checkInit():void
 		{
 			

File src/com/thaumaturgistgames/flakit/XMLLoader.as

 	
     public class XMLLoader
     {
-        private var loader:URLLoader = new URLLoader(new URLRequest("../lib/library.xml"));
+        private var loader:URLLoader;
 		
 		public var XMLData:XML;
-		public var loaded:Boolean;
+		private var listener:Function;
     
-        public function XMLLoader()
+        public function XMLLoader(listener:Function)
         {
-            loader.addEventListener(Event.ACTIVATE, onActivated);
+			this.listener = listener;
+			
+			loader = new URLLoader(new URLRequest("../lib/library.xml"));
             loader.addEventListener(Event.COMPLETE, onComplete);
-            loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
-        }
-    
-        private function onActivated(event:Event):void
-        {
-            //trace("Load of XML library initialized.");
+			loader.addEventListener("xmlLoaded", listener);
         }
     
         private function onComplete(event:Event):void
         {
             //trace("Load of XML library complete.");
-						
 			XMLData = new XML(loader.data);
-			loaded = true;
-        }
-    
-        private function onProgress(event:Event):void
-        {
-            //trace("Load of XML library progress:", loader.bytesLoaded, "out of", loader.bytesTotal, "bytes.");
+			loader.removeEventListener(Event.COMPLETE, onComplete);
+			loader.dispatchEvent(new Event("xmlLoaded"));
+			loader.removeEventListener("xmlLoaded", listener);
         }
     }
 }

File src/com/thaumaturgistgames/utils/Input.as

 package com.thaumaturgistgames.utils 
 {
-	import flash.display.Stage;
 	import flash.events.KeyboardEvent;
 	import flash.events.MouseEvent;
+	import com.thaumaturgistgames.flakit.Engine;
 	
 	/**
 	 * ...
 		private static var _mousePressed:Boolean;
 		private static var _mouseReleased:Boolean;
 		private static var _elapsed:Number;
-		private static var _stage:Stage;
+		private static var _engine:Engine;
 		
 		public function Input() {}
 		
 		/**
 		 * Called by engine
 		 */
-		public static function init(stage:Stage):void
+		public static function init(engine:Engine):void
 		{
 			_keyStates = new Vector.<Boolean>;
 			
 			
 			_keyStates.fixed = true;
 			
-			_stage = stage;
+			_engine = engine;
 			
-			_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
-			_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
-			_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
-			_stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
+			engine.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
+			engine.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
+			engine.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
+			engine.addEventListener(KeyboardEvent.KEY_UP, keyUp);
 		}
 		
 		private static function keyUp(e:KeyboardEvent):void 
 		 */
 		public static function get mouseX():Number
 		{
-			return _stage.mouseX;
+			return _engine.mouseX;
 		}
 		
 		/**
 		 */
 		public static function get mouseY():Number
 		{
-			return _stage.mouseY;
+			return _engine.mouseY;
 		}