MouseWheelManager Class
Tuesday, June 13th, 2006OVERVIEW
Flash allows you to capture the events from a mouse wheel. The event fires with two params (delta:Number, scrollTarget:MovieClip). In most cases, you only want one thing to respond to this event. For example, if you have two listboxes with scrollbars, you only want the one that your mouse is over to respond to the event. You could accomplish this by having each element listen for the wheel event and do it’s own check to determine whether it should handle it or not. However, if you did this everytime the event fired, all the handler checks would have to run. In the case of having 50 elements listening, that would be 50 if checks, which is inefficient.
COMPONENT TECHNIQUE
If you dig into the Flash component classes, you will find they implement the check rather intelligently. In mx.core.ScrollView, a single listener is implemented. The handler evaluates the second param (scrollTarget) to determine which component should handle the event. It does this by starting at the MovieClip target passed in and working up the parent chain until it finds a MovieClip that is a ScrollView instance. Once it finds that instance, it makes the instance dispatch a scroll event.
SIMPLE SOLUTION
I created a MouseWheelManager class that has the same intelligence as the ScrollView, but is a lot simpler to use . To utilize it with an Class or Object, instead of making that class subclass ScrollView (doesn’t rely on additional classes), the class just has to have a onMouseWheel handler and register itself and associated MovieClip with the manager.
/** * Dreamsocket * * @author : Kenny Bunch * @example: * * import com.dreamsocket.managers.MouseWheelManager; * * var o:Object = {}; * o.onMouseWheel = function(p_delta:Number, p_startingTarget:MovieClip):Void * { * trace(”works”); * } * * MouseWheelManager.getInstance().register(foo_mc, o); */ class com.dreamsocket.managers.MouseWheelManager { private static var k_instance:MouseWheelManager; private var m_targets:Object; private function MouseWheelManager() { this.m_targets = {}; Mouse.addListener(this); } public static function getInstance():MouseWheelManager { if(MouseWheelManager.k_instance == null) { MouseWheelManager.k_instance = new MouseWheelManager(); } return MouseWheelManager.k_instance; } public function register(p_tgt:MovieClip, p_instance:Object):Void { this.m_targets[p_tgt] = p_instance; } public function unregister(p_tgt:MovieClip):Void { delete(this.m_targets[p_tgt]); } private function onMouseWheel(p_delta:Number, p_startingTarget:MovieClip) : Void { var testTarget:MovieClip = p_startingTarget; var target:MovieClip; while(testTarget != null) { if (this.m_targets[testTarget] != null) { // target exists in list and is bottom of tree set as target and stop climbing parents target = testTarget; break; } // child mc is not a target climb to parent and test testTarget = testTarget._parent; } if (target != null) { // target found dispatch event this.m_targets[testTarget].onMouseWheel(p_delta, p_startingTarget); } } }
Subscribe to RSS