MovieClip.prototype.easeTo = function(x, y) {
this.onEnterFrame = function() {
this._x = x-(x-this._x)/1.2;
this._y = y-(y-this._y)/1.2;
this._x>x-1 && this._x<x+1 ? delete this.onEnterFrame : null;
};
};
home.onRollOver = products.onRollOver=services.onRollOver=support.onRollOver=function () {
bar.easeTo(this._x-this._width/2-15, this._y);
};
I define easeTo in the prototype object of the MovieClip class, so that every movieclip can access it. As arguments, I define x and y to ease to. Y is actually not necessary here, but it can come in handy for later use.
Then, I define the onEnterFrame handler of the movieclip you use easeTo on. Say I’d use voetsjoeba.easeTo(500,250), then I’m setting the onEnterFrame handler of the movieclip voetsjoeba.
Inside that handler is the code that makes the movieclip ease to the x and y position you passed along to the function as arguments, plus a line that checks if it has reached it’s target, and if so delete the onEnterFrame handler, if not, do nothing.
The first two lines are the actual code that create the movement. What they basically do is get the distance to the target, then dividing that distance by 1.2 onEnterFrame, creating an easing movement. Both for x and y can this be applied. The way you should remember this code is:
property = targetvalue-(targetvalue-property)/1.2
That is my code for easing a property. There are others, but I invented this one myself and I’m sticking with it. You can alter the speed by changing 1.2 to a higher or lower value - higher is faster, lower is slower. Just don’t set it to 1 or anything underneath or you’ll get no movement for 1 or a failed elastic like movement with a value less than one. You can also apply this code on many properties, such as height, width, alpha (thought that doesn’t have much use), even colour easing.
The longer line checks if the current x position is less than target + 1 and more than target -1, so we are actually having a buffer here in which the movieclip may stop easing. I only check for the x position, since logically the y will be there at the same time of the x. The dividing by 1.2 in the easing code takes care of that. If it is inside that buffer, it deletes his onEnterFrame stopping the movement. We need the buffer because it wxill never reach it’s target value, always something very very close to it because of the dividing by 1.2.