11 Replies Latest reply on Jul 12, 2006 4:09 PM by EvilDog2000

    Mouse Stop

    Walloompoom
      Hi, I have a custom cursor that needs to know when the mouse stops but there is no onMouseStop. Is there a way to do it with the mouse listener?

      Thanks! James

        • 1. Re: Mouse Stop
          EvilDog2000
          Hey,

          You can check the position of the mouse everyframe with a setInterval or onEnterFrame and keep that position in some variables, when both variables don't change, the mouse has stopped.
          • 2. Re: Mouse Stop
            Walloompoom Level 1
            How would you write the "Dont change" part?
            • 3. Mouse Stop
              fikshin Level 1
              Yeah do what EvilDog said. in code form it might look something like below. I added a range to the detection if you like, AND you might want to call the detect function with an onEnterFrame or or setInterval like evildog said instead of the onMouseMove because its not frequent enough to give an accurate response.

              hope that helps

              • 4. Re: Mouse Stop
                EvilDog2000 Level 1
                The whole thing would be something like that. Makes sense?

                // Variables that keep the mouse pos and if it was stopped already to avoid doing OnMouseStop all the time
                // when the mouse doesn't move.
                var xMousePos:Number = 0;
                var yMousePos:Number = 0;
                var bMouseIsStopped:Boolean = true;

                function OnMouseStop()
                {
                // Your code here for when the mouse stops.
                }

                function onEnterFrame()
                {
                // Get the new mouse position.
                var xNewMousePos:Number = _xmouse;
                var yNewMousePos:Number = _ymouse;

                // Check if the mouse has stopped if it wasn't stopped already.
                if(!bMouseIsStopped && xNewMousePos == xMousePos && yNewMousePos == yMousePos)
                {
                bMouseIsStopped = true;
                OnMouseStop();
                }

                // Keep the new mouse position.
                xMousePos = _xNewMousePos;
                yMousePos = _yNewMousePos;
                }

                mouseListener.onMouseMove = function() {
                crosshair_mc._x = _xmouse;
                crosshair_mc._y = _ymouse;
                _root.mainMc.crosshair_mc.butterFlyMc.gotoAndStop("flap");

                // The mouse isn't stopped anymore since it moved.
                bMouseIsStopped = false;
                };
                • 5. Re: Mouse Stop
                  EvilDog2000 Level 1
                  Oops sorry for the indentation, I'll use "attach code" next time
                  • 6. Re: Mouse Stop
                    Walloompoom Level 1
                    Hi. I tried it but the cursor doesnt react at all. Here is the full code


                    • 7. Re: Mouse Stop
                      EvilDog2000 Level 1
                      Ok many things could go wrong
                      where is that code exactly? on the main timeline?

                      make sure onEnterFrame is called everyframe by tracing _xmouse and _ymouse inside the function and making sure the mouse position is correct.
                      Also do this trace:
                      trace(!bMouseIsStopped && xNewMousePos == xMousePos && yNewMousePos == yMousePos);
                      you'll see a true pass by when the mouse stops if it's working well.

                      if it'S working well put a trace("OnMouseStop"); in the function OnMouseStop

                      Also trace (); probably won'T do much, use trace("InsideStopCondition"); or something like that to make the trace significant.

                      Basicly work your way through the code with traces and see where it stops working, trace the values of the variables you're using at different places to see where it stops being right.
                      • 8. Re: Mouse Stop
                        Walloompoom Level 1
                        Hi, when I trace(!bMouseIsStopped && xNewMousePos == xMousePos && yNewMousePos == yMousePos); it comes up false reguardless of mouse
                        • 9. Re: Mouse Stop
                          EvilDog2000 Level 1
                          Did you put the trace right before the if condition?
                          if you did then trace everything independently until you find what's wrong

                          trace("Mouse stopped: " + bMouseIsStopped); // Should be false when the mouse moves, true when it doesnt.
                          trace("New X Pos: " + xNewMousePos); // Should be the mouse position in X.
                          trace("New Y Pos: " + yNewMousePos); // Should be the mouse position in Y.
                          trace("Old X Pos: " + xMousePos); // Should be the previous mouse x position from the last frame.
                          trace("Old Y Pos: " + yMousePos); // Should be the previous mouse y position from the last frame.

                          it's called debugging my friend :)
                          • 10. Re: Mouse Stop
                            Walloompoom Level 1
                            trace("Mouse stopped: " + bMouseIsStopped); returns true ONLY when you havent put the mouse on stage.
                            trace("Old Y Pos: " + yMousePos); and x are always undefined.
                            • 11. Re: Mouse Stop
                              EvilDog2000 Level 1
                              // Keep the new mouse position.
                              xMousePos = _xNewMousePos;
                              yMousePos = _yNewMousePos;

                              it's xNewMousePos not _xNewMousePos
                              same for y