views:

51

answers:

2

Hi,

I'm trying to create an inertia effect for dragging that's constricted to the x axis. I know this questions was asked before here but I couldn't understand the answer!

I'm currently using startDrag and stopDrag. I'm assuming I should add something to the startDrag function that captures the speed in which the user is moving the mouse, but I'm not sure how this is done. I guess then I'd add something to the stopDrag function that kicks in and makes the object keep moving at that speed and then come to a stop.

Any suggestions?

A: 

Don't use startDrag and stopDrag. Instead use something like this:

var myMC:MovieClip = new MovieClip();
myMC.graphics.beginFill(0x00ff00);
myMC.graphics.drawRect(-50, -50, 100, 100);
myMC.graphics.endFill();
myMC.x = myMC.y = 50;
addChild(myMC);

myMC.addEventListener(MouseEvent.MOUSE_DOWN, startMotionHandler, false, 0, true);

function startMotionHandler($evt:MouseEvent):void {
    myMC.removeEventListener(MouseEvent.MOUSE_DOWN, startMotionHandler);
    stage.addEventListener(Event.ENTER_FRAME, handleMotion, false, 0, true);
    stage.addEventListener(MouseEvent.MOUSE_UP, stopMotionHandler, false, 0, true);
}


var damping:Number = 8;

function handleMotion($evt:Event):void {
    myMC.x += (stage.mouseX - myMC.x) / damping;
    myMC.y += (stage.mouseY - myMC.y) / damping;
}

function stopMotionHandler($evt:MouseEvent):void {
    myMC.addEventListener(MouseEvent.MOUSE_DOWN, startMotionHandler, false, 0, true);
    stage.removeEventListener(Event.ENTER_FRAME, handleMotion);
    stage.removeEventListener(MouseEvent.MOUSE_UP, stopMotionHandler);
}

Just put this code on the first frame of a new, blank FLA and run it.

sberry2A
Hmmm. Not sure I understand... What do I put inside handleMotion?
Maria
Hey... Thanks for the help. This approximates what I'm trying to do. If I may ask you--...I'd like the object to be dragged along with mouse-down (just like it looks like with startDrag) but then experience inertia in that same direction when mouse-up (but the object shouldn't change directions and start following the mouse in the opposite direction unless I press the mouse again).Really, truly appreciate your help!
Maria
I guess I'm not there yet like I thought.
Maria
A: 

I've found an grat online tutorial that has the answer. I was a matter of tinkering with the code. The writer even has .fla files for download.

http://www.quasiuseful.com/?id=11

Maria