Monday, October 10, 2011

AS2: Mouse Controls Character Rotation

Source File Available Here: http://adf.ly/38qsi

Welcome to Tutorial #9! In this tutorial, I will teach you how to aim your character towards the mouse position.
Let's get started by opening up a new Flash AS2 document:












Now let's draw our character on the stage. Then convert it to a movie clip called "char". Next, put the registration point where you would like to have the character's rotation axis at. In this case, I'm putting mine in the center box, so that the character rotates around the center of itself:











Now let's open up that actions of the character and type:

onClipEvent (enterFrame) {
disx = _root._xmouse-_x;
disy = _root._ymouse-_y;
Radians = Math.atan2(disy, disx);
Degrees = 360*Radians/(2*Math.PI);
_rotation = Degrees;
}


1.) disx and disy are setting variable for the x and y coordinates of the mouse cursor
2.) Radians is settings the variable for the distance between disx and disy
3.) Degrees calculating the rotation angle
4.) _rotation is settings the angle to the Degrees variable


Now you are done! Test your game to see your character aim at the mouse.
This concludes Tutorial #9! Thanks for viewing and remember to subscribe to my blog for more tutorials!

Source File Available Here: http://adf.ly/38qsi

No comments:

Post a Comment