You are here:

### Rotation in ActionScript 3

Welcome to Foundation Flash's tutorials. In today's tutorial, I will be expanding on some of the methods you learned about in our previous tutorials about trigonometry, and introducing those methods into Flash, in order to show you how Flash deals with them, and how you can use them in your work. The final product of today's tutorial will be this:

So, let's get to work. You'll remember last time, we explained how to get an angle from two lengths of a triangle using inverse trigonometric functions. I will use these functions in order to make that angle rotation swf above. But first, let's look at how Flash handles these functions. If you were to try to find an angle where you had the opposite (5) side, and the hypotenuse (10) side, you would do the following calculation:

sin-1(5/10)

In Flash, you would do this:

`Math.asin(5/10)`

This would return the correct answer of 30, right? Wrong. Open a new Flash document and type the following code into the first frame of your movie:

```var angle = Math.asin(5/10);
trace(angle);```

You'll notice it returns a very odd number, 0.523598775598299. So why isn't it 30? Well, this is because Flash measures angles in an odd form of angle measurement, radians. You don't need to really need to know much about this form of measurement (I don't), all you need to know is how to convert between degrees and radians. The formula for converting radians to degrees is:

And the formula for converting degrees to radians is:

degrees/(180/pi)

The one we will be using is radians to degrees. So change your angle variable to:

`Math.asin(5/10)/(Math.PI/180)`

Now test your movie. Yay! It now outputs the correct number, 30. So, now we know how Flash handles trigonometric functions, we can get to work on our mouse following arrow.

Open a new Flash document, and inside create a Movie Clip with the instance name of arrow. Inside, draw (you guessed it) an arrow. Note, the registration point should be on the end of the arrow, on the point which you want it to rotate about. Point the arrow upwards. Next, create a dynamic textbox with the instance name angletext. Go to the frame your arrow is on and draw up the frame's actions panel by pressing the F9 Key. Inside, you need to first type:

```stage.addEventListener(Event.ENTER_FRAME,rotate);
function rotate (e:Event){```

This means that the following code will be executed every frame. Next, type:

```     var theX:int = mouseX - theArrow.x;
var theY:int = (mouseY - theArrow.y) * -1;```

This stores the x and y positions of the mouse, in relation to theregistration point of the arrow. The y is reversed, as y co-ordinates in Flash are reversed, and we want them not to be reversed for our equation. Next, we figure out the angle, using the following equation:

`     var angle = Math.atan(theY/theX)/(Math.PI/180);`

And we store it inside the angle variable. We use atan as we are using the theX and theY variables, and if you think of it like a triangle, with the angle at the center, we are using the opposite and adjacent sides. However, this does not stop here. Think about it carefully. If our mouse goes to the left of the object Movie Clip, then the theX variable becomes negative so if we wanted x to be 10 and y to be 5, it would actually calculate:

`Math.atan(-5/10)/(Math.PI/180);`

So would return -26.565051177078 instead of 26.565051177078. And even in that case, its still wrong as it is calculating in the wrong direction. What can we do to remedy this? Well, in this instance, we would add 180. The first 90 degrees would be to make it positive, and the second would be to move the angle into the upper left quadrant (corner). So under where we declared our angle variable, type:

``` if (theX<0) {
angle += 180;
}```

So, if theX is negative, add 180 to our angle. Now we need to sort out the bottom quadrants. Well, the bottom left would actually return a positive number, as both theX and theY would be negative, so all it would need would be another 180 degrees to put it to its correct position. Luckily, I planned this so that it is covered by the first if statement. Clever, eh? Now, last quadrant need 90 degrees added to take it out of negative numbers, and then another 270, to put it in its right place. That makes 360, so:

``` if (theX>=0 && theY<0) {
angle += 360;
}```

If theX is greater than or equal to 0, and theY is less than 0, then we add 360 to our angle variable. Now, we want to display our returned angle in our angletext textbox, so type:

`     angletext.text = angle;`

Now, our angle will be displayed in our textbox. Lastly, we want our arrow to follow our mouse. We do this by first making the angle negative, as Flash uses cartesian co-ordinates, which are back to front, and then by adding 90, as Flash measures 0 degrees as 90, 90 degrees as 180, etc. So we end up with:

` arrow.rotation = (angle*-1) + 90`

And of course, don't forget to close our event handler:

`}`

Test your movie, and it should work. The full source code is also available, should you need it.

Now, I've tried to explain this in as much detail as possible, as I know it is a very difficult concept to understand, but if you still don't understand, or it's not working, or you just want a bit more explanation, feel free to email us at [email protected]. I hope you have learned something valuable from this, thanks for reading, and stay tuned!

Leon (edited for ActionScript 3 by Harry).