1 Reply Latest reply on Jul 29, 2009 7:01 AM by logeye

    I need help understanding how this works. AS 2.0


      Its my first time here and i only started learning flash today. I aint too great with maths and i was showed this piece of code that makes a triangles point follow the cursor. It involved mathimatical things ( which i aint so great at) and the guy said "You dont need to know how it works, just how to use it." well i copied his code and it worked, but i have no idea how to use it in other rotation apart from the one he taught us. I dont even understand how to redo his code i saved it to a word document and anytime i wanted to make something rotate something in that fashion i would refer to that. But i dont want to have to do that, i want to know how to use it, dont have to understand it, but use it for other rotation matters. If you are going to help, please try to be a bit simmple i explaining it, cause im new. Dont get me wrong i aint thick but i can find it a bit hard to follow some things, that is i my current problem.


      here is the rotation code:


      onEnterFrame = function()

      hero._rotation = getmouse(hero);


      getmouse = function(mc:MovieClip):Number


      dy = _ymouse-mc._y;
      dx = _xmouse-mc._x;


      rad = Math.atan2(dy,dx);

      rotate = rad*180/Math.PI
      return rotate+90




      also if it helps, here is the video i was watching: http://www.youtube.com/watch?v=w3OfrpbNhHs


      please if you can, explain how the entire thing works.


      thanks for any help given in advance.


        • 1. Re: I need help understanding how this works. AS 2.0



          Here's a short primer.  It may not be sufficient but here goes.


          1st, move the closing bracket at the end and put it on the third line.  This makes the code more efficient


          onEnterFrame = function(){                               // this causes Flash to repeatedly

                                                                                    execute the next line at the

                                                                                    frame rate you selected

                                                                                    for your document
                 hero._rotation = getmouse(hero);               // this tells Flash to rotate a

                                                                                    movie clip (named hero) based

                                                                                    on the function getmouse()

          };                                                                     // putting the }; here makes the

                                                                                    code more efficient and readable

          getmouse = function(mc:MovieClip):Number{   // This is the function called with

                                                                                   mc referring to hero that was

                                                                                   passed from the second line.

               dy = _ymouse-mc._y;                                // dy means delta y which subtracts

                                                                                  the y position of the movieclip

                                                                                  from the mouses y position
               dx = _xmouse-mc._x;                               // dx = delta x (same as above line

                                                                                  but on the x axis)

                                                                               // once you have the x and y sides

                                                                                  you male a triangle.

                                                                                  Now use trig to find the angle

               rad = Math.atan2(dy,dx);                           // the computer works in radians

                                                                                  the arc tangent atan2 will give the

                                                                                  angle in radians

               rotate = rad*180/Math.PI                            // you want to convert the radians to

                                                                                  degrees, that's what this line does
               return rotate+90                                         // this returns the value of rotate back

                                                                                  to the calling function in line 2.

                                                                                  the +90 determines which part

                                                                              // of the hero movie clip is facing the



          If you put the mouse cursor over any of the green reserved words above in the Actions panel you will get a desctription of what these do.


          hope that helps.