View Full Version : Question: Drag Inertia
slayerment
June 23rd, 2003, 07:12 PM
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.
thoriphes
June 23rd, 2003, 07:44 PM
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).
kode
June 23rd, 2003, 08:14 PM
thoriphes already gave you the answer, but i figured i'd post it anyway... :P
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);
slayerment
June 23rd, 2003, 08:23 PM
Thanks guys, this works great! Perfect :)
kode
June 23rd, 2003, 08:24 PM
;)
Neo-Geo
June 23rd, 2003, 10:40 PM
excellent!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.