Does anybody know how I could make a MC keep moving a certain amount when I let go after dragging it. I want the distance it goes to be based upon how quick I moved the mouse before I let it go. Thanks for any help.
This is concept code, meaning it may or may not work, but its purpose is to give you an idea of how to approach this situation:
if (dragging) {
ox = nx;
oy = ny;
nx = _root._xmouse;
ny = _root._ymouse;
} else {
_x += (nx-ox)/7;
_y += (ny-oy)/7;
ox = nx;
oy = ny;
nx = _x;
ny = _y;
}
alright, this basic code keeps track of the previous location of the ball or whatever you’re working with and based on the distance between the points, it determines its velocity when you release it. when you do, it still records the points and eventually slows down (because of the divide by 7, this eases the object, or slows it down).
thoriphes already gave you the answer, but i figured i’d post it anyway…
MovieClip.prototype.inertialDrag = function(ratio, decel) {
var x, y, xspeed, yspeed, drag;
this.onEnterFrame = function() {
if (!drag) {
this._x += xspeed *= decel;
this._y += yspeed *= decel;
} else {
x = this._x;
y = this._y;
this._x = this._parent._xmouse;
this._y = this._parent._ymouse;
}
};
this.onPress = function() {
drag = true;
};
this.onRelease = this.onReleaseOutside=function () {
drag = false;
xspeed = (this._x-x)*ratio;
yspeed = (this._y-y)*ratio;
};
};
// usage
myMovieClip.inertialDrag(.1, .95);
Thanks guys, this works great! Perfect
excellent!