15 Replies Latest reply on Jan 22, 2010 10:32 PM by kglad

    MOUSE FOLLOW in AS3

    Pain in the neck

      I have looked at other posts on this subject, but have not seen what I needed. I am using CS3, AS3 to do a mouse follow and I have what I want in terms of the smooth mouse movement with my mc. What I am scratching my head about is I would like the active mc to "nest" in one area of the larger movie (which in this case is a banner) when the mouse is not over the banner area. Here is the code I have used so far:

       

      stage.addEventListener(Event.ENTER_FRAME,followBall);

       


      function followBall(event:Event):void {
      var dx:int = ball_mc.x - mouseX;
      var dy:int = ball_mc.y - mouseY;
      ball_mc.x -= dx / 5;
      ball_mc.y -= dy /5;
      }

       

      This code has been applied to the timeline of the banner, not to the mc symbol.

       

      Any suggestion for a beginner? Baby talk pleeese.

        • 1. Re: MOUSE FOLLOW in AS3
          kglad Adobe Community Professional & MVP

          if your banner covers the entire stage, use:

           

          var targetX:Number;

          var targetY:Number;

           

          stage.addEventListener(Event.MOUSE_LEAVE,nestF);

          stage.addEventListner(MouseEvent.MOUSE_MOVE,updateTargetF);

           

          function updateTargetF(e:MouseEvent){

          targetX=mouseX;

          targetY=mouseY;

          }

           

          function nestF(e:Event){

          targetX=whatever;

          targetY=whatever else;

          }

           

          stage.addEventListener(Event.ENTER_FRAME,followBall);

           


          function followBall(event:Event):void {
          var dx:int = ball_mc.x - targetX;
          var dy:int = ball_mc.y - targetY;
          ball_mc.x -= dx / 5;
          ball_mc.y -= dy /5;
          }

          • 2. Re: MOUSE FOLLOW in AS3
            Pain in the neck Level 1

            kglad,

             

            I receive a syntax error for line 27 when I copied and pasted your code into my timeline. Any suggestions? Thanks much for the help.

            • 3. Re: MOUSE FOLLOW in AS3
              Pain in the neck Level 1

              Sorry that would be this line:

               

              targetY=whatever else;

               

              }

              • 4. Re: MOUSE FOLLOW in AS3
                kglad Adobe Community Professional & MVP

                you need to define the x,y that you want to use for that movieclip when the mouse leaves the stage.

                • 5. Re: MOUSE FOLLOW in AS3
                  Pain in the neck Level 1

                  Forgive me if I am dense here... Do you mean where you have written the word whatever in the code, instead add the coordinates (where I want the mc to "rest") such as :

                   

                  function nestF(e:Event){

                   

                  targetX=187.4;

                   

                  targetY=54.2 else;

                   

                  }

                   

                  ????

                   

                  I continue to get the error saying "right brace expected before else".

                   

                  I do appreciate your continued help!

                  • 6. Re: MOUSE FOLLOW in AS3
                    kglad Adobe Community Professional & MVP

                    use:

                     

                    function nestF(e:Event){

                    targetX=10;

                    targetY=10;

                    }

                    • 7. Re: MOUSE FOLLOW in AS3
                      Pain in the neck Level 1

                      My code now reads as follows:

                       

                      var targetX:Number;

                       

                      var targetY:Number;

                       

                       

                       

                      stage.addEventListener(Event.MOUSE_LEAVE,nestF);

                       

                      stage.addEventListner(MouseEvent.MOUSE_MOVE,updateTargetF);

                       

                       

                       

                      function updateTargetF(e:MouseEvent){

                       

                      targetX=mouseX;

                       

                      targetY=mouseY;

                       

                      }

                       

                       

                       

                      function nestF(e:Event){

                       

                      targetX=10;

                       

                      targetY=10;

                       

                      }

                       

                      stage.addEventListener(Event.ENTER_FRAME,followBall);

                       

                       

                       


                      function followBall(event:Event):void {
                      var dx:int = ball_mc.x - targetX;
                      var dy:int = ball_mc.y - targetY;
                      ball_mc.x -= dx / 5;
                      ball_mc.y -= dy /5;
                      }

                       

                      Here is the error I receive:

                      1061: Call to a possibly undefined method addEventListner through a reference with static type flash.display:Stage.�

                      • 8. Re: MOUSE FOLLOW in AS3
                        kglad Adobe Community Professional & MVP

                        my bad.  the stage doesn't accept mouseevents.  use:

                         

                        var targetX:Number;
                        var targetY:Number;

                         

                        var sp:Sprite = new Sprite();
                        with(sp.graphics){
                            beginFill(0x000000);
                            drawRect(0,0,stage.stageWidth,stage.stageHeight);
                            endFill();
                        }
                        addChild(sp);
                        sp.alpha = 0;
                        sp.addEventListener(MouseEvent.MOUSE_MOVE,updateTargetF)

                         

                        stage.addEventListener(Event.MOUSE_LEAVE,nestF);
                        function updateTargetF(e:MouseEvent) {
                            targetX=mouseX;
                            targetY=mouseY;
                            trace(targetX);
                        }

                         

                        function nestF(e:Event) {
                            targetX=10;
                            targetY=10;
                        }

                         


                        stage.addEventListener(Event.ENTER_FRAME,followBall);

                         


                        function followBall(event:Event):void {
                            var dx:int = ball_mc.x - targetX;
                            var dy:int = ball_mc.y - targetY;
                            ball_mc.x -= dx / 5;
                            ball_mc.y -= dy /5;
                        }

                        • 9. Re: MOUSE FOLLOW in AS3
                          Pain in the neck Level 1

                          Well that is curious, upon playing the movie I get the output box giving me constant numbers while it plays. Hmmm.

                           

                          What I see is my mc following my mouse but still not returning to the original resting spot when the mouse leaves the stage area.

                           

                          You no bad, you being helpful! Thank you!!!

                           

                          In this area (where the bold is) should I fill in my stage size?

                           

                           

                          var sp:Sprite = new Sprite();
                          with(sp.graphics){
                              beginFill(0x000000);
                              drawRect(0,0,stage.stageWidth,stage.stageHeight);
                              endFill();

                          • 10. Re: MOUSE FOLLOW in AS3
                            kglad Adobe Community Professional & MVP

                            test in your browser so the mouseleave event is dispatched.

                             

                            and yes, the sprite fills your stage but shouldn't be visible because its alpha is zero.

                            • 11. Re: MOUSE FOLLOW in AS3
                              Pain in the neck Level 1

                              What I did was insert it into a web page and test it there, is there a simpler way to test in a browser?

                               

                              Yes it worked! The mc goes back to the "nesting" spot when the mouse leaves the stage area. I would like that spot to be in a different location tho, right now the mc goes to the far upper left corner and I want it mid lower left. Which part of the code identifies where the location of the "nest" is?

                               

                              Thank you!

                              • 12. Re: MOUSE FOLLOW in AS3
                                Pain in the neck Level 1

                                kglad,

                                 

                                Okay I see where to change it! YES!!! My question has been answered! Thank you! Can you recommend a readable beginning actionscript book for AS3? Thank you again.

                                • 13. Re: MOUSE FOLLOW in AS3
                                  kglad Adobe Community Professional & MVP

                                  the flash help files are pretty good.  and, senocular's website senocular.com has a lot of helpful info about as3.

                                  • 14. Re: MOUSE FOLLOW in AS3
                                    Pain in the neck Level 1

                                    Thanks I will look into the site. I often become frustrated with the help files, not knowing the term I am looking for to do a complete search.

                                     

                                    Appreciate your expert help.

                                    • 15. Re: MOUSE FOLLOW in AS3
                                      kglad Adobe Community Professional & MVP

                                      to learn as3 from the help files, click "Programming Actionscript 3.0"

                                       

                                      to use the help files i expand the class i need to use to check its properties, methods and events.  that requires a fair amount of familiarity with as3 so you know which class you should be checking.

                                       

                                      but even now it's not unusual for me to want to check something (like say the pixelBounds property) and i might check the displayobject class looking for that property and failing to find it there i might check the sprite class and when that fails i might check the interactiveobject class and when that fails i finally check the transform class.  and only when i get desperate will i use the search option.