21 Replies Latest reply on Dec 11, 2009 2:49 PM by kglad

    Newbie: drag n drop -> stillDown question

    B.Russell

      First of all, this is being done in AS2.

       

      OK, so I've got a bazillion years of Director experience and my brain tends to think in Lingo so forgive me if I have to describe what I need in some half-assed vernacular.

       

      I've also got 10+ years in AS but haven't run into this until today, odd as that may sound.

       

      I need to be able to drag a movie clip over a "hot spot" (target area).  When it's dragged over the hotspot BEFORE the mouse goes up, the movie clip being dragged needs to do a gotoAndStop to a named frame (the "I'm being rolled over a hot spot" animation).  Think of it as an enhanced mouse animation.

       

      I've got all the component pieces, startDrag & stopDrag, together.  Mouse down and mouse up work fine. I just need to be able to trap the "I'm over the hotspot" ***while the mouse is down and dragin', before mouseUp happens***

       

      If this were being done in Lingo, it would be something like:

       

      if the stillDown

           doSomething()

      end

       

      Since Flash is so much more powerful than Director, I'm sure it can be done in one or two lines of code, right? </snark>

       

      Sorry if this is stupid.  The Google is not much help.

        • 1. Re: Newbie: drag n drop -> stillDown question
          kglad Adobe Community Professional & MVP

          you probably have an onPress with a startDrag() in it.  just create a loop that checks for a hittest with your "hot spot":

           

          yourmovieclip.onPress=function(){

          this.startDrag();

          this.onEnterFrame=hittestCheckF;

          }

          yourmovieclip.onRelease=function(){

          this.stopDrag();

          delete this.onEnterFrame;

          }

           

          function hittestCheckF(){

          if(this.hitTest(yourhotspotmovieclip)){

          this.gotoAndStop("I'm being rolled over a hot spot");

          }

          }

          1 person found this helpful
          • 2. Re: Newbie: drag n drop -> stillDown question
            B.Russell Level 1

            Sorta kinda works but the enterFrame script isn't picking up the onRelease. 

             

            Is there any way to do a "repeat while the stillDown" (as they say in Lingo)?

             

            Thanks again.

            • 3. Re: Newbie: drag n drop -> stillDown question
              kglad Adobe Community Professional & MVP

              the onrelease is coded to terminate the onenterframe loop.

              • 4. Re: Newbie: drag n drop -> stillDown question
                B.Russell Level 1

                I understand that, but it has a "not working" problem. 

                 

                I need to restate the question, though, as there's more to it now than what I described previously.

                 

                Here's the scenario:

                 

                On the stage there is an array of items in graphical form, each with it's own named movieClip.  Each of these in turn is associated with a companion movie clip which is basically a text definition.  These companion clips are invisible at the start.

                 

                There is a hotspot on stage where these definitions appear if you drag and drop the graphic into it.  But it's more than just that.

                 

                If you drag the graphic into the hotspot it *is swapped* out for the description, and back again to the graphic if you drag it back out.  So you're dragging the description instead of the graphic while you're within the hotspot, until onRelease. If you drop on the hotspot, the description pops into place and the graphic disappears.  If you drop outside the hotspot, the graphic pops back to it's original position and the description disappears.

                 

                But wait, there's more.  Once a description has been dropped onto the hotspot, you can remove it by dragging it back out again -- and in that case, it behaves as described above (swapping out the dragged MCs based on mouse position, two different results depending on where we drop).  <<- NOTE: I haven't coded this part yet.  Still trying to get the first part down.

                 

                One further complication: When you rollover a graphic, the cursor changes to an open hand. When you press, it changes to a fist, so that's two more movie clips that have listeners attached to the mouse.

                 

                Finally - when I first prototyped it, I was having a big problem with the fact that the two swapping movie clips were different sizes, so they would "flicker" like mad back and forth whenever I got near the edge of the hotspot.  I fixed that by making a single-pixel mouse tracker MC and checking for collision of THAT with the hotspot instead of the graphic or text description, and all the other clips just follow along.

                 

                So here's how it all adds up, in pseudo-code (actual code samples are in the next section):

                 

                onPress on a graphic means --

                 

                hideMouse

                handCursor Off

                mouseTracker on via listener

                show graphic, attached to mouseTracker x/y

                fistCursorTracker on via listener

                DescriptionTracker hidden

                 

                graphic.onEnterFrame = {

                if the mousetracker.hittest(hotspot) {

                     hide graphic

                     DescriptionTracker on

                     _root.hitme = 1; // flag used in drop

                 

                     } else {

                 

                     DescriptionTracker hidden

                     show graphic, attached to mouseTracker x/y

                     _root.hitme = 0; // flag used in drop

                } }

                 

                onRelease on graphic means --

                 

                          delete graphic.onEnterFrame

                          graphic.stopDrag();

                          mousetracker_mc.stopDrag();

                          fistcursor_mc.stopDrag();

                          Description.stopDrag();

                          mouseTrackerOff();

                          handCursorOff();

                          fistCursorOff();

                          DescriptionTrackerOff;

                 

                if (_root.hitme == 1) {

                     hide graphic

                     Description x/y = final resting place

                 

                     } else {

                 

                     hide Description

                     Graphic x/y = final resting place

                 

                } }

                 

                 

                HOW IT FAILS

                 

                Everything works right right up until I drop the description into the hotspot.  It lands in the right place, but as soon as I start rolling the mouse away, the description picks right back up and continues to follow the mouse and basically behave as if the mouse were still down.  Thru using Trace, I've been able to verify that onEnterFrame is not being cleared out even though I've defined it as empty and even though I've killed all mouse tracking objects.

                 

                To summarize: The problem is related to having to track multiple movie clips attached to the mouse on Press, and these movie clips have show/hide and have different behaviors depending on where we are onscreen. The onRelease is not clearing all the behaviors like it's supposed to, even though I've reset all the objects every way I know how.

                 

                /////// CODE SAMPLES

                 

                MOUSE TRACKERS

                 

                Here's how the mouse trackers look.  They all follow the same pattern, just the names of the MCs change.

                 

                //     e.     mouseTrackerOn

                 

                            mouseTrackerOn = function () {

                                 this.attachMovie("mousetracker", "mousetracker_mc", this.getNextHighestDepth(), {_x:this._xmouse, _y:this._ymouse});

                                 var mouseListener:Object = new Object();

                                 mouseListener.onMouseMove = function() {

                                    mousetracker_mc._x = _xmouse;

                                   mousetracker_mc._y = _ymouse;

                                    updateAfterEvent();

                                 };

                            Mouse.addListener(mouseListener);

                            }

                 

                //     f.     mouseTrackerOff

                            mouseTrackerOff = function () {

                                 Mouse.removeListener(mouseListener);

                                 mousetracker_mc.removeMovieClip();

                                 var mouseListener = "";

                 

                            }

                 

                As you can see, I both remove the listener and the movie clip and init the object to "", but the objects still won't go away.

                 

                THE ON PRESS FUNCTION

                 

                // e.g., doDrag (gfx1, desc1);

                 

                doDrag = function (target, desc) {

                 

                      // hide mouse

                           hideMouse();

                 

                      //     hide hand

                          handCursorOff();

                 

                 

                      //     init mousedragger

                          mouseTrackerOn();

                 

                     //     show fist

                          fistCursorOn();       

                 

                          // drag MC around

                 

                          mousetracker_mc.startDrag(false);

                          target._x = mousetracker_mc._x;

                          target._y = mousetracker_mc._y;

                 

                          //     onEnterFrame

                 

                          // should this perhaps be mousetracker_mc.onEnterframe

                          // even though this script is triggered from target onPress?

                 

                          target.onEnterFrame = function () {

                 

                                  target._x = mousetracker_mc._x;

                                  target._y = mousetracker_mc._y;

                 

                          //     check for mousedragger/hotspot collision

                 

                          if (mousetracker_mc.hitTest(hotspot)) {

                 

                          _root.hitme = 1; // used in drop script, instantiated on init

                          hideGfx(target); // uses _visible boolean = 0

                          descTrackerOff(desc); // get rid of any previous instances

                          descTrackerOn(desc);

                          showGfx(desc); // make visible

                 

                           } else {

                 

                           _root.hitme =  0;

                          descTrackerOff(desc);

                          hideDesc(desc);

                          showGfx(target);

                          target._x = mousetracker_mc._x;

                          target._y = mousetracker_mc._y;

                 

                                    }

                            }

                 

                           // since onRelease wasn't working I tried tying it to mouse up.

                           // it does trap the event, but doesn't completely solve problem

                 

                     target.onMouseUp = function () {

                 

                         // redefine enerFrame as blank

                           target.onEnterFrame = function () {};

                 

                           // just in case

                          target.stopDrag();

                          mousetracker_mc.stopDrag();

                          handcursor_mc.stopDrag();

                          fistcursor_mc.stopDrag();

                          desc.stopDrag();

                     /// kill kill kill

                          mouseTrackerOff();

                          handCursorOff();

                          fistCursorOff();

                          descTrackerOff(desc);

                          hideDesc(desc);

                          hideGfx(target);

                 

                           // show mouse

                           showMouse();

                 

                           // on release handler

                           doCheck (target, desc);

                           }

                 

                      }

                 

                // ON RELEASE FUNCTION

                 

                     doCheck = function (target, desc) {

                 

                     // test for hotspot       

                          if (_root.hitme == 1) {

                          hideGfx(target);

                          desc._x = desc.origX; // populated on init

                          desc._y = desc.origY;

                          showDesc(desc);

                 

                           } else {              

                 

                          hideDesc(desc);

                          target._x = target.origX; // populated on init

                          target._y = target.origY;

                          showBook(target);

                 

                     }              

                }

                 

                Phew!  Glad you asked? 

                 

                My deepest thanks again.

                • 5. Re: Newbie: drag n drop -> stillDown question
                  kglad Adobe Community Professional & MVP

                  all your mouseup's will execute, no matter the mouse's location, when the left mouse button is released.

                  • 6. Re: Newbie: drag n drop -> stillDown question
                    B.Russell Level 1

                    Fair enough, but if we kill that, snip those lines of code and put them in at the very top of the doCheck function (where they originally were, example below), they still don't work, because that function never gets called... which goes back to what I originally said -- onRelease / delete onEnterFrame isn't working.

                     

                    Again, I *really* appreciate the help!

                     

                    //

                     

                    // ON RELEASE FUNCTION

                     

                         doCheck = function (target, desc) {

                               delete target.onEnterFrame();;

                          // alternately, redefine enerFrame as blank

                               target.onEnterFrame = function () {};

                     

                               // just in case

                              target.stopDrag();

                              mousetracker_mc.stopDrag();

                              handcursor_mc.stopDrag();

                              fistcursor_mc.stopDrag();

                              desc.stopDrag();

                         /// kill kill kill

                              mouseTrackerOff();

                              handCursorOff();

                              fistCursorOff();

                              descTrackerOff(desc);

                              hideDesc(desc);

                              hideGfx(target);

                     

                               // show mouse

                               showMouse();

                     

                         // test for hotspot      

                              if (_root.hitme == 1) {

                              hideGfx(target);

                              desc._x = desc.origX; // populated on init

                              desc._y = desc.origY;

                              showDesc(desc);

                     

                               } else {             

                     

                              hideDesc(desc);

                              target._x = target.origX; // populated on init

                              target._y = target.origY;

                              showBook(target);

                     

                         }             

                    }

                     

                    • 7. Re: Newbie: drag n drop -> stillDown question
                      B.Russell Level 1

                      I just verified this by putting a trace in the onRelease function for the graphic, before the doCheck handler gets called --and the onRelease never gets called, apparently because of the onEnterFrame (or the stuff that's going on inside it).  I even tried putting the delete enterframe script for that instance in the onRelease before the call to doCheck, but that doesn't work, either.

                      • 8. Re: Newbie: drag n drop -> stillDown question
                        kglad Adobe Community Professional & MVP

                        i don't see any onRelease handler.   i don't see any onPress handler.

                         

                        the code i gave works.  the code you're posting doesn't look anything like the code i posted.

                        • 9. Re: Newbie: drag n drop -> stillDown question
                          B.Russell Level 1

                          Sorry - I figured you'd be able to read between the lines on that:

                           

                          gfx1.onPress = function () {

                               doDrag (gfx1, desc1)

                          };

                           

                          gfx1.onRelease = function () {

                               doCheck (gfx1, desc1)

                          };

                          • 10. Re: Newbie: drag n drop -> stillDown question
                            B.Russell Level 1

                            Getting closer...

                             

                            It looks like it's something to do with the mouseTracker not killing itself off properly.

                             

                             

                            // e. mouseTrackerOn

                             

                              mouseTrackerOn = function () {

                              // trace ("mouseTrackerOn");

                              this.attachMovie("mousetracker", "mousetracker_mc", this.getNextHighestDepth(), {_x:this._xmouse, _y:this._ymouse});

                             

                              var mouseListener:Object = new Object();

                             

                              mouseListener.onMouseMove = function() {

                                 mousetracker_mc._x = _xmouse;

                                mousetracker_mc._y = _ymouse;

                                // trace ("mouseListenerOn: " + mouseListener);

                                 updateAfterEvent();

                              };

                              Mouse.addListener(mouseListener);

                              }

                             

                            // f. mouseTrackerOff

                              mouseTrackerOff = function () {

                              //trace ("mouseTrackerOff");

                              Mouse.removeListener(mouseListener);

                              mousetracker_mc.removeMovieClip();

                              var mouseListener = 0;

                              // trace ("mouseListenerOff: " + mouseListener);

                             

                              }

                             

                            Via Trace, I can tell the listener is getting cleared onRelease, but then it picks right back up again when I start to roll, even though there's no call to re-initialize it.

                            • 11. Re: Newbie: drag n drop -> stillDown question
                              kglad Adobe Community Professional & MVP

                              what's the following show:

                               


                               

                              gfx1.onPress = function () {

                                   doDrag (gfx1, desc1)

                              };

                               

                               

                              gfx1.onRelease = function () {

                                   doCheck (gfx1, desc1)

                              };

                               

                              doDrag = function(x,y){

                              trace(x+" "+y);

                              }

                              doCheck = function(x,y)

                              trace(x+" "+y);

                              }

                              • 12. Re: Newbie: drag n drop -> stillDown question
                                B.Russell Level 1

                                Hi:

                                 

                                I modified it slightly.

                                 

                                doDrag = function(x,y){

                                trace("drag: " + x+" "+y);

                                }

                                doCheck = function(x,y)

                                trace("check: " + x+" "+y);

                                }

                                 

                                It returns --

                                 

                                drag: _level0.centerpane.book3 _level0.centerpane.cit1

                                Never shows me the one for check (onRelease).
                                Are to assume onRelease is broken in Flash CS3 Mac? 

                                • 13. Re: Newbie: drag n drop -> stillDown question
                                  B.Russell Level 1

                                  Note: these are "books" and "citations" and it's a mix n match thing.  The fact that it's book 1 and citation3 is normal.

                                  • 14. Re: Newbie: drag n drop -> stillDown question
                                    kglad Adobe Community Professional & MVP

                                    if that doCheck() with the trace() function is not called when expected, it means you either have more than one onRelease defined for the same object or you have more than one doCheck() function.

                                    1 person found this helpful
                                    • 15. Re: Newbie: drag n drop -> stillDown question
                                      B.Russell Level 1

                                      Ok - thanks.  I will do a top to bottom audit of the code.  Of course, I just did one putting this all together, but what the hell...    There's been a few long nights at this.

                                       

                                      My humble thanks again.

                                      • 16. Re: Newbie: drag n drop -> stillDown question
                                        B.Russell Level 1

                                        Ok, rewrote that whole section, did a complete scan of the FLA and scripts folder for any duplicates (none found).

                                         

                                        Still no dice.  Not even snapping the citations into place onRelease.. because onRelease is not getting trapped. (proven via trace)

                                         

                                        I'm posting a stripped down version here.

                                         

                                        To run -

                                         

                                        Start the movie, click on SECOND menu item, then grab a book and drag it to the "Works Cited" grey area (hotspot).  Notice onRelease does not work.

                                         

                                        To see the relevant section in Flash, navigate to frame "b2" on the main timeline. Click on the Centerpane frame and select Edit in Place.  The scripts are instantiated in frame 1 of that MC. 

                                         

                                        Relevant scripts (in the scripts folder, ignore the rest):

                                         

                                        setupCitations2.as
                                        citActions2.as

                                        setupScripts.as (for a few initial _root level params).

                                         

                                        That's it!

                                         

                                        Many thanks again.

                                        • 17. Re: Newbie: drag n drop -> stillDown question
                                          kglad Adobe Community Professional & MVP

                                          i generally don't download and correct files unless i'm hired.

                                          • 18. Re: Newbie: drag n drop -> stillDown question
                                            B.Russell Level 1

                                            Fair enough.  It's just such a stupid problem I thought maybe looking at the files might help resolve it.

                                             

                                            I must say, if this were being done in Director, it would have been solved 3 days ago. 

                                             

                                            Anybody else want to take a crack at it?  Be my guest...

                                            • 19. Re: Newbie: drag n drop -> stillDown question
                                              kglad Adobe Community Professional & MVP

                                              i'm sure i could solve it if i went through you file(s).  but i'm also sure it would take more than a few minutes to find your error and that's the issue with downloading and correcting files.

                                              • 20. Re: Newbie: drag n drop -> stillDown question
                                                B.Russell Level 1

                                                Not a problem.  I greatly appreciate your time.  I'll post when I find the answer.

                                                 

                                                Many thanks again.

                                                • 21. Re: Newbie: drag n drop -> stillDown question
                                                  kglad Adobe Community Professional & MVP

                                                  you're welcome.