This is one of the first things that i tried to do in flash when i first got mx. now i will explain how to rotate a movieClip to face the mouse in adobe cs3.
To start off you can either draw the object that you want to rotate on the stage, or make it entirely in actionscript, as long as its a movieclip with an instance of it on the stage it will work.
First i have to create the object i am going to rotate. i am going to do it in actionscript with the built in drawing API.
I will explain the drawing API in greater detail in a later tutorial. But this code will make a turret shaped movieclip.
turret.graphics.beginFill(0xFFFFFF);
turret.graphics.lineStyle(2, 0xFFFFFF);
var turret:MovieClip = new MovieClip();
turret.graphics.drawRect(-10, 0, 20, 40);
turret.graphics.endFill();
turret.graphics.beginFill(0xFFFFFF);
turret.graphics.drawCircle(0,0,20);
turret.graphics.endFill();
turret.x = 275;
turret.y = 200;
addChild(turret);
Ok now to explain the code;
line 1: This introduces a new variable called “turret” which is a movieClip. We can now use the instance “turret” to give it commands.
line2: Using the movieClip.graphics method i am telling the turret movieclip to start a fill colour which is white. the “0xFFFFFF” is a colour hex code. 6 Fs means white.
line 3: This is setting the linestyle of the movieClip to 2 pixels thick and then its colour, again white.
Line 4: This line draws a rectangle in the movieClip instance, the first number is the x coordinate, the second is the y coordinate, the third number is the width and the last number is the height. If you want the object to have dynamic actions applied to it it is best to set the x and y coordinates to 0.
line 5: Telling the movieClip to stop filling in with the colour.
line 6: Setting a new fill, with the same colour as before. If i hadn’t ended the previous fill then part of the movieClip would be transparent.
line 7: This command “drawCircle” does exactly what it says, it draws circles. the numbers are x then y coordinates and then the radius of the circle. The coordinates are set to 0 because we want the turret to rotate about its centre.
line 8: Ending the circle fill colour.
line 9-10:These are setting the x and y coordinates of the turret, these can be anything but i’ve set them to the centre of the stage.
line 11: This is probably the most important line. It adds the movieClip instace to the stage so that we can see it!
Copy these actions in your actions panel and set the background colour to something other than white. I have used black. It should look exactly like this.
In the next part of the tutorial we will make it actually rotate.



April 15th, 2010 at 1:56 pm
This code would probably be of more help if it… Well, you know… Actually had the code to perform what the header says… All this does is make a turret…