13 Replies Latest reply on Apr 12, 2007 8:32 AM by Bart Cross

    Need a Mouse Listener

    Bart Cross Level 4
      I have a main menu with a series of buttons, when you press a button, it triggers an animation for a fly-out menu with a series of buttons to trigger other events. Everything works fine up to this point.

      What I need is an independent hit area underneath the buttons on the fly-out menu so that when the mouse leaves the area, the fly-out menu will retract. The current methods require that the mouse be down or released, but I need a mouse listener for an 'over' and 'out' state so that when the mouse is over the hit area, the fly-out menu will show and when the mouse leaves the hit area, the fly-out menu will retract.

      I've have googled and read and am now totally confused. Can anyone point me in the right direction to solve this problem?
        • 1. Need a Mouse Listener
          Greg Dove Level 4
          If I understand what you're doing correctly, then I think you need to use the movieclip.hitTest method with usage 1 (in livedocs). You would use this for the movieclip in the background of the buttons in your flyout menu. The mouse coordinates serve as the x and y co-ordinates for the parameters (from memory I think you need to use _root._xmouse and_root._ymouse). You would need to check this repeatedly after the flyout in either an onMouseMove or onEnterFrame event... If the hitTest is false then you're no longer on the flyout clip and you can tell it to retract
          • 2. Re: Need a Mouse Listener
            elearningdiva Level 1
            Did you try an "on (rollOut)"?
            • 3. Re: Need a Mouse Listener
              kglad Adobe Community Professional & MVP
              use can use an onMouseMove method to check your mouse position and determine if it's in the region where your menu should show or not.
              • 4. Re: Need a Mouse Listener
                Bart Cross Level 4
                I've tried the onMouseMove, but I also need it to see that I am no longer on the hitarea and go somewhere else. I have used a trace and it shows when I'm over the hit area, but the trace shows nothing when I move out of the hitarea.

                I've had so many variations that I can't seem to find the current one. The attached is close.
                • 5. Re: Need a Mouse Listener
                  Greg Dove Level 4
                  This works if you just have the actions on a frame and photohit clip on the main timeline.


                  • 6. Re: Need a Mouse Listener
                    Bart Cross Level 4
                    GWD: Thank you for taking the time to look at this but, what is the code to make it go to frame 1 when it is not over the hitarea? (or did I get that right?).
                    • 7. Re: Need a Mouse Listener
                      Greg Dove Level 4
                      just replace my trace action

                      trace("not over the photohit") with yours:

                      photohit.gotoAndStop(1);

                      [I'm going to sleep now where I am in the world, so others may need to follow up]
                      • 8. Re: Need a Mouse Listener
                        Bart Cross Level 4
                        GWD: Thank you for your time, solved my problem, at least in one scenario. This is a perfect case of over-thinking, your solution was so simple it would have evaded me for quite some time. I just have to unload the listener so it is not always active, but that, I think I can do.

                        Again, thank you.
                        • 9. Re: Need a Mouse Listener
                          Greg Dove Level 4
                          You're welcome. And yes it makes sense to remove the listener when the clip retracts. You would need to add the listener again everytime you needed it when photohit 'appears' again. I would aim to add the listener again whenever the mouse is over the the photohit clip each time it reappears - this means you need to wait until you roll off your main menu onto it to add the listener again or if you do add it back earlier you will have to code around the fact that the mouse is not already on the photohit clip yet in your listener.

                          And don't worry, its always the simple things that make life complicated [if that makes sense?]. I frequently over-complicate things myself and have learnt heaps of simple approaches from others here in the forums.
                          • 10. Re: Need a Mouse Listener
                            Bart Cross Level 4
                            Just working on a setInterval so that the listener will only update every ½ second. Gots to cool down the CPU load.
                            • 11. Re: Need a Mouse Listener
                              Bart Cross Level 4
                              I cannot seem to get the set interval to work, any ideas about my mistake here?
                              • 12. Re: Need a Mouse Listener
                                Greg Dove Level 4
                                you shouldn't need setInterval, and its incorrectly set up. If you wanted to use a setInterval call instead of the onMouseMove event then you could do it as follows (but its NOT good programming, just providing it for illustrative purposes)

                                remove the line Mouse.addListener... etc (you wouldn't do both an onMouseMove check and a setInterval check)

                                and put in
                                var ML = setInterval(mouseListener,"onMouseMove",500);

                                That should work... I haven't tested it. Nor do I recommend it. I think you're better off with onMouseMove.

                                If you are going to use the setInterval its better to change the name of the onMouseMove everywhere that it appears to something like onIntervalCheck just so it makes more sense to you if you look at it later on (or for someone else too). Using onMouseMove shouldn't be too demanding on the CPU - and if its noticeable it should only be for very short bursts.

                                BTW You can also delete or comment out the trace actions... they were just there to help you understand how it works. Best not to leave them in when you do a final publish (or exclude trace actions in your publish settings)
                                • 13. Re: Need a Mouse Listener
                                  Bart Cross Level 4
                                  GWD: Thank you for checking in.

                                  Thinking about it, the setInterval would create another problem with the menu system (minor).

                                  I was thinking about this only in that 'code optimization' is the charge call in most of the tuts that I have read, but being that the action is only updated when the mouse moves, the overhead is not that high. So the problem is now solved and I thank you for that.

                                  Have a good year!