8 Replies Latest reply on Jul 15, 2009 2:50 PM by Ned Murphy

    Problem with event handling (draggin windows)

    fenosc Level 1

      Hi there,


      In my site I want to do some movieclips dragging (like the same as Windows window objects). But I`m having some trouble making the events working correctly.


      Here is my site code design:

      Main class has a WindowsManager class instance, which has a WindowBuilder instance.

      WindowBuilder creates a window movieclip and returns it to the WindowManager so it can add it as its child.


      What I want: I want to drag the whole window when I click anywhere on it (after I am able to do this, I will want to make it draggable only on the top area of the window).


      The problem: It starts dragging the parts from the window (if a click on the shadow, drags the shadow, and not the whole movieclip).


      I know that is a eventpropagation issue, but I don`t know how to control it.

      Can anyone please tell me ?


      Thanks in advance,

      Sorry for the trouble,


        • 1. Re: Problem with event handling (draggin windows)
          fenosc Level 1

          I edited the topic to make the problem easier to understand.

          Thanks in advance

          • 2. Re: Problem with event handling (draggin windows)
            Ned Murphy Adobe Community Professional & MVP

            It sounds like it may be a case of you using event.target when you should be using event.currentTarget, though there is no way of telling what you might be doing wrong without you sharing the code that is not functioning properly.

            • 3. Re: Problem with event handling (draggin windows)
              fenosc Level 1

              Hi Ned,


              About the currentTarget thing, I don`t know the difference between them, but I did try both. Didn`t work. I really don`t know what is happening.


              I don`t know what do you think its better, to post the code here or attach the files. So I attached the files, sorry if you prefered the code here.

              3 small classes and the FLA so you can see the window.


              Can you please tell me whats wrong ?

              I hope there is a way to stop event propagation, that can be really anoying sometimes.

              Also, in my window I have dynamic text fields so I can change their contents. The problem here is that startDrag and stopDrag doesn`t work for them.

              I hope that fixing my problem will fix this too. (I don`t need to drag the text field, just the movieclip that contains it.)


              Thanks in advance,


              • 4. Re: Problem with event handling (draggin windows)
                fenosc Level 1

                hi, just upping he topic, and also Ill post the code here for the sake of easyness


                Document class: Main

                        var manager:WindowsManager;
                         public function Main()
                             manager = new WindowsManager();
                             manager.newWindow("Experiences", 200, 200);
                             manager.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag, false, 0, true);
                             manager.addEventListener(MouseEvent.MOUSE_UP, onStopDrag, false, 0, true);


                         private function onStartDrag(evt:MouseEvent):void {
                         private function onStopDrag(evt:MouseEvent):void {


                WindowsManager class

                        private var _windowBuilder:WindowBuilder;
                        public function WindowsManager()
                            _windowBuilder = new WindowBuilder(this);
                        public function newWindow(AsName:String, AposX:Number, AposY:Number):void {
                            addChild(_windowBuilder.buildWindow(AsName, AposX, AposY));
                        public function removeWindow(AsName:String):void {


                WindowBuilder class

                        private var _mcManager:WindowsManager;
                        public function WindowBuilder(AWindowsManager:WindowsManager)
                            _mcManager = AWindowsManager;
                        public function buildWindow(AsName:String, AposX:Number, AposY:Number):janela_mc {

                            //Building the window using the object janela_mc in the library as template

                          // I dont think this part has any problem, is just the drawing...

                            var _mcWindow:janela_mc = new janela_mc();
                            _mcWindow.x = AposX;
                            _mcWindow.y = AposY;
                            _mcWindow.name = AsName;
                            _mcWindow.dynTitulo.text = AsName;
                            _mcWindow.dynTitulo.mouseEnabled = false;
                            _mcWindow.dynTitulo.selectable = false;
                            _mcWindow.dynTitulo.autoSize = TextFieldAutoSize.LEFT;

                            _mcWindow.mcArrastador.x = _mcWindow.mcCorpo.width - 1 ;
                            _mcWindow.mcArrastador.width = _mcWindow.mcCorpo.width - _mcWindow.dynTitulo.width;
                            return _mcWindow;




                • 5. Re: Problem with event handling (draggin windows)
                  Ned Murphy Adobe Community Professional & MVP

                  Just so you know, I ask for code to be shown mainly because 1) many people are using CS4, which I do not have, and 2) most zip files that get attached to the forum I cannot unzip... it is as if the forum corrupts them.  I originally thought it was due to people creating them with Mac's, but I gave up chasing that... it's easier to ask for code and ignore the post if none shows up.


                  I'll look thru what you posted, but warn you that I am class challenged.

                  • 6. Re: Problem with event handling (draggin windows)
                    Ned Murphy Adobe Community Professional & MVP

                    It's hard to put a finger one what is going on. partly because what you're creating gets lost between my lack of class knowledge and not knowing what is involved with a janela_mc.


                    But I do see that you are using evt.target rather than evt.currentTarget, so maybe I can explain a little about the difference.  The evt.target can refer to whatever subobject the mouse happens to be over in an object that has the event listener assigned to it.  However, the evt.currentTarget can only refer to the object that has the listener assigned to it.  This difference is only evident when yo get involved with the "MOUSE_..." events, and not the "ROLL_..." events.


                    So in your case, if the manager is assigned the event listener, but the event handler function is using the evt.target, it can be targeting any object within the manager as the startDrag item.  I thought that replacing the evt.target with evt.currentTarget would alleviate that, but if you say it didn't help, then I'd have to gain a better understanding of the players on the field.

                    • 7. Re: Problem with event handling (draggin windows)
                      fenosc Level 1

                      Hi Ned,

                      Thanks for the warning, I`ll remind to post the code then.

                      I thought that posting codes was a hassle since you need time to udnerstand it and all. I thought that maybe there could be a problem with my logic, then there is no need for code.


                      Well, letting me know the difference between target and current target really helps me out!

                      Now I understand why the currentTarget wasn`t working.


                      Actually, when I use currentTarget and I have on window, it moves fine! It works! The problem is with 2 windows or more in the WindowsManager class. In this situation it moves all the windows! That`s why it I said it didn`t work.


                      With this in mind I know that I have to put the drag event IN the WindowsManager class (which makes much more sense) but I wasn`t able to complete earlier.


                      I`ve just changed my code adding the events to the window in the WindowManager class just before adding it as a child.

                      It works!

                      Thanks for the help Ned!.



                      • 8. Re: Problem with event handling (draggin windows)
                        Ned Murphy Adobe Community Professional & MVP

                        You're welcome Oscar