8 Replies Latest reply on May 9, 2007 2:43 AM by ggshow

    boundary for movie clips following the mouse

    NickTheNameless
      here's my AS...

      myInterval = setInterval(KBMOglobal,15);

      function KBMOglobal () {
      KBMOglobal1._x -= (KBMOglobal1._x - _xmouse)/10;
      KBMOglobal1._y -= (KBMOglobal1._y - _ymouse)/10-2;
      }

      ok, so my movie clip follows the mouse around, with a small delay and the y axis is slightly lowered. the stage of my movie is 380 x 640. does anyone have any suggestions on how to create a boundary, equal to the size of my stage, that does not let the movie clips that follow the mouse extend past? does that even make any sense? essentially, i would like the movie clips to follow the mouse, the entire movie clip stay visible on the stage. because of the positioning of the items on the stage that initiate this function, i only have to consider the width of each movie clip, which luckily is a constant = 200. does that make any sense? let me try this all again....

      how do adjust the above function so that the movie clip, KBMOglobal1 (which has a width of 200) stay within the horizontal limits of the stage (which is 380)?

      thanks for your time and to everyone in this forum that has been so very helpful with all my questions in this project so far, yall rock!
        • 2. Re: boundary for movie clips following the mouse
          NickTheNameless Level 1
          this is almost working perfectly, however i need to adjust the boundary of the x axis. for some reason, the movie clip stops following the mouse when it reaches half way across stage, horizontally.

          would you please be so kind to explain what your script is doing? because i'm not a total idiot, i realize i could probably replace 380 with 760 (double the width of the stage) and it would work as expected. however, if you could spare the time to explain what the following two lines are doing, step by step....i'll give you my first born.....

          KBMOglobalGeneralCancelDispatch._x<0?KBMOglobalGeneralCancelDispatch._x=0:KBMOglobalGenera lCancelDispatch._x;
          KBMOglobalGeneralCancelDispatch._x>380-KBMOglobalGeneralCancelDispatch._width?KBMOglobalG eneralCancelDispatch._x=380-KBMOglobalGeneralCancelDispatch._width:KBMOglobalGeneralCancel Dispatch._x;

          thanks again for your time!
          • 3. Re: boundary for movie clips following the mouse
            Greg Dove Level 4
            I think from what you describe, whether this works as expected will depend on the registration point of the clip... i.e. what part of it is at 0,0 internally . It sounds like it might be centred and you need it at 0,0 for this code to work. Try double clicking on your instance to edit the symbol, select all (Ctrl-a if there are no locked layers) and type 0 for x and y in the property panel. See if that makes a difference.
            • 4. Re: boundary for movie clips following the mouse
              NickTheNameless Level 1
              thanks. for cosmetic reasons, i want to keep the registration point in the center of the movie clip. so i altered the line to divide the width of the clip by 2 like so.....

              KBMOglobal1._x>(380-KBMOglobal1._width/2)?KBMOglobal1._x=(380-KBMOglobal1._width/2):KBMOgl obal1._x;

              thanks again for your time and answer!
              • 5. Re: boundary for movie clips following the mouse
                ggshow Level 2
                quote:

                Originally posted by: NickTheNameless
                this is almost working perfectly, however i need to adjust the boundary of the x axis. for some reason, the movie clip stops following the mouse when it reaches half way across stage, horizontally.

                would you please be so kind to explain what your script is doing? because i'm not a total idiot, i realize i could probably replace 380 with 760 (double the width of the stage) and it would work as expected. however, if you could spare the time to explain what the following two lines are doing, step by step....i'll give you my first born.....

                KBMOglobalGeneralCancelDispatch._x<0?KBMOglobalGeneralCancelDispatch._x=0:KBMOglobalGenera lCancelDispatch._x;
                KBMOglobalGeneralCancelDispatch._x>380-KBMOglobalGeneralCancelDispatch._width?KBMOglobalG eneralCancelDispatch._x=380-KBMOglobalGeneralCancelDispatch._width:KBMOglobalGeneralCancel Dispatch._x;

                thanks again for your time!



                do you know ?: operator? it is like if...else... statements,

                when we say:

                somethingIsTrue ? do1() : do2();

                it is exactly same meaning like:

                if (somethingIsTrue) {
                do1();
                } else {
                do2();
                }

                Now, we look at this line:

                KBMOglobalGeneralCancelDispatch._x<0? KBMOglobalGeneralCancelDispatch._x=0: KBMOglobalGeneralCancelDispatch._x;

                it is same like

                if (KBMOglobalGeneralCancelDispatch._x<0) {
                KBMOglobalGeneralCancelDispatch._x=0
                } else {
                KBMOglobalGeneralCancelDispatch._x
                }
                • 6. boundary for movie clips following the mouse
                  ggshow Level 2
                  when x smaller than zero (_x<0), we set it back to zero (_x=0), that mean it will never go over the left boundry

                  380 is your stage width, KBMOglobal1._width is the width of your KBMOglobal1
                  380-KBMOglobal1._width is the maximum x allowed for your KBMOglobal1 to go to right side, lets call it "right limit",

                  KBMOglobal1._x>380-KBMOglobal1._width?KBMOglobal1._x=380-KBMOglobal1._width:KBMOglobal1._x ;

                  here we set, if the x bigger than right limit, we set it back to right limit,
                  so now your KBMOglobal1 will never go more than right limit.

                  we have already set the left & right limit

                  we use the same way to apply up & down limit based on _y.
                  • 7. boundary for movie clips following the mouse
                    NickTheNameless Level 1
                    i did not know what the :? operator did, it confused me for a minute, but then i figured it out (also figuring out my solution above didn't do what i wanted it to as well). i ended up altering the lines as so....

                    KBMOglobal1._x<102?KBMOglobal1._x=102:KBMOglobal1._x;
                    KBMOglobal1._x>278?KBMOglobal1._x=278:KBMOglobal1._x;

                    since all my "KBMO" movie clips were the same width (200), with a registration point in the top center, i allowed a 102 pixel buffer on each side of my 380 pixel wide stage. because of the layout of my movie i did not have to worry about the vertical boundaries on the y axis.

                    i want to really thank you for your answer again. not only did this solve my problem, this knowledge will be very helpful in the future and i expect i will use this operator frequently from now on. thanks again, you just taught someone how to fish!
                    • 8. Re: boundary for movie clips following the mouse
                      ggshow Level 2
                      you are welcome

                      cheers