Math Functions

Move the mouse and Lincoln watches the mouse. Get Adobe Flash player

Eyes follow cursor: The eye is drawn as shown with the registration point in the middle as shown.
eyeThe eye is on a layer that is below the face, and the actual eye in the face layer has been erased so that only part of the circle shows through.
Whenever the mouse moves, the angle between the mouse and the eye is calculated and the eye is rotated so that it appears to follow the mouse.
The x and y values of a movie clip is relative to the movie clip it is inside. If lincoln.eye1.x is 50 and lincoln.x is 20.
The x position of eye1 relative to the stage is 70. (lincoln.x + lincoln.eye1.x)

this.addEventListener(Event.ENTER_FRAME,frameEvent);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveEyes);
//functions
function frameEvent(e:Event): void {
	var r:Number=Math.random()*30; //a random number between 0 and 30
	if(r<1) lincoln.gotoAndPlay(2); //Lincoln blinks about once in 30 frames
}//frameEvent

function moveEyes(e:MouseEvent): void {
	//lincoln left eye
	var dx:int=e.stageX - (lincoln.x+lincoln.eye1.x); //position of eye on stage
	var dy:int=e.stageY - (lincoln.y+lincoln.eye1.y);
  	var radians:Number=Math.atan2(dy,dx);
  	lincoln.eye1.rotation=radians*180/Math.PI;
	//lincoln right eye
	dx=e.stageX - (lincoln.x+lincoln.eye2.x);
 	dy=e.stageY - (lincoln.y+lincoln.eye2.y);
 	radians=Math.atan2(dy,dx);
 	lincoln.eye2.rotation=radians*180/Math.PI;
} //move eyes

Eyes blink: There are three positions for the eyelid. In frame 1 there is no eyelid covering the eye.
The ActionScript for frame 1 is stop(); In frame 2 the eyelid partially covers the eye, and in frames 3 and 4 the eyelid completely covers the eye.
Each time we enter a frame, a random number from 0 to 30 is calculated. If the number is less than 1 (about 1 in 30 times) we gotoAndPlay lincolns frame 2.
When it gets to the end, it goes back to frame 1 and stops. The whole blink is only 4 frames.
Frame 1:
frame1
Frame 2:
frame2
Frames 3 & 4:
frame 3 and 4

Save the fla file to your computer to experiment.
Experiment: Make Washington's eyes do the same.
Experiment: Create an Eye class that blinks and follows the cursor.

NEXT: Random Numbers: Math.Random()