Sunday, 13 July 2008

Follow object on mouse click

This Flash tutorial will teach you how to move an object when the mouse is clicked. So, an object will follow the mouse click. I will be using a simple circle as an example, but you can use whatever ever objects you wish. I have also used a custom cursor.

Follow object on mouse click

Step 1


Open a new Flash document.
Select the oval tool (O) and create a circle shape on the stage. You could also import an image or create whatever object on the stage.



Step 2

Select your oval and convert it into symbol by pressing F8. Give your symbol an appropriate name, check movie clip and click ok.

Give your movie clip the instance name “ball”.



Step 3

On the timeline insert a new layer called “Action” right click on 1st frame and select actions from the drop down menu.

Add the following code:

moveBall = function () {
this.dx = (this.targx-this._x)*.1;
this.dy = (this.targy-this._y)*.1;
this._x += this.dx;
this._y += this.dy;
};

setTarget = function () {
this.targx = _root._xmouse;
this.targy = _root._ymouse;

};
ball.targx = 10;
ball.targy = 10;
ball.onEnterFrame = moveBall;
ball.onMouseDown = setTarget;


**This piece of code basically moves the ball when the mouse is clicked. There are two functions: moveBall and setTarget. The moveBall function moves the ball and the setTarget moves the ball to the position of the mouse click.


Step 4

Test your movie clip Ctrl + enter and click anywhere on the stage and the object should follow your mouse. To use a custom cursor check out this tutorial.



Your object should now follow your mouse.

0 comments:

What next ?

Subscribe to Flash Tutorials

Add to Technorati Favorites

Follow my Twitter