RSS link icon

.

Cool mouse follow animation


In this Flash tutorial you will see how to make an object follow your mouse cursor, this is a great feature for games, animations or any other place, (but please don't do custom cursor on a website, not many web designs can pull that off without looking like an amature site).

Download button

I have added a delay on the object that follows the mouse, to make it a bit more interesting, because honestly its fairly easy and only requrers a few lines of code.

First you have to make or import an object / image, then convert it to a movie clip by right click and choose convert to symbol. As you can see in the image below I made a flower of some kind, you can do as I did, go into the movie clip and animate it if you feel like it, but thats another tutorial.

flash follow mouse tutorial

Now give your new movie clip an instance name we can refere to in our action script coding, I named mine flower, as you can see from the image below.

follow mouse flash tutorial

Now hit F9 and add the following code.

_root.onEnterFrame = function(){
    X = _root._xmouse - 10;
    Y = _root._ymouse - 10;
    flower._x = flower._x + (X - flower._x)/10
    flower._y = flower._y + (Y - flower._y)/10
}

onEnterFrame is one of the most common event handlers, everything you put in the onEnterFrame will be updated all the time, so therefore if you say something should have the same position as the mouse it will update as the mouse moves, as it is shown in the code above.

What we then did was to add some delay to the flower. These two lines control the x and y axis and the speed our flower should have going closer to the mouse cursor.

flower._x = flower._x + (X - flower._x)/10
flower._y = flower._y + (Y - flower._y)/10

Admin Bob says: 2008-10-13

hi thijs, this usually means you have been placing some code outside the enterframe event brackets, try placing it inside as shown above, if not then send me an email with your code, and I will have a look for you. support@0tutor.com

thijs says: 2008-10-13

I keep getting this error: Error Scene=Scene 1, layer=Layer 2, frame=1:Line 1: Statement must appear within on/onClipEvent handler _root.onEnterFrame = function()

eee says: 2008-09-19

F9 is the actions window

Tyler says: 2008-08-06

Uh, this is great, BUT iChr(34)m a newbie to flash so... I donChr(34)t know what the F9 does and since I have a Mac... IDK WHAT TO DO!

piprazer says: 2008-07-08

i tryed it but it does not work

Tony says: 2008-03-13

Nice one, worked a charm :)

Rio says: 2008-03-12

Good work Thanks

Admin Bob says: 2008-02-23

try to download the flash project, at the top of the page, and see if that works, if it does, it might help you on how to make your own work.

lfb says: 2008-02-22

forgot to mention this: I did not paste the actionscript code on the main timeline but only on the Chr(34)flowerChr(34)(the instance name is also flower too).

jaythefray says: 2008-02-22

Nope that still doesnChr(34)t work, I get the same error.

jaythefray says: 2008-02-22

Nope that still doesnChr(34)t work, I get the same error.

jaythefray says: 2008-02-22

Nope that still doesnChr(34)t work, I get the same error.

lfb says: 2008-02-22

I got an error. Error Scene=Scene 1, layer=Layer 1, frame=2:Line 1: Statement must appear within on/onClipEvent handler _root.onEnterFrame = function(){ Total ActionScript Errors: 1 Reported Errors: 1

Admin Bob says: 2008-02-22

Hi jaythefray, That error usually indicates that you have placed that actionscript code in the main timeline, and not within the movieclip, you should first click to select the movie clip,then open up actionscript.

jaythefray says: 2008-02-22

Hey I get this error: Error Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Statement must appear within on/onClipEvent handler _root.onEnterFrame = function(){ Total ActionScript Errors: 1 Reported Errors: 1 I named the object I have "flower" just to follol along...

tyler says: 2008-01-26

when you say use f9 where are we entering the actionscript...sorry mac user

Admin bob says: 2008-01-27

Sorry tyler, I donChr(34)t use mac my self, but I guess you can find the actionscript panel from the top menu called window then choose Acions

Admin bob says: 2008-01-20

hi lady_bird, I got that too, but what works for me is to open flash then drag the flash file into the program, then it opens. let me know if you still have problems.

josh says: 2008-01-17

if u cant make it work just double check everything. i misspelt the instance name in my code and it took me a second to figure it out but the code works.

lady_bird says: 2008-01-16

hi, I tried to open the flash file u have attached.but its showing unexpected file formet.I tried to opened it in 2004 and flash8 as well.but its not working.

Dan says: 2008-01-05

erm..........when u hit F9 to actionscript.... make sure the movie clip isnt selected and make sure the instance names match up should work bud

Jenifer Lopez says: 2007-12-24

I must say this thing is awesome, my team used it for my music video but they put a picture off me!. it was my best music video so far

Mark Danforth says: 2007-12-24

I also canChr(34)t makei t work. i type in all the codes and everything i need help

Gillian Wentworth says: 2007-12-24

Yes i agree with Libby, it is cool, and it was surprisingly easy! Very good indeed

Libby Tayla says: 2007-12-24

Wow!! this is seriously cool! i got a little stickman to follow the mouse and somehow it does cartwheels.

cabs says: 2007-12-19

I cant get it to work... i make a shape, make it a movie clip, i gives it the instance-name: flower And i put in the code, but somehow it doesnt work

Chris Andrews says: 2007-12-19

I followed the tut 25 times and the mouse didnChr(34)t follow. Oops. What did I miss Thx

Admin bob says: 2007-12-20

Hi Chris and cabs, I have uploaded a copy of the flash project file I used, I think this could help you, but if not, then either you should specify what error message flash gives you, so I can trouble shoot it for you. Best regards Administrator Bob

Admin Bob says: 2007-12-25

Thanks all for the compliments. Hi Mark Danforth you have to specify what goes wrong when you try to follow the tutorial, so I can try to help you.