MouseWheelManager Class

OVERVIEW
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);
                }
        }
}
  1. #1 by Mason - June 15th, 2006 at 10:37

    niice work!

  2. #2 by Ghi - June 15th, 2006 at 18:02

    it works! but …

    In this condition, flash player can not catch the scrollTarget precisely.

    ……

  3. #3 by Ghi - June 15th, 2006 at 18:04

    <div style=”position:absolute; left:100; top:100″>
    <object ….
    &ltparam name=”wmode” value=”transparent”/>
    </div>

  4. #4 by Kenny Bunch - June 18th, 2006 at 19:29

    Ghi can you explain what you mean?

  5. #5 by Ghi - June 25th, 2006 at 01:14

    sorry for my poor english..

    In mentioned condition above, flash player considers the left-top (0, 0) point of html-body as the left-top(0, 0) point of flahs swf movie.

    I’ve linked my test files to here..

    http://mfiles.naver.net/9b4fac76613057e4c9a1/data20/2006/6/25/82/mouseWheelScrollTarget_test-bacciamoci.zip

(will not be published)
Submit Comment
Subscribe to comments feed
  1. No trackbacks yet.