Need to know when the mouse leaves the stage?

Ever need to know when the cursor leaves the stage? This comes up every once in a while and is useful to know. First there is Event.MOUSE_LEAVE which is:

Dispatched by the Stage object when the mouse pointer moves out of the Flash Player window area.

According to Flash Help.

stage.addEventListener( Event.MOUSE_LEAVE, mouseLeaveListener );
 function mouseLeaveListener (e:Event):void {
         trace("mouse left the stage");
         stage.addEventListener( Event.MOUSE_MOVE, mouseMoveListener );
 }
 function mouseMoveListener (e:Event):void {
         trace("mouse re-entered the stage");
         // remove the listener until the next time the mouse exits the stage.
         stage.removeEventListener(Event.MOUSE_MOVE, mouseMoveListener);
}

Then there is also Event.ACTIVATE and Event.DEACTIVATE. These events are dispatched when Flash gains and loses focus. In other words when the cursors leaves the area of the movie.

A link to a discussion of the subject on Ationscript.orghere

XMLLoader

Here’s a simple class that wraps the URLLoader. I called this XMLLoader but it will load any text. The class itself illustrates the basic code required to load a text file. The class can be used to simplify the process in any project.

Save the following as XMLLoader.as

package {
	import flash.events.*;
	import flash.net.*;

	public class XMLLoader extends EventDispatcher {
		public var data;

		public function XMLLoader() {

		}

		public function load( _xml_url:String ):void {
			var loader = new URLLoader();
			loader.addEventListener( Event.COMPLETE, on_complete );
			loader.load( new URLRequest( _xml_url ) );
		}

		private function on_complete( e:Event ):void {
			data = e.target.data;
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}

Use the class like this. Add the following code to any project:

my_xml = new XMLLoader();
my_xml.addEventListener( Event.COMPLETE, xml_loaded );
my_xml.load( "file.xml" );