A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Dragging a movieclip vertically only

  1. #1
    Junior Member
    Join Date
    Dec 2003
    Location
    Johannesburg, South Africa
    Posts
    5

    Dragging a movieclip vertically only

    Hi! I want to restrain a movieclip to only be dragable vertically within an 800 x 600 size flash document. How do I do this? )Please don't tell me to use the "restrain to rectangle" function, I wouldn't know where to begin... help me from scratch?
    Last edited by houseofdesign; 12-01-2003 at 10:52 AM.

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    first give the clip an instance name dragClip (or whatever suits your movie) now in the frame that contains the clip add the following actions,

    code:

    // this assumes that the movie clip is created so its top edge is at the point y = 0
    function drag() {
    // find the position the clip should move to based on the position of the mouse relative to the main movie and where it was clicked relative to the clip being dragged
    var position = _root._ymouse - this.offset;
    // now check that this position is within the available height of the movie
    if (position < 0) position = 0;
    else if (position > 600 - this._height) position = 600 - this._height;
    // position the clip
    this._y = position;
    }
    dragClip.onPress = function() {
    this.offset = this._ymouse;
    this.onEnterFrame = drag; // start dragging the clip
    };
    dragClip.onRelease = dragClip.onReleaseOutside = function() {
    delete this.onEnterFrame; // stop dragging the clip
    };


  3. #3
    Junior Member
    Join Date
    Dec 2003
    Location
    Johannesburg, South Africa
    Posts
    5

    Re:

    Ok, so now I'm being fussy. The code has to be on a button inside the movie clip. It's a drag button in a movie clip called _root.cpMain.Silly me, forgetting little details.
    Last edited by houseofdesign; 12-01-2003 at 11:13 AM.

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    In that case you might use something like this,

    (attached to the button)

    code:

    on (press) {
    this.offest = this._ymouse;
    this.onEnterFrame = this.drag;
    }
    on (release, releaseOutside) {
    delete this.onEnterFrame;
    }



    then inside the clip in the frame that contains the button add the drag function,

    code:

    function drag() {
    // find the position the clip should move to based on the position of the mouse relative to the main movie and where it was clicked relative to the clip being dragged
    var position = _root._ymouse - this.offset;
    // now check that this position is within the available height of the movie
    if (position < 0) position = 0;
    else if (position > 600 - this._height) position = 600 - this._height;
    // position the clip
    this._y = position;
    }



    However I would point out it is considered bad practice to attach code to buttons or movie clips, wherever possible code should be placed in a frame action in the main timeline where it is easy to find and debug.

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