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 with this tutorial.

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 can alternatively import an image or create whatever object on the stage at this point.

Step 2

Select your circle shape 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” as shown in the diagram below:

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 when you click.


Preeth 5 December 2008 at 00:48  

i red u r tutorial ,its realy helpful thank u..

Also can i ask u 4 a favour.. I want to lern actionscript, i jst knw d play, goto such simple functions, i chkd in net 4 tutorils but i cant understand most of'em..., can u help me to get started wit it

thank u

iliketo 5 December 2008 at 10:55  


I don't understand your question. Can you rephase it?

lost 25 December 2008 at 09:49  

hi, i mean i really want to study action scripting, but i cant find any tutorial on net that introduces these built in functions in flash, just shows how to do this n that... thats not realy wat i want... i want to understand these things...can u help me.

iliketo 26 December 2008 at 04:25  


What in particular do you want learn?

If you want to learn actionscript in general I recommend you going to a book shop and buying a book on actionscript. Then you will get a better understanding of how to use the functions.

Andrew 31 December 2008 at 13:38  

How do you make the ball move at the same speed to any location?

iliketo 1 January 2009 at 05:16  


You click on the location on the stage.

liquidfired 11 February 2009 at 03:36  

When I put the code it just says error 1220 can you help me

iliketo 13 February 2009 at 12:18  


I used a actionscript 2.0 file. This might be your problem

Anonymous 14 July 2009 at 10:41  

Exactly what i needed. Thanks for sharing.

Natasha O 22 February 2010 at 08:42  

This was a great tutorial. Exactly what I was looking for. I have a kids game that uses key presses for movements, but some kids said it was too hard... so trying the mouse movement. Unfortunately, I cannot seem to make it so that the object will stop moving when it hits an obstacle.

Originally, I had speed associated, so when they hit the wall, speed went to 0. Now I don't have speed and cannot figure a simple way to make the object stop moving.

I have tried making the target = to the current x and y, but that does not seem to do the trick. Any help would be welcomed. Thanks!

iliketo 23 February 2010 at 05:00  

@Natasha 0

Add the following code inside the move function. Replace the 'hit_mc' for the name of the object you want to hit.

if (this.hitTest(hit_mc)) {
this._x -= this.dx;
this._y -= this.dy;

Natasha O 23 February 2010 at 07:58  

Thanks that did the trick! You rock!!

keeweet 11 March 2011 at 17:40  

i tried to do and it come out with an error

iliketo 12 March 2011 at 03:28  


What is the error message?

Faisal Raza 29 March 2011 at 00:28  

That is very nice...

The Violinist 27 March 2013 at 23:00  

Is there a new version of this code for AS3, because it doesn't seem to work for me... :(

iliketo 30 March 2013 at 03:09  

It doesn’t work because you are using Actionscript 2 code in an Actionscript 3 file. Please look at this AS3 version.

Egbert Tababa 2 September 2013 at 13:24  

It is a greet tutorial...

Question: Having an obstacle object, What should I add on the actionscript if I don't want to move the ball when the mouse clicked the obstacle object? Can you help me please on this.. Thanks

iliketo 3 September 2013 at 12:58  


Try removing the enter frame event when you are over the obstale object.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP