A useful little trick in Actionscript is to make Movie Clips elastically attracted to something (in this case, the mouse). This is really easy once you know how to do it. Create a movie clip, select it and open the actions panel (F9). Put in this code:

//set variables
onClipEvent (load) {
//the higher you set this the faster your movie clip will move
var spring:Number = 0.10;
//the higher the bouncier. keep below 1 so it reaches it's destination
var inertia:Number = 0.9;
var xspeed:Number = 0;
var yspeed:Number = 0;
}
//motion
onClipEvent (enterFrame) {
var xspeed:Number = ((_root._xmouse-this._x)*spring)+(xspeed*inertia);
var yspeed:Number = ((_root._ymouse-this._y)*spring)+(yspeed*inertia);
_x += xspeed;
_y += yspeed;
}

Set your frame rate nice and high, at least 25 for a smooth effect (personally I like 50 fps). Now run your movie to test it out. Note this is for Flash MX 2004 or higher (Actionscript 2.0). It works, good, now what if you wanted your movie clip to be atracted to another movie clip?

Start by making another movie clip. Because being attracted to something static would be pretty uninteresting, apply this code to the new clip:

onClipEvent (load) {
    speed = 5 //how fast it moves (obviously)
}
onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
        _x -= speed;
    }
    if (Key.isDown(Key.RIGHT)) {
        _x += speed;
    }
    if (Key.isDown(Key.UP)) {
        _y -= speed;
    }
    if (Key.isDown(Key.DOWN)) {
        _y += speed;
    }
}

Now you can move the new movie clip using the arrow keys. Very Simple. Now go back to the stage and click the new movie clip. If it isn’t open already, open the Properties little tab thing. In the bottom-left corner there should be a drop down menu saying movie clip. Don’t click on it. Click on the box below it that says in washed out text, “<Instance Name>”. Type in a name for it. I’m calling mine Frank. so type in frank. Now go back to the first movie clip you made, select it and change the actionscript:

where it said this:

var xspeed:Number = ((_root._xmouse-this._x)*spring)+(xspeed*inertia);
var yspeed:Number = ((_root._ymouse-this._y)*spring)+(yspeed*inertia);

change it to this:

var xspeed:Number = ((_root.frank._x-this._x)*spring)+(xspeed*inertia);
var yspeed:Number = ((_root.frank._y-this._y)*spring)+(yspeed*inertia);

if you chose to name your second clip something other than frank, for instance, you chose to name it Gary instead, simply change every instance of frank in the above code to gary.

Now run your movie. Let the elastic clip chase Frank or Gary everywhere.

And you are done :D I’ll continue this soon if you want to add more stuff :)

I just discovered that annoyingly, unless I was to use payed services on wordpress or I moved the whole blog to Nachoman, I can barely customise this blog at all, nor can I embed flash in the blog. This is irritating as I need to embed flash heaps, but since I can’t, I will just link you to a page on Nachoman that has the finished results of this tutorial on it:

You should have something that looks nothing like this but does the same thing – http://www.nachoman.net/flash/frank.html

Thanks for Reading,

Jai :D