Skip to main content
Participating Frequently
July 1, 2011
Answered

Constrain a dragged movie clip to the stage

  • July 1, 2011
  • 2 replies
  • 639 views

I have a movie clip (pano_mc) that I can click and drag left and right. When I drag it to the right, it stops dragging when the left edge of the clip reaches the left edge of the stage. This is great!

When I drag the image to the left, the right side edge if the clip does not stop at the right edge of the stage, but continues to click and drag until it reaches the left edge of the stage.

I need the left edge of the clip to stop at the left edge of the  stage and the right edge of the clip to stop at the right edge of the  stage.

Here is the code I have so far

The pano_mc is registered to the top right corner and is 7024px in length. I know it is a bit large.

The axis_mc is anchored to the upper left.

pano_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void

{

    if (pano_mc.x == 0) { //I have tried 7024, 3512 and 0 but none have been successful

        pano_mc.stopDrag();

    } else {

    pano_mc.startDrag(false,new Rectangle(axis_mc.x,axis_mc.y,7024,0));

    }

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
    pano_mc.stopDrag();
}

Any suggestions?

This topic has been closed for replies.
Correct answer kglad

use:


pano_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void

{


    pano_mc.startDrag(false,new Rectangle(pano_mc.width,0,stage.stageWidth-pano-mc.width,stage.stageHeight-pano_mc.height));

   

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
    pano_mc.stopDrag();
}


// updated after seeing neds message and then reading your if-statement which serves no useful purpose.

2 replies

Ned Murphy
Legend
July 1, 2011

Maybe my mind isn't working correctly, but for what I interpret from what you wrote it doesn't make sense.   I don't see how dragging to the right will get you anywhere near the left edge of the stage.  Following that I kinda lose hope that any of it is explained correctly.

In any case, you shouldn't need to have that stopDrag conditional.  The rectangle argument of your startDrag function should be all you need to use to restrict the area in which you can drag the object.

SkwentAuthor
Participating Frequently
July 1, 2011

I did not mention that the pano_mc was centered on stage, with the excess flowing off the stage both left and right.

So by dragging the mc to the right, the right edge of the mc comes closer and closer to the left edge of the stage.

Sorry for the confusion.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 1, 2011

use:


pano_mc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void

{


    pano_mc.startDrag(false,new Rectangle(pano_mc.width,0,stage.stageWidth-pano-mc.width,stage.stageHeight-pano_mc.height));

   

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
    pano_mc.stopDrag();
}


// updated after seeing neds message and then reading your if-statement which serves no useful purpose.

SkwentAuthor
Participating Frequently
July 1, 2011

Thank you!

Now I need to disect the code to figure out exactly why it works! Your, and the other folks, answers are great learning tools.

Thanks Again!

kglad
Community Expert
Community Expert
July 1, 2011

you're welcome.