Movie Symbol Follows Mouse

Movie Symbol Follows Mouse

Flash CS4 ActionScript 3.0 Tutorial

 

How to Make Symbol Follow User's Mouse Movement in Flash

 

 

This code detects the user's mouse location and moves the Movie Symbol to follow the mouse.

Tutorial Elements

Movie Symbol with instance name moveObj_mc

 

Step One

Add an object to the stage.

Step Two

Convert the object to a Movie Symbol by pressing F8 and choosing Movie Clip for the type.

Step Three

Assign your Movie Symbol an instance name of moveObj_mc.

Step Four

Add a new layer at the top and name it Actions.

Step Five

Option (PC-Alt) double-click the first frame of the Actions layer to open the Actions panel.

Step Six

Copy/Paste the following code to the ActionScript panel.

addEventListener(Event.ENTER_FRAME, onLocation);

function onLocation(e:Event):void {

moveObj_mc.x=mouseX;

moveObj_mc.y=mouseY;

}

Step Seven

Click the Auto Format button to format the code

Step Eight

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Move your mouse on the stage and the Movie Symbol follows the mouse.

Dissecting the Code

The first line of the code adds an event listener and connects it to a function named onLocation. The listener fires at the frame rate of the document. If your frame rate is 18 fps, the event happens 18 times per second.

The second line adds a function that causes the x and y mouse locations to equal the x and y location of the object.

You may want to place the registration point of your object in the center when following the mouse movements.

Dragging an Object

Another option besides following the mouse is to click-drag the object with the user's mouse. For more info, see topic, Click and Drag Movie Symbol.

 

Stock Photos from 123RF