A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Question: Drag Inertia

  1. #1
    has AIDS slayerment's Avatar
    Join Date
    Apr 2002
    Location
    Tempe, Arizona, USA
    Posts
    223

    Question: Drag Inertia

    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.

  2. #2
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    Ok, it's not perfect, but it works. Place this code on a frame:
    Code:
    MovieClip.prototype.onPress = function() {
    	this.drag = true;
    	this.startDrag(false);
    	this.xPos = this._x;
    	this.yPos = this._y;
    	
    	this.onEnterFrame = function() {
    		if (this.drag) {
    			this.xspeed = this._x - this.xPos; // x mouse speed
    			this.yspeed = this._y - this.yPos; // y mouse speed
    			this.xPos = this._x;
    			this.yPos = this._y;
    		} else {
    			this._x += this.xSpeed;
    			this._y += this.ySpeed;
    			this.xSpeed *= 0.8; // decrease x speed by 20%
    			this.ySpeed *= 0.8; // decrease y speed by 20%
    			if (Math.abs(this.xSpeed) < 0.1 && Math.abs(this.ySpeed) < 0.1) { // if almost no movement, end enterframe loop
    				delete this.onEnterFrame;
    			}
    		}
    	}
    }
    
    MovieClip.prototype.onMouseUp = function() {
    	this.drag = false;
    	this.stopDrag();
    }
    Will make all movie clips on the same timeline "throwable". If you just want an instance to behave like this replace MovieClip.prototype with that instance name.

    I've attached a fla also.
    Attached Files Attached Files

  3. #3
    has AIDS slayerment's Avatar
    Join Date
    Apr 2002
    Location
    Tempe, Arizona, USA
    Posts
    223
    Thank you very much. This is more than perfect!

  4. #4
    Member
    Join Date
    Mar 2002
    Location
    Dayton, Ohio.
    Posts
    67

    Thumbs up Thank You Very Much!!!!

    @strille: This is EXACTLY what I was looking for!

    Thank you very much!

    FYI: One set of xspeed variables at lines 9 and 10 has the wrong case and should be xSpeed and ySpeed, respectively.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center