A tutorial in changing rotation due to where the mouse is

Firstly open up a flash document and make a barrel-Draw a big circle and a smaller circle within this and then a Diagonal line coming out from this smaller circle. Select this whole drawing and convert it into a movie clip and give it an instance name.

Now make a tank that this barrel will go on top of-Simply draw a rectangle and convert it into a graphic and give it an instance name. If this rectangle hides the barrel, click modify and send to back.

Now go back to the movie clip and in the actions panel enter this code:

onclipEvent (enterFrame)  {                                                                                                radians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);                degrees = (radians/Math.PI)*180;                                                                      this.rotation = degrees;                                                                  }                                                         

This code will make the diagonal line follow the mouse. I will now discuss what parts of the code do:

  • radians = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);   

The above code equates the angle between the mouse and the barrel measured in radians. We subtract the y and x co-ordinate of the mouse from the y and x co-ordinate of the barrel. The atan2 part requires differences between two co-ordinates to evaluate the angle in radians. To view a picture on how this works and to understand more into radians and degrees, visit the link below:


  • degrees = (radians/Math.PI)*180;  

The above code converts radians into degrees. It easily evaluates the angle measured in degrees.

  • this.rotation = degrees;

Finally this part of the code sets the rotation of the barrel. It does this by assigning rotation property of the barrel to the variable of degrees.

If you now want to learn how to shoot out of the barrel by clicking the mouse as well as rotating it, visit the link below:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: