8 Replies Latest reply on Mar 21, 2015 3:21 AM by kglad

    How to handle mouse events in Flash? What exactly does 'this' refer to?

    User_0 Level 1

      Hi, I'm having problems with Mouse Events (working in HTML5 Canvas) I'm hoping somebody here can explain a few things to me.

       

      First of all, can someone explain what exactly 'this' refers to? From what I've read, it seems that 'this' refers to the current timeline that you are in, when typing the code?

       

      -------

       

      Anyways, my specific problem, is I have a few nested movie clips that I want to control (start, stop) with Mouse Rollover Events, and using the below code, the parent layer seems to work, but I can't find a way to get a 'child' movieClip to use this code (and work). There's not any tutorials on this that I can find on this subject, so any help would be very much appreciated.



      this.stop();

       

      var frequency=3;

      stage.enableMouseOver(frequency);

      this.on("rollover",fl_MouseOverHandler_32);

       

       

      function fl_MouseOverHandler_32()

      {

          this.play();

        stage.enableMouseOver(0);

      }

        • 1. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
          kglad Adobe Community Professional & MVP

          'this' refers to the current object, and the current object varies depending on the context where 'this' is used.

           

          but when attached to a timeline outside a named function body, 'this' refers to the timeline.  inside a named function body, 'this' loses context (unless you bind it in the function call).

           

          anyway, i find it easiest to use:

           

          var tl=this;

           

          at line 1 of a frame and then use tl wherever i want, including inside named functions.

           

          if there's a movieclip on stage named p that has a child c, you can use:

           

          this.p.c.addEventListener('rollover',f);

          function f(e){

          //don't use this in here.

          }

           

          or i would use:

           

          var tl=this;

          tl.p.c.addEventListener('rollover',f);

          function f(e){

          you can use tl here

          }

           

          or you can use:

           

          this.p.c.addEventListener('rollover',f.bind(this));

          function f(e){

          //you can use this here

          }

          • 2. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
            User_0 Level 1

            Thanks a lot for your very helpful response.

             

            Can I ask you to clarify/expand on your thoughts a bit?

             

            var tl=this;

             

            at line 1 of a frame and then use tl wherever i want, including inside named functions.

            Do you mean, that by adding this to a frame in a timeline animation, the current timeline that is 'open' will always be able to be referenced with 'tl'? How would I reference 'tl' in a child movieclip? If I'm in a movieclip within a movieclip, if I use 'tl.play();' in a piece of code will that still reference the previous timeline?

            • 3. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
              User_0 Level 1

              To be more specific, and less abstract, (sorry, I get really confused with some of this stuff) can you please tell me why the following isn't working?

               

              What I want to do, is to be able to

              • rollover the stage on a timeline,
              • have this timeline play,
              • have that rollover disabled after it's played.

               

                             var tl=this;

                             tl.stop();

               

               

                             var frequency=3;

                             stage.enableMouseOver(frequency);

                             tl.on("rollover",fl_MouseOverHandler_32);

               

               

                             function fl_MouseOverHandler_32()

                                  {

                                      tl.play();

                                      stage.enableMouseOver(0);

                                  }

               

              ....................................................

               

               

              Then, in a child movieclip (which has been playing after the last rollover event)

              • stop at a frame
              • when a hotspot (clickCatcher) is rolled over, play a different part of this timeline. (with the below code, it just plays the parent timeline

                            

                             var rl=this;

                             rl.stop();

               

               

                             var frequency=3;

                             stage.enableMouseOver(frequency);

                             rl.clickCatcher.on("rollover",fl_MouseOverHandler_16);

               

               

                             function fl_MouseOverHandler_16()

                                  {

                                      rl.play(328);

                                      stage.enableMouseOver(0);

                                  }

              • 4. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
                kglad Adobe Community Professional & MVP

                use:

                 

                       var tl=this;

                               tl.stop();

                 

                 

                               var frequency=3;

                               stage.enableMouseOver(frequency);

                               tl.on("rollover",fl_MouseOverHandler_32,null,true);

                 

                 

                               function fl_MouseOverHandler_32()

                                    {

                                        tl.play();

                                        stage.enableMouseOver(0);

                                    }

                 

                // child mc

                 

                    var rl=this;

                               rl.stop();

                 

                 

                               var frequency=3;

                               stage.enableMouseOver(frequency);

                               rl.clickCatcher.on("rollover",fl_MouseOverHandler_16,null,true);

                 

                 

                               function fl_MouseOverHandler_16()

                                    {

                                        rl.gotoAndPlay(328);

                                        stage.enableMouseOver(0);

                                    }

                • 5. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
                  User_0 Level 1

                  Thanks a lot, I'm thrilled to have found a solution. I wonder if you'd mind explaining what the 'null, true' values do?

                  • 6. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
                    kglad Adobe Community Professional & MVP

                    the 2nd parameter is the 'scope'.  (ie, you can use the keyword 'this', without quotes, to pass a reference to the timeline) and the third parameter indicates whether the listener should be removed after executing once.

                    • 7. Re: How to handle mouse events in Flash? What exactly does 'this' refer to?
                      User_0 Level 1

                      That's great. This'll help me a lot in the future. Thanks again.