Search Tutorials
Step Four: The Revolving MovieclipThe movieclip I wanted to follow the needle is actually a dynamic textbox inside a movieClip. On the stage use the text tool to create a textbox. In the options choose dynamic, single line, dont check any of the checkboxes and embed any fonts you require. In the variable box add this:_root.v Then select this textbox and convert it to a movieClip. It does not need an instance name. We will use an onClipEvent action to position this movieClip at the correct place. Now comes the maths!
We need to give the movieclip the coordinates (x,y) at the end of the needle. Seeing we have the angle (w) and the length of the needle (in my case 210 pixels) we can work out everything else with sin/cos. Use the grid to measure the length of your own needle. The bottom right has the formulae if you are unfamiliar with these trigometric functions. One thing we need to do now is convert our angle (w) from degrees to radians, which we must use as the angle in these formulae. Back on the main timeline with the other variables add this: radian = Math.PI/180*(w); This is just a formula to do the conversion. Select the textbox movieclip and add this code:
onClipEvent (enterFrame) { Using the top line of code which generates the x coordinate as an example I will explain the maths. If the sine of an angle is the opposite over the hypotenuse that would mean sine(radian) = opposite/210 therefore the opposite (x coordinate) = 210 * sin(radian), which you can see is what we have in the code. At the end we have added + _root.needle._x; as we are working out our angles from the needles registration point. So as the angle w increases, our x,y coordinates will be constantly updated and the movieclip will track around the needles outer edge. If you test the movie now you will see everything working apart from one small detail. The movie clip is tracking in a counerclockwise direction, opposite to the needle!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||
|