I love the MouseUtil class.

This is a handy utility class for tracking where the mouse is when trying to create one of those non-clicking interfaces that designers just love so much.
You can find it along with a lot more at: code.google.com/p/as3-commoncode/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.as3commoncode.utils
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;	
 
	/**
	 * Utility class for determining if the mouse is currently within the bounds
	 * of any display object.
	 * 
	 * @author Tony Birleffi
	 */
	public class MouseUtil
	{
		/**
		 * Method used to determine if the mouse is within the bounds of a DisplayObject.
		 * 
		 * @example
		 * <listing>
                 * import com.target.utils.MouseUtil;
                 * var ifMouse:Boolean = MouseUtil.checkMouseInBounds(do);
                 * </listing>
                 *
		 * @param level				which sprite level.
		 * @param displayObject		any display object, sprite, movie clip, shape.
		 * @return Boolean.
		 */
		public static function checkMouseInBounds(displayObject:DisplayObject = null):Boolean
		{
			// Define.
			var res:Boolean = false;
			var rect:Rectangle;
 
			// If defined then continue.
			if(displayObject != null)
			{
				// Define the bounds rectangle.
				rect = displayObject.getBounds(displayObject);
 
				// Get the current mouse position
				var mouseX:Number = displayObject.mouseX;
				var mouseY:Number = displayObject.mouseY;
 
				// Check the x boundry, if in boundry then return true.
				if ((mouseX >= rect.left) && (mouseX <= rect.right))
				{
					// Check the y boundry.
					if ((mouseY >= rect.top) && (mouseY <= rect.bottom)) res = true;
				} 
				else res = false;
			}
 
			// Return.
			return res;
		}
	}
}

Leave a Reply

Powered by WP Hashcash