A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: constrain dragging to 45 degree line

  1. #1
    how would i constrain dragging an object along a 45 degree angle

    ive seen tutorials on how to duplicate mc's and position the dragged object that way, but i thought this would be easier seeing that the object follows a constant path

    thanks

  2. #2
    Senior Member
    Join Date
    Sep 2000
    Posts
    467
    I think something like this would do:

    on the movie clip you want to drag, place this action
    onClipEvent (load) {
    x1 = _x;
    }
    onClipEvent (enterFrame) {
    x2 = _x;
    this._y += (x1-x2);
    }



    ^1letser

  3. #3
    hmmm, i placed a mc on the stage and placed this code on the mc

    onClipEvent (load) {
    x1 = _x;
    }
    onClipEvent (mouseDown) {
    if (hitTest( _root._xmouse, _root._ymouse, false)) {
    startDrag ("");
    }
    }
    onClipEvent (enterFrame) {
    x2 = _x;
    this._y += (x1-x2);
    }


    when i clicked on the mouse it shot straight up and vanished, although playing with it a little longer a managed to keep it on screen when i moved the mouse a lot slower

    thanks for you help so far

  4. #4
    Senior Member Leo Lima's Avatar
    Join Date
    Jul 2000
    Location
    São Paulo, Brazil
    Posts
    745
    First, it will depend on where your 45 is, in which quadrant. But let's assume that you are on the first on, y>0, x>0, so your 45 graph is y = x. With that in mind, you can build something like

    Code:
    onClipEvent (load) {
      x0 = this._x;
      y0 = this._y;
      angle = 1;     // to switch the direction change to -1
    
      function YinX(x) {
        //will return the Y position about the X that enters.
        return (angle * (x - x0) + y0);
      }
    }
    
    onClipEvent (enterFrame) {
      if (drag) {
        this._x = _root._xmouse;
        this._y = YinX(this._x);
      }
    }
    
    onClipEvent (mouseDown) {
      if (this.hitTest(_root._xmouse, _root._ymouse)) 
        drag = true;
    }
    
    onClipEvent (mouseUp) {
      drag = false;
    }
    This works just fine.

    Regards,
    Leo Lima

  5. #5
    Senior Member
    Join Date
    Sep 2000
    Posts
    467
    I see the problem but I haven't got much time to solve it. you can use the code by Leo Lima, it will work just fine.

    sorry about the code,
    ^1letser

  6. #6
    Senior Member Leo Lima's Avatar
    Join Date
    Jul 2000
    Location
    São Paulo, Brazil
    Posts
    745
    You just forgot to consider y0. That's a simple algebra problem, about finding the equation of a straight line.

    Regards,
    Leo Lima

  7. #7
    nice one Leo Lima!!!

    that works a treat, much faster than the one ive developed myself.

    i ended up duplicating a load of mcs to create the 45 degree line and then positioning the draggable object to them....tedious and messy

    thanks

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