A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] Drag boundaries in AS3?

  1. #1
    Member
    Join Date
    Mar 2002
    Posts
    53

    resolved [RESOLVED] Drag boundaries in AS3?

    I'm new to AS3.0 and hope someone could help me out a bit.

    I'd like to be able to drag a movieclip/button within a restricted area.

    In AS2 I just wrote that code to the Button with the instance name BtnDrag:
    Code:
    on (press) {
       startDrag(BtnDrag, true, 0, 0, 0, -100);
    }
    This made the handler be dragable on a line from Y:0 to Y:-100.

    In AS3.0 I wrote following code into the same frame as the movieclip/button is and got this far at the moment:
    Code:
    McBtnDrag.BtnDrag.addEventListener(MouseEvent.MOUSE_DOWN, FncBtnDragOn);
    function FncBtnDragOn(event:MouseEvent):void {
    	McBtnDrag.startDrag();
    }
    With this code I'm able to drag the movieclip McBtnDrag wich contains the handler-button called BtnDrag.
    What I'm missing however is how to add the restriction area into the startDrag command. Now I'm pretty much sure I miss some really important thing on how AS3.0 works and fear the approach I'm taking is not working at all. Would anyone be so kind and help me out here?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    startDrag takes two optional parameters, lockCenter and bounds. Look at the livedocs. http://livedocs.adobe.com/flash/9.0/...ay/Sprite.html

    You could do this:
    Code:
    McBtnDrag.BtnDrag.addEventListener(MouseEvent.MOUSE_DOWN, FncBtnDragOn);
    var boundsRect:Rectangle = new Rectangle(McBtnDrag.x, -100, 0, 100);
    function FncBtnDragOn(event:MouseEvent):void {
    	McBtnDrag.startDrag(false, boundsRect);
    }

  3. #3
    Member
    Join Date
    Mar 2002
    Posts
    53
    Thank you very much. I think I got that working with the rectangle. Unfortunately fixing this revealed there is another problem still. To make it stop dragging I usually used this code in AS2:
    Code:
    on (release, releaseOutside) {
    	stopDrag();
    Like this i made sure, that when the left mouse button is released, it's not being dragged anymore. This worked no matter whether the cursor was on the interactive object still or off it.

    My AS3 Code so far is:
    Code:
    McBtnDrag.BtnDrag.addEventListener(MouseEvent.MOUSE_UP, FncBtnDragOff);
    function FncBtnDragOff(event:MouseEvent):void {
    	McBtnDrag.stopDrag();
    }
    Now if the crusor is on the interactive object while the mouse button is being released, the object stops being dragged. So far so good.
    But since I'm restricting the movement of the object with a rectangle defined like this:
    Code:
    var boundsRect:Rectangle = new Rectangle(100, 100, 800, 0);
    the object is following a straight X-line no matter the mouse's Y-position. Therefore the crursor very often is not on the object anymore when the user releases the mouse button. If that is the case, the object will stillbe dragged.

    so I wonder how can I get that old "releaseOutside" effect?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Add your mouse up listener to the stage instead of the button.
    Code:
    McBtnDrag.BtnDrag.addEventListener(MouseEvent.MOUSE_DOWN, FncBtnDragOn);
    var boundsRect:Rectangle = new Rectangle(100, 100, 800, 0);
    function FncBtnDragOn(event:MouseEvent):void {
    	McBtnDrag.startDrag(false, boundsRect);
            stage.addEventListener(MouseEvent.MOUSE_UP, FncBtnDragOff);
    }
    
    function FncBtnDragOff(event:MouseEvent):void {
    	McBtnDrag.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_UP, FncBtnDragOff);
    }
    Here, I add the mouse_up listener when starting the drag and remove it after one dispatch so that it is only active when the button is actually being dragged. You could get the same effect with a boolean variable keeping track of whether the button is currently being dragged, but you'd have to check it inside FncBtnDragOff.
    You may still have issues when releasing the mouse button outside of the swf entirely.

  5. #5
    Member
    Join Date
    Mar 2002
    Posts
    53

    resolved

    Sometimes it's as simple as that. Thank you very much.

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