PDA

View Full Version : Question: Drag Inertia


slayerment
06-23-2003, 11:26 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.

subquark
06-24-2003, 06:15 AM
In a frame: //bounce prototype
MovieClip.prototype.bounceMe = function(targX, targY) {
this.speedY = this.speedY*this.damping+(targY-this._y)*this.easing;
this._y += this.speedY;
this.speedX = this.speedX*this.damping+(targX-this._x)*this.easing;
this._x += this.speedX;
};
on the object: onClipEvent(load){
type = "throw"
// create bounds of object which its to be constrained
bounds = {left:348,right:768,up:189,down:341}
// the vector is its movement. vector[0] is the _x movement
// while vector[1] is the _y movement
vector = [0,0];
// your force of gravity
gravity = 5;
}
onClipEvent(enterFrame){
if (dragging){
_x = _parent._xmouse
_y = _parent._ymouse
lastPos = thisPos
thisPos = [_x,_y]
dragVector = [thisPos[0]-lastPos[0],lastPos[1]-thisPos[1]]
}else{
// add your vector movements to this objects location
// because 0 for the _y starts at the top, we subtract for _y movement
_x += vector[0]
_y -= vector[1]

// checking bounds:
// checking for _x
if (_x <= bounds.left){
_x = bounds.left // place it back within bounds
vector[0] *= -1 // change the direction of the vector
}else if (_x >= bounds.right){
_x = bounds.right // place it back within bounds
vector[0] *= -1 // change the direction of the vector
}

// checking for _y
if (_y <= bounds.up){
_y = bounds.up // place it back within bounds
vector[1] *= -1 // change the direction of the vector
}else if (_y >= bounds.down){
_y = bounds.down // place it back within bounds
vector[1] *= -1 // change the direction of the vector
}

// this simulates air resistance, otherwise, the object may move forever
// reducing the _y and _x movement by 8%
vector[0] *= .96
vector[1] *= .94

// figure in gravity for the _y vector
vector[1] -= gravity
}
}
looks like the "Q" on my site. This code is not mine, I wish I could do this! (I can't find the original one to let you know who did it :( )

subquark
06-26-2003, 07:14 AM
ah ha! That code is from Senocular! See his site, great tutorials and flas. :)

slayerment
06-26-2003, 09:36 AM
Thank you, that worked great!

agent81
06-26-2003, 02:16 PM
subquark, where are you calling bounceMe, from your movieclip?

I do have a bad case of domestic blindness, but i can't seem to see where it's used?

subquark
06-26-2003, 02:37 PM
in the same frame as the mc to be affected:stop();
//bounce prototype
MovieClip.prototype.bounceMe = function(targX, targY) {
this.speedY = this.speedY*this.damping+(targY-this._y)*this.easing;
this._y += this.speedY;
this.speedX = this.speedX*this.damping+(targX-this._x)*this.easing;
this._x += this.speedX;
};

and on the actual mc instance:onClipEvent(load){
type = "throw"
// create bounds of object which its to be constrained
bounds = {left:348,right:768,up:189,down:341}
// the vector is its movement. vector[0] is the _x movement
// while vector[1] is the _y movement
vector = [0,0];
// your force of gravity
gravity = 5;
}
onClipEvent(enterFrame){
if (dragging){
_x = _parent._xmouse
_y = _parent._ymouse
lastPos = thisPos
thisPos = [_x,_y]
dragVector = [thisPos[0]-lastPos[0],lastPos[1]-thisPos[1]]
}else{
// add your vector movements to this objects location
// because 0 for the _y starts at the top, we subtract for _y movement
_x += vector[0]
_y -= vector[1]

// checking bounds:
// checking for _x
if (_x <= bounds.left){
_x = bounds.left // place it back within bounds
vector[0] *= -1 // change the direction of the vector
}else if (_x >= bounds.right){
_x = bounds.right // place it back within bounds
vector[0] *= -1 // change the direction of the vector
}

// checking for _y
if (_y <= bounds.up){
_y = bounds.up // place it back within bounds
vector[1] *= -1 // change the direction of the vector
}else if (_y >= bounds.down){
_y = bounds.down // place it back within bounds
vector[1] *= -1 // change the direction of the vector
}

// this simulates air resistance, otherwise, the object may move forever
// reducing the _y and _x movement by 8%
vector[0] *= .96
vector[1] *= .94

// figure in gravity for the _y vector
vector[1] -= gravity
}
}
I guess it's called when the playhead enters the frame:confused:

go to Senocular's site for the original file called bounce.fla