A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [MX04] Drag and Drop- locking object into center of target...

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    6

    [MX04] Drag and Drop- locking object into center of target...

    Hello all,
    New member and relatively new Flash user. I created a drag and drop in Flash MX 2004, and I'm trying to find script that will lock the object into the center of the target when it is dragged over, sort of like a magnet effect. Here is the script I currently have on each object:


    on(press) {
    startDrag(this);
    _root.reply.text="";
    xstart = this._x;
    ystart = this._y;
    }

    on(release) {
    stopDrag();
    if (this._droptarget == "/pinottarget") {
    _root.reply.text="Correct!";
    this.enabled = false;
    _root.counter++;
    }

    else{
    _root.reply.text="No, keep trying!";
    this._x = xstart;
    this._y = ystart;
    }
    }




    If anybody can help me out with the correct script to do what I'm trying to do, I would greatly appreciate it. Attached is the source file. Thanks.

    Lee
    Attached Files Attached Files

  2. #2
    Senior Member hum's Avatar
    Join Date
    Sep 2003
    Location
    CloudCuckooland
    Posts
    1,714
    Hi
    Maybe this example found on flashkit will help you:
    code:
    // dragMe_mc is the instanceName of the movieClip
    // Starts the drag onPress
    dragMe_mc.onPress = function() {
    this.origX = this._x;
    this.origY = this._y;
    this.startDrag();
    };

    dragMe_mc.onRelease = function() {
    // stops drag onRelease
    this.stopDrag();
    // dymainc txtField+Message displays "error"
    if (dropHere_mc.hitTest("_root.dragMe_mc")) {
    // snaps the dragMe_mc to the dropHere_mc in the middle of _x&_y
    this._x = _root.dropHere_mc._x;
    this._y = _root.dropHere_mc._y;
    // dynamic txtField+message displays "success"
    infoText = "Congratulations you dropped it correctly";
    // disables this dragMe_mc from being dragged again after dropped correctly
    this.enabled = false;
    } else {
    infoText = "You dropped it incorrectly";
    this.onEnterFrame = function() {
    //temporary values here to hold the x & y distance because
    // they're being referenced more than once
    var dx = this.origX-this._x;
    var dy = this.origY-this._y;
    //approximation of the distance to the original position
    if (Math.max(dx, dy)<.1) {
    // stop moving when we're close enough
    this.onEnterFrame = undefined;
    }
    this._x += dx/3;
    // 3 controls the speed - use a larger number for slower movement
    this._y += dy/3;
    };
    }
    };


  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    See attached file...
    Last edited by dawsonk; 08-22-2007 at 10:50 AM.

  4. #4
    God yeps's Avatar
    Join Date
    Dec 2005
    Location
    :noitacoL
    Posts
    660
    personally, im not a big fan of the whole drag and drop thing.. i always use

    if (this._x<_root._xmouse){
    this._x += 5
    }if (this._x>_root._xmouse){
    this._x -= 5
    }if (this._y<_root._ymouse){
    this._y += 5
    }if (this._y>_root._ymouse){
    this._y -= 5
    }

    give that a shot
    Insert thoughts here.

  5. #5
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    im kind of in a hurry so i cant explain it all but i know when you use the function
    startDrag();
    you can give it the parameter
    lockcenter
    like this
    startDrag(lockcenter);
    if you want more parameters when you type
    startDrag(
    flash should give you a list of example parameters

  6. #6
    Junior Member
    Join Date
    Jul 2007
    Posts
    6

    Thanks.

    dawsonk- Thanks for doing all the work for me, and more importantly showing me a better script than the one I was using.

    Others- Thanks for your help as well. This forum rocks. Hopefully I'll get good enough to actually contribute.

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