A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Drag in a Straight Line

  1. #1
    Senior Member
    Join Date
    Jun 2001
    Posts
    343

    Drag in a Straight Line

    I know you can restrain an mc in a rectangle when using startDrag, but is there a way to have the mc only go horizontal or vertical without going off diagonally? Sort of like a slider that can only move in a straight line in four directions (like a cross).

    Any help is most appreciated.

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    PHP Code:
    mc.startDrag (false, new Rectangle (0mc.y5000)); 
    Think you can set the y of the rectangle to the MovieClip's y and set the height to 0.

  3. #3
    Senior Member
    Join Date
    Jun 2001
    Posts
    343
    Thanks for the reply. Unfortunately that only allows the user to drag horizontally. I also need the option of the mc to be dragged vertically as well. The mc would either be dragged straight up/down OR left/right.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Oh I should have read more carefully... well this is what I came up with

    PHP Code:
    var awd:MovieClip = new MovieClip ();
    awd.graphics.beginFill (0x000000);
    awd.graphics.drawRect (-20, -204040);
    awd.graphics.endFill ();
    awd.stage.stageWidth 2;
    awd.stage.stageHeight 2;
    this.addChild (awd);

    var 
    startpoint:Point;//The point where you click on the square

    awd.addEventListener (MouseEvent.MOUSE_DOWNonDownfalse0true);
    function 
    onDown (e:MouseEvent) {
        
    startpoint = new Point (mouseXmouseY);
        
    awd.addEventListener (MouseEvent.MOUSE_MOVEonMovefalse0true);
    }

    function 
    onMove (e:MouseEvent) {
        
    awd.removeEventListener (MouseEvent.MOUSE_MOVEonMove);
        
        var 
    xdiff:Number Math.abs (startpoint.mouseX);//Difference between starting point and the mouse
        
    var ydiff:Number Math.abs (startpoint.mouseY);
        
        if (
    xdiff ydiff) {//If x difference is larger than y then you're trying to drag horizontally
            
    awd.startDrag (true, new Rectangle (0awd.ystage.stageWidth0));
        } else {
    //Or else it's vertically
            
    awd.startDrag (true, new Rectangle (awd.x00stage.stageHeight));
        }
    }

    stage.addEventListener (MouseEvent.MOUSE_UPonUpfalse0true);
    function 
    onUp (e:MouseEvent) {
        
    awd.stopDrag ();

    It's not the most accurate in terms of judging whether you want to move horizontally or vertically but it's a start.

  5. #5
    Senior Member
    Join Date
    Jun 2001
    Posts
    343
    Wow. It's more than just a start. I was really struggling with this one. Thank you so much. Now I'm going to mess around with it to get it to work with my current code. Thanks again!

Tags for this Thread

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