9 Replies Latest reply on Nov 17, 2009 5:07 PM by kglad

    drag and drop with attached movie clips

    Shad0wdrag0n Level 1

      I have a button that has an onPress function that attaches a movieclip with startDrag(true) so the attached movie snaps to the mouse and can be dragged around. The problem is that I don't know how to set up the onRelease function so that when the mouse button is lifted the attached movieclip is removed. Also, the attached movie appears in the upper left corner for just an instant before snapping to the mouse. What's the best way to set up something like this? Is there a better way than the way I'm trying to do it?

        • 1. Re: drag and drop with attached movie clips
          kglad Adobe Community Professional & MVP

          1.  you can use removeMovieClip() to remove your attached movieclip (in your onRelease handler).

           

          2.  you can assign assign your attached movieclip's _visible property to false after it's created and reassign to true after it's positioned correctly.

          • 2. Re: drag and drop with attached movie clips
            Shad0wdrag0n Level 1

            I tried adding this to the timeline of the attached movie:

             

            this.onRelease = this.onReleaseOutside = function(){
                this.removeMovieClip();
            }

             

            but it only sort of works. When the mouse button is released the movieclip stays stuck to the mouse. When the button is clicked again and released it will remove the attached movieclip. How do I make it so the movieclip is removed when the button is first released instead of on the second release?

            • 3. Re: drag and drop with attached movie clips
              kglad Adobe Community Professional & MVP

              you must have conflicting code elsewhere.  in addition, it's best to keep all code on the same timeline (if you're going to add code to a timeline):

               

              mc.onPress=function(){

              this.startDrag();

              }

              mc.onRelease=mc.onReleaseOutside=function(){

              this.removeMovieClip();  // assuming mc is at a removable depth.

              }

              • 4. Re: drag and drop with attached movie clips
                Shad0wdrag0n Level 1

                I tried putting all the code on the same timeline but it didn't work. On the attached movieclip's timeline the startDrag part doesn't work, and on the main timeline the removeMovieClip part doesn't work (even after multiple clicks). I tried removing all the other code and it still wont remove the movieclip on the first click, so I have no idea where the conflict could be.

                • 5. Re: drag and drop with attached movie clips
                  kglad Adobe Community Professional & MVP

                  if removeMovieClip() doesn't work, then your movieclip is not at a removable depth.  use:

                   

                  mc.onPress=function(){

                  this.startDrag();

                  }

                  mc.onRelease=mc.onReleaseOutside=function(){

                  this.swapDepths(this._parent.getNextHighestDepth());

                  this.removeMovieClip();  // assuming mc is at a removable depth.

                  }

                  • 6. Re: drag and drop with attached movie clips
                    Shad0wdrag0n Level 1

                    I think the problem is that the movieclip I want to drag is an attached moviclip that doesn't exist until a button is clicked on.

                     

                    This is the code I'm using on the main timeline where the button is:

                     

                    button.onPress = function(){
                        attachMovie("item1", "item1", this.getNextHighestDepth(),{_x:-200, _y:0});
                        item1.startDrag(true);
                    }

                     

                    And this is the code I put onto the timeline of the "item1" movieclip:

                     

                    this.onRelease=this.onReleaseOutside=function(){
                        this.removeMovieClip();
                    }

                     

                    When you click on the button movieclip it attaches item1, which snaps to the mouse cursor. However, when the mouse button is released item1 stays stuck to the cursor. If you click a second time anywhere on the stage and release, item1 will be removed. Everything works, except I want the item1 movieclip to be removed when the mouse button is released the first time. I don't want to have to click a second time to get rid of it.

                    • 7. Re: drag and drop with attached movie clips
                      kglad Adobe Community Professional & MVP

                      on one timeline:

                       

                      button.onPress = function(){
                          attachMovie("item1", "item1", this.getNextHighestDepth(),{_x:-200, _y:0});
                          item1.startDrag(true);
                      }

                      button.onRelease=button.onRelaseOutside=function(){

                      item1.removeMovieClip();

                      }

                      • 8. Re: drag and drop with attached movie clips
                        Shad0wdrag0n Level 1

                        Oh, so the onRelease goes on the button and not on item1. OK, I just tried it out and it works perfectly. Thank you.

                        • 9. Re: drag and drop with attached movie clips
                          kglad Adobe Community Professional & MVP

                          you're welcome.