8 Replies Latest reply on Jun 10, 2007 7:30 PM by kglad

    AS3 MOUSE_UP Event

    Rothrock Level 5
      I'm working through Keith Peters excellent book "Making Things Move!" and I'm learning a lot about AS3. But I'm at a bit of a loss with MOUSE_UP and the loss of the onReleaseOutside Handler. (This isn't something he is actually talking about, it is a side question that keeps popping up in my head.)

      So I've got a custom sprite on the stage, just a round red "ball." I'm doing an addEventListener on that ball for the MOUSE_DOWN and calling my grabBall function. The grabBall function starts dragging the ball instance and adds an event listener to the stage for the MOUSE_UP event and when it is heard will call my releaseBall function. All well and good to this point.

      Inside the releaseBall function I want to stopDragging the ball – and eventually may wish to do other things. Now in the samples in the book this is how he does it:

      ball.stopDrag();

      But what if I want to use the grabBall function for several different instances of ball that could all be different? Then I could just use:

      stopDrag();

      Since there can be only one startDrag() at a time that works and will stop whatever is dragging. Doesn't seem good form to me, but it does work. But suppose I wanted to make the item that was being dragged have an alpha of .5? How do I get that item?

      If I only have one thing it could be, then I would use:

      ball.alpha=.5;

      But if I have multiple items there doesn't seem to be a way reliably get the item being released. At least with the MOUSE_UP listener being attached to the stage. If I release with the pointer on the ball, I can capture that with:

      event.target.alpha=.5;

      But if I drag outside the stage area or the ball is restricted by other reasons, then when I release event.target gives the stage. And event.currentTarget always gives stage.

      I'm pretty sure that there is a reason for listening for the MOUSE_UP on the stage. If I tried to attach that to the Sprites it would never get called in the situations that I'm talking about where the pointer is not over the Sprite.

      Since event.target doesn't give consistent results, am I left with having to make a variable/property for myself that holds the name of the currentDrag item? Or am I just missing something here?

      Also, what should the type of currentDrag be? I'm totally confused by this. According to the documentation event.target is of type InteractiveObject, but I can't declare a variable to be of this type because the compiler gives a "1046: Type was not found or was not a compile-time constant: InteractiveObject.
        • 1. Re: AS3 MOUSE_UP Event
          kglad Adobe Community Professional & MVP
          if you add MOUSE_UP listeners to the balls that would allow you to change a property of the correct currentTarget except when you miss a MOUSE_UP event because of player performance issues or the mouse being dragged off-stage.

          in those situations, you need a work-around. i would use a variable to store a reference to the currently dragging ball and use a stage MOUSE_UP and the stage focus property to determine if the ball is no longer being dragged.
          • 2. Re: AS3 MOUSE_UP Event
            SymTsb Level 2
            My suggestion is to read the AS3 Cookbook. It talks a great deal about dragging things across the stage as well as showing you how to use a custom "cooked" up class/package to the startDrag and stopDrag they wrote and is available for download from the book's helper site.
            • 3. Re: AS3 MOUSE_UP Event
              Rothrock Level 5
              Thanks for the ideas. Any suggestion on what the type of the variable should be?
              • 4. Re: AS3 MOUSE_UP Event
                kglad Adobe Community Professional & MVP
                use the same type as the object type that you are dragging. interestingly, the mouse exiting the stage is not detected while the mouse is down. only when it's up is it detected:

                • 5. Re: AS3 MOUSE_UP Event
                  Rothrock Level 5
                  kglad, did you only test this in the testing environment or did you try either just the swf or in an html page? I've found that MOUSE_LEAVE doesn't work at all for me in the testing environment.

                  I am having a strange experience with your code. If I drag a circle and move the mouse of the edge of my swf and then let go the dot jumps to the mouse position – off the stage.

                  What is causing that? I find it strange because with my earlier test that didn't happen. And I don't see anything that would make that happen.
                  • 6. Re: AS3 MOUSE_UP Event
                    kglad Adobe Community Professional & MVP
                    i tested in the test environment where, as i mentioned above, the MOUSE_LEAVE event is only detected when the mouse is up.

                    so, if you drag to the stage edge and beyond with the mouse down, not only is the MOUSE_LEAVE not detected, but when you finally release the mouse button, the MOUSE_LEAVE is detected, the stopDrag() executes and the object that had been dragged to the stage edge is suddenly off-stage under the mouse!

                    i have no explanation.
                    • 7. Re: AS3 MOUSE_UP Event
                      Rothrock Level 5
                      Hmm. Strange. In the testing environment I can't even get the MOUSE_LEAVE event to happen at all. I have to test it separately. Well evidently there is a lot to figure out with the new event model!

                      Thanks for all your help.
                      • 8. Re: AS3 MOUSE_UP Event
                        kglad Adobe Community Professional & MVP
                        in the test environment, if i open the swf in a new window (not tabbed) that's not full-screen i see the same MOUSE_LEAVE being detected (once the mouse is up).